圖像銳化算法 C++ 實現

http://blog.csdn.net/cay22/article/details/5547094 

圖像銳化算法 C++ 實現

之前一段我們提到的算法都是和平滑有關, 經過平滑算法之後, 圖像銳度降低, 降低到一定程度, 就變成了模糊。 今天我們反其道行之, 我們看看銳化是怎麼做的。 這裡的銳化, 還是的從平滑談開去。我們先來觀察原來的圖像和平滑圖像的區別:

原圖 raw

                                        

減去模糊圖 blur

                                        

 

_________________________________________________________

 

等於 mask

                                        

 

 

這個時候, 我們發現,減法做完的這個圖赫然勾勒出了原圖的邊緣!! 這樣給我們一個啟示就是, 如果我們把這個mask加到原圖上那豈不就是銳化了? (不明白? 銳化的意思就是邊緣的色差比較大, 產生的圖片貌似清晰的效果) 說干就干, 馬上我們來做個新的算式:

 

老圖 raw:

                                     

+ 加上mask

 

                                     

_______________________________________________________

等於銳化圖  sharpen

                                     

 

怎麼樣, 是不是有了銳化的效果了??所以我們實際上的銳化效果就是從這麼簡單的想法衍生出來的。 所以銳化的公式可以簡單的表述為 sharp = raw + ( raw-blur ); 再來看看我們原來的高斯模版的話就是這樣:

 

 

這樣的話, 我們的銳化算法,也變得和之前的高斯平滑差不多了, 就是像素的加權平均值的計算就可以得到了。可以想見的事情是代碼肯定也會出奇的一致! 這是那個template改掉了:

 

view plaincopy to clipboardprint?

/** 

** method to remove sharp the raw image with unsharp mask 

* @param gray input grayscale binary array  

* @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 sharpenImage  (unsigned char* gray, unsigned char* smooth, int width, int height)  

{  

      

    int templates[25] = { -1, -4, -7, -4, -1,   

        -4, -16, -26, -16, -4,   

        -7, -26, 505, -26, -7,  

        -4, -16, -26, -16, -4,   

        -1, -4, -7, -4, -1 };         

    memcpy ( smooth, gray, width*height*sizeof(unsigned char) );  

    for (int j=2;j<height-2;j++)  

    {  

        for (int i=2;i<width-2;i++)  

        {  

            int sum = 0;  

            int index = 0;  

            for ( int m=j-2; m<j+3; m++)  

            {  

                for (int n=i-2; n<i+3; n++)  

                {  

                    sum += gray [ m*width + n] * templates[index++] ;  

                }  

            }  

            sum /= 273;  

            if (sum > 255)  

                sum = 255;  

            if (sum <0)  

                sum = 0;  

            smooth [ j*width+i ] = sum;  

        }  

    }  

 

當然, 這個銳化算法或者說銳化的模板只是我根據前面的算式自己計算的來的,其實還是有非常主流的銳化模版可以供使用的, 比如說著名的拉普拉斯算子. 點開這些類似的網頁你也可以獲取一些有用的信息:

 

http://www.cgafaq.info/wiki/Image_Sharpening_and_Blurring

http://www.spatialanalysisonline.com/output/html/Linearspatialfiltering.html

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/hhygcy/archive/2009/07/08/4330939.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值