均值滤波是像素周围的33的像素做平均值操作, 那么中值就是在33中的像素中寻找中值
一般来说这个中值滤波是去除椒盐噪声的非常理想的选择。
/**
** method to remove noise from the corrupted image by median value
* @param corrupted input grayscale binary array with corrupted info
* @param smooth output data for smooth result, the memory need to be allocated outside of the function
* @param width width of the input grayscale image
* @param height height of the input grayscale image
*/
void medianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );for (int j=1;j<height-1;j++){for (int i=1;i<width-1;i++){int k = 0;unsigned char window[9];for (int jj = j - 1; jj < j + 2; ++jj)for (int ii = i - 1; ii < i + 2; ++ii)window[k++] = corrupted[jj * width + ii];// Order elements (only half of them)for (int m = 0; m < 5; ++m){int min = m;for (int n = m + 1; n < 9; ++n)if (window[n] < window[min])min = n;// Put found minimum element in its placeunsigned char temp = window[m];window[m] = window[min];window[min] = temp;}smooth[ j*width+i ] = window[4];}}
}