中值濾波器 ( Median Filter ) C++ 實現

來源:互聯網
上載者:User

 有了前面一個均值濾波器

的基礎, 在看中值濾波器就不是很容易繼續了。均值濾波是像素周圍的3*3的像素做平均值操作, 那麼中值就是在3*3中的像素中尋找中值。 來看這樣一個描述圖(無圖無真相)

這把可以清晰地看到, 這裡有6,2,0,3,97,4,19,3,10這些像素, 然後中間的這些像素值就被這些像素的中位元也就是中值取代了。為了滿足和前面一篇文章的格式相對應, 我們馬上進入下一個單元, 來看看在平滑和降噪方面的功效!

原圖1                                                                           中值濾波之後

雜訊圖(5%)                                                                中值濾波後:

非常impressive的一點在這裡就可以看出來了, 很明顯中值濾波不僅是映像變得平滑,同時去除了椒鹽雜訊(映像最外圈的像素沒有去除掉只是因為我沒有從0-width處理而已)。從這裡中值的邏輯來看, 我們做中值操作的時候, 那麼白色(255)和黑色(0)因為是最大最小值, 除非周圍的顏色都是黑色或者白色,不然一般都會被剔除掉, 這就是和均值最大的不同! 所以在效果上要好很多。一般來說這個中值濾波是去除椒鹽雜訊的非常理想的選擇。

一樣的,最後還是貼一段我啟動並執行代碼:

/**<br />** method to remove noise from the corrupted image by median value<br />* @param corrupted input grayscale binary array with corrupted info<br />* @param smooth output data for smooth result, the memory need to be allocated outside of the function<br />* @param width width of the input grayscale image<br />* @param height height of the input grayscale image<br />*/<br />void medianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)<br />{</p><p>memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );<br />for (int j=1;j<height-1;j++)<br />{<br />for (int i=1;i<width-1;i++)<br />{<br />int k = 0;<br />unsigned char window[9];<br />for (int jj = j - 1; jj < j + 2; ++jj)<br />for (int ii = i - 1; ii < i + 2; ++ii)<br />window[k++] = corrupted[jj * width + ii];<br />// Order elements (only half of them)<br />for (int m = 0; m < 5; ++m)<br />{<br />int min = m;<br />for (int n = m + 1; n < 9; ++n)<br />if (window[n] < window[min])<br />min = n;<br />// Put found minimum element in its place<br />unsigned char temp = window[m];<br />window[m] = window[min];<br />window[min] = temp;<br />}<br />smooth[ j*width+i ] = window[4];<br />}<br />}<br />}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.