medians_1D.c File Reference

#include "medians_1D.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Include dependency graph for medians_1D.c:

Go to the source code of this file.

Functions

void swap (pixelvalue *a, pixelvalue *b)
 Function implementing basic pixel swapping algorithm.
pixelvalue quick_select (pixelvalue a[], int n)
 Function implementing quickselect algorithm.
pixelvalue kth_smallest (pixelvalue a[], int n, int k)
 Function implementing kth_smallest() for Wirth macro.
pixelvalue wirth (pixelvalue a[], int n)
 Function wrapper for kth_smallest to get Wirth's median.
pixelvalue torben (pixelvalue m[], int n)
 Function implementing Torben's algorithm.

Variables

static const char rcsid []


Function Documentation

pixelvalue kth_smallest ( pixelvalue  a[],
int  n,
int  k 
)

Function implementing kth_smallest() for Wirth macro.

Function : kth_smallest()

Definition at line 130 of file medians_1D.c.

References swap().

Referenced by wirth().

00130                                            {
00131     register int i,j,l,m ;
00132     register pixelvalue x ;
00133 
00134     l=0 ; m=n-1 ;
00135     while (l<m) {
00136         x=a[k] ;
00137         i=l ;
00138         j=m ;
00139         do {
00140             while (a[i]<x) i++ ;
00141             while (x<a[j]) j-- ;
00142             if (i<=j) {
00143                 swap(&a[i],&a[j]) ;
00144                 i++ ; j-- ;
00145             }
00146         } while (i<=j) ;
00147         if (j<k) l=i ;
00148         if (k<i) m=j ;
00149     }
00150     return a[k] ;
00151 }

Here is the call graph for this function:

Here is the caller graph for this function:

pixelvalue quick_select ( pixelvalue  a[],
int  n 
)

Function implementing quickselect algorithm.

Function : quick_select()

Definition at line 70 of file medians_1D.c.

References swap().

Referenced by bench().

00070                                     {
00071     int low, high ;
00072     int median;
00073     int middle, ll, hh;
00074 
00075     low = 0 ; high = n-1 ; median = (low + high) / 2;
00076     for (;;) {
00077         if (high <= low) /* One element only */
00078             return a[median] ;
00079 
00080         if (high == low + 1) {  /* Two elements only */
00081             if (a[low] > a[high])
00082                 swap(&a[low], &a[high]) ;
00083             return a[median] ;
00084         }
00085 
00086     /* Find median of low, middle and high items; swap into position low */
00087         middle = (low + high) / 2;
00088         if (a[middle] > a[high])    swap(&a[middle], &a[high]) ;
00089         if (a[low] > a[high])       swap(&a[low], &a[high]) ;
00090         if (a[middle] > a[low])     swap(&a[middle], &a[low]) ;
00091 
00092     /* Swap low item (now in position middle) into position (low+1) */
00093         swap(&a[middle], &a[low+1]) ;
00094 
00095     /* Nibble from each end towards middle, swapping items when stuck */
00096         ll = low + 1;
00097         hh = high;
00098         for (;;) {
00099             do ll++; while (a[low] > a[ll]) ;
00100             do hh--; while (a[hh]  > a[low]) ;
00101 
00102             if (hh < ll)
00103             break;
00104 
00105             swap(&a[ll], &a[hh]) ;
00106         }
00107         
00108         /* Swap middle item (in position low) back into correct position */
00109         swap(&a[low], &a[hh]) ;
00110         
00111         /* Re-set active partition */
00112         if (hh <= median)
00113             low = ll;
00114         if (hh >= median)
00115             high = hh - 1;
00116     }
00117 }   

Here is the call graph for this function:

Here is the caller graph for this function:

void swap ( pixelvalue a,
pixelvalue b 
)

Function implementing basic pixel swapping algorithm.

Macro left-over from initial implementation. Need to change to a real function and let the compiler do the work

Definition at line 53 of file medians_1D.c.

Referenced by kth_smallest(), and quick_select().

00053                                         {
00054     register pixelvalue t;
00055     t=*a; *a=*b; *b=t;
00056     return;
00057 }

Here is the caller graph for this function:

pixelvalue torben ( pixelvalue  a[],
int  n 
)

Function implementing Torben's algorithm.

Function : torben()

Definition at line 178 of file medians_1D.c.

Referenced by bench().

00178                               {
00179     int         i, less, greater, equal, half;
00180     pixelvalue  min, max, guess, maxltguess, mingtguess;
00181 
00182     half = (n+1)/2 ;
00183     min = max = m[0] ;
00184     for (i=1 ; i<n ; i++) {
00185         if (m[i]<min) min=m[i];
00186         if (m[i]>max) max=m[i];
00187     }
00188 
00189     while (1) {
00190         guess = (min+max)/2;
00191         less = 0; greater = 0; equal = 0;
00192         maxltguess = min ;
00193         mingtguess = max ;
00194         for (i=0; i<n; i++) {
00195             if (m[i]<guess) {
00196                 less++;
00197                 if (m[i]>maxltguess) maxltguess = m[i] ;
00198             } else if (m[i]>guess) {
00199                 greater++;
00200                 if (m[i]<mingtguess) mingtguess = m[i] ;
00201             } else equal++;
00202         }
00203         if (less <= half && greater <= half) break ;
00204         else if (less>greater) max = maxltguess ;
00205         else min = mingtguess; 
00206     }
00207     if (less >= half) return maxltguess;
00208     else if (less+equal >= half) return guess;
00209     else return mingtguess;
00210 }

Here is the caller graph for this function:

pixelvalue wirth ( pixelvalue  a[],
int  n 
)

Function wrapper for kth_smallest to get Wirth's median.

Function : wirth()

Definition at line 163 of file medians_1D.c.

References kth_smallest().

Referenced by bench().

00163                              {
00164     return kth_smallest(a,n,(((n)&1)?((n)/2):(((n)/2)-1)));
00165 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char rcsid[] [static]

Initial value:

    "$Id"

Definition at line 38 of file medians_1D.c.


Generated on Fri Dec 8 18:11:27 2006 for Median Filtering Routines by  doxygen 1.5.1