中值濾波器 ( Median Filter ) C++ 實現
有了前面一個均值濾波器 的基礎, 在看中值濾波器就不是很容易
這把可以清晰地看到, 這裡有6,2,0,3,97,4,19,
原圖1
噪聲圖(5%)
非常impressive的一點在這裡就可以看出來了, 很明顯
一樣的,最後還是貼一段我運行的代碼:
view plaincopy to clipboardprint?
/**
** 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++
// 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 place
unsigned char temp = window[m];
window[m] = window[min];
window[min] = temp;
}
// 從這裡知道, 圖像中的每一個像素都經過這樣的處理(四條
smooth[ j*width+i ] = window[4];
}
}
}
本文來自CSDN博客,轉載請標明出處:http://