均值濾波器就是在圖像處理的時(shí)候, “把每個(gè)像素都用周?chē)?個(gè)像素來(lái)做均值操作 ”, 比如說(shuō)這里有一個(gè)例子:
非常明顯, 這個(gè)3*3區域像素的顏色值分別是5,3,6,2,1,9,8,4,7那么中間的1這個(gè)像素的過(guò)濾后的值就是這些值的平均值, 也就是前面的計算方法: (5+3+6+2+1+9+8+4+7)/9=5一目了然。
那么這個(gè)均值濾波器有什么用處呢?主要還是平滑圖像的用處, 有的圖像的銳度很高,用這樣的均值算法,可以把銳度降低。使得圖像看上去更加自然,下面就有幾幅圖我們可以看出一些端倪:
原圖:
平滑處理之后:
這里還是可以明顯的感覺(jué)到不同的, 沒(méi)有好壞之分,就是第二幅圖片看上去更為平滑。 那這里均值平滑是否具有去除噪聲的功能呢? 我們搞來(lái)了椒鹽噪聲(就是隨機的白點(diǎn),黑點(diǎn))來(lái)試試手:
噪聲圖(5%):
平滑處理之后:
首先這里的噪聲還是比較小的, 只有5%,從均值的效果來(lái)看的話(huà), 我可以說(shuō)幾乎沒(méi)有用,其實(shí)直觀(guān)的想也可以判斷, 因為這里的處理并沒(méi)有剔除這些噪聲點(diǎn), 而只是微弱地降低了噪聲,所以效果可以想見(jiàn)的。。
一段處理的代碼:
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;
}
}
}
/**
** 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;
}
}
}
一般處理的時(shí)候通常還有邊界上的一些處理, 這里就簡(jiǎn)單的從1...width-1來(lái)處理, 所以第一個(gè)和最后一個(gè)像素就簡(jiǎn)單的拋掉了, 如果只是簡(jiǎn)單的看看效果還是沒(méi)有問(wèn)題的!
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。