均值濾波器 ( Mean Filter ) C++ 實現
均值濾波器是 「把每個像素都用周圍的8個像素來做均值操作」,
非常明顯的, 這個3*3區域像素的顏色值分別是5,3,6,
一目了然。那麼這個均值濾波器有什麼用處呢?
主要還是平滑圖像的用處, 有的圖像的銳度很高,
原圖:
這裡還是可以明顯的感覺到不同的, 沒有好壞之分,
噪聲圖(5%):
首先這裡的噪聲還是比較小的, 只有5%,
view plaincopy to clipboardprint?
/**
** method to remove noise from the corrupted image by mean 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 meanFilter (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++)
{
smooth [ j*width+i ] = (corrupted [ (j-1)*width+(i-1) ] + corrupted [ (j-1)*width+i] +
corrupted [ (j-1)*width+(i+1) ] + corrupted [ j*width+(i-1) ] +
corrupted [ j*width+i] + corrupted [ j*width+(i+1) ] +
corrupted [ (j+1)*width+(i-1) ] + corrupted [ (j+1)*width+i] +
corrupted [ (j+1)*width+(i+1) ] ) / 9;
}
}
}
一般處理的時候通常還有邊界上的一些處理, 但是這裡就簡單的從
本文來自优快云博客,轉載請標明出處:http://