Median Search Demo  0.4
Median search demo (lib used by adaptive median image filter)
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)
 Pixel-swapping macro. More...
 
pixelvalue quick_select (pixelvalue a[], int n)
 Function implementing quickselect algorithm. More...
 
pixelvalue kth_smallest (pixelvalue a[], int n, int k)
 Function implementing kth_smallest() for Wirth macro. More...
 
pixelvalue wirth (pixelvalue a[], int n)
 Function wrapper for kth_smallest to get Wirth's median. More...
 
pixelvalue torben (pixelvalue m[], int n)
 Function implementing Torben's algorithm. More...
 

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()

  • In : array of elements, # of elements in the array, rank k
  • Out : one element

Definition at line 130 of file medians_1D.c.

References swap().

Referenced by wirth().

130  {
131  register int i,j,l,m ;
132  register pixelvalue x ;
133 
134  l=0 ; m=n-1 ;
135  while (l<m) {
136  x=a[k] ;
137  i=l ;
138  j=m ;
139  do {
140  while (a[i]<x) i++ ;
141  while (x<a[j]) j-- ;
142  if (i<=j) {
143  swap(&a[i],&a[j]) ;
144  i++ ; j-- ;
145  }
146  } while (i<=j) ;
147  if (j<k) l=i ;
148  if (k<i) m=j ;
149  }
150  return a[k] ;
151 }
void swap(pixelvalue *a, pixelvalue *b)
Pixel-swapping macro.
Definition: medians_1D.c:53

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()

  • In : array of elements, # of elements in the array
  • Out : one element

Definition at line 70 of file medians_1D.c.

References swap().

Referenced by bench().

70  {
71  int low, high ;
72  int median;
73  int middle, ll, hh;
74 
75  low = 0 ; high = n-1 ; median = (low + high) / 2;
76  for (;;) {
77  if (high <= low) /* One element only */
78  return a[median] ;
79 
80  if (high == low + 1) { /* Two elements only */
81  if (a[low] > a[high])
82  swap(&a[low], &a[high]) ;
83  return a[median] ;
84  }
85 
86  /* Find median of low, middle and high items; swap into position low */
87  middle = (low + high) / 2;
88  if (a[middle] > a[high]) swap(&a[middle], &a[high]) ;
89  if (a[low] > a[high]) swap(&a[low], &a[high]) ;
90  if (a[middle] > a[low]) swap(&a[middle], &a[low]) ;
91 
92  /* Swap low item (now in position middle) into position (low+1) */
93  swap(&a[middle], &a[low+1]) ;
94 
95  /* Nibble from each end towards middle, swapping items when stuck */
96  ll = low + 1;
97  hh = high;
98  for (;;) {
99  do ll++; while (a[low] > a[ll]) ;
100  do hh--; while (a[hh] > a[low]) ;
101 
102  if (hh < ll)
103  break;
104 
105  swap(&a[ll], &a[hh]) ;
106  }
107 
108  /* Swap middle item (in position low) back into correct position */
109  swap(&a[low], &a[hh]) ;
110 
111  /* Re-set active partition */
112  if (hh <= median)
113  low = ll;
114  if (hh >= median)
115  high = hh - 1;
116  }
117 }
void swap(pixelvalue *a, pixelvalue *b)
Pixel-swapping macro.
Definition: medians_1D.c:53

Here is the call graph for this function:

Here is the caller graph for this function:

void swap ( pixelvalue *  a,
pixelvalue *  b 
)

Pixel-swapping macro.

Macro left-over from initial implementation. Need to change to a real function and let the compiler do the workFunction implementing basic pixel swapping algorithm

Definition at line 53 of file medians_1D.c.

Referenced by kth_smallest(), and quick_select().

53  {
54  register pixelvalue t;
55  t=*a; *a=*b; *b=t;
56  return;
57 }

Here is the caller graph for this function:

pixelvalue torben ( pixelvalue  m[],
int  n 
)

Function implementing Torben's algorithm.

Function : torben()

  • In : array of elements, # of elements in the array
  • Out : one element

Definition at line 178 of file medians_1D.c.

Referenced by bench().

178  {
179  int i, less, greater, equal, half;
180  pixelvalue min, max, guess, maxltguess, mingtguess;
181 
182  half = (n+1)/2 ;
183  min = max = m[0] ;
184  for (i=1 ; i<n ; i++) {
185  if (m[i]<min) min=m[i];
186  if (m[i]>max) max=m[i];
187  }
188 
189  while (1) {
190  guess = (min+max)/2;
191  less = 0; greater = 0; equal = 0;
192  maxltguess = min ;
193  mingtguess = max ;
194  for (i=0; i<n; i++) {
195  if (m[i]<guess) {
196  less++;
197  if (m[i]>maxltguess) maxltguess = m[i] ;
198  } else if (m[i]>guess) {
199  greater++;
200  if (m[i]<mingtguess) mingtguess = m[i] ;
201  } else equal++;
202  }
203  if (less <= half && greater <= half) break ;
204  else if (less>greater) max = maxltguess ;
205  else min = mingtguess;
206  }
207  if (less >= half) return maxltguess;
208  else if (less+equal >= half) return guess;
209  else return mingtguess;
210 }

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()

  • In : array of elements, # of elements in the array
  • Out : one element

Definition at line 163 of file medians_1D.c.

References kth_smallest().

Referenced by bench().

163  {
164  return kth_smallest(a,n,(((n)&1)?((n)/2):(((n)/2)-1)));
165 }
pixelvalue kth_smallest(pixelvalue a[], int n, int k)
Function implementing kth_smallest() for Wirth macro.
Definition: medians_1D.c:130

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.