圖像銳化算法 C++ 實現
之前一段我們提到的算法都是和平滑有關, 經過平滑算法之後,
原圖 raw
減去模糊圖 blur
______________________________
等於 mask
這個時候, 我們發現,
老圖 raw:
+ 加上mask
______________________________
等於銳化圖 sharpen
怎麼樣, 是不是有了銳化的效果了??
這樣的話, 我們的銳化算法,也變得和之前的高斯平滑差不多了,
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/
http://www.
本文來自CSDN博客,轉載請標明出處:http://