混合中值濾波器 ( Hybrid Median Filter ) C++ 實現
在中值濾波器和均值濾波器之後, 我們看到是中值濾波器的改版,
圖截的有些模糊, 但是還是看出了大致的流程, 首先當前像素的
原圖
噪聲污染:
從前面的圖片可以看到, 圖片從高銳度經由混合中值濾波之後,
把圖放大之後看的很清楚了, 在紅色的圓圈圈出來的地方, 不出
老規矩, 最後再貼一下處理的函數代碼:
view plaincopy to clipboardprint?
unsigned char median(unsigned char* elements, int width)
{
// Order elements (only half of them)
for (int i = 0; i < (width >> 1) + 1; ++i)
{
// Find position of minimum element
int min = i;
for (int j = i + 1; j < width; ++j)
if (elements[j] < elements[min])
min = j;
// Put found minimum element in its place
unsigned char temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}
// Get result - the middle element
return elements[width >> 1];
}
/**
** method to remove noise from the corrupted image by hybrid 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 hybridMedianFilter (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++)
{
unsigned char window[5];
unsigned char results[3];
// Pick up cross-window elements
window[0] = corrupted[(j - 1) * width + i];
window[1] = corrupted[j * width + i - 1];
window[2] = corrupted[j * width + i];
window[3] = corrupted[j * width + i + 1];
window[4] = corrupted[(j + 1) * width + i];
// Get median
results[0] = median(window, 5);
// Pick up x-window elements
window[0] = corrupted[(j - 1) * width + i - 1];
window[1] = corrupted[(j - 1) * width + i + 1];
window[2] = corrupted[j * width + i];
window[3] = corrupted[(j + 1) * width + i - 1];
window[4] = corrupted[(j + 1) * width + i + 1];
// Get median
results[1] = median(window, 5);
// Pick up leading element
results[2] = corrupted[j * width + i];
// Get result
smooth[j*width+i] = median(results, 3);
}
}
}
因為要取中值, 還附加了一個取中值的函數median:)
本文來自CSDN博客,轉載請標明出處:http://