Image sharpening algorithm C ++ implementation

Source: Internet
Author: User

Image sharpening algorithm C ++ implementation

The algorithms we mentioned in the previous section are related to smooth. After smooth algorithms, the image sharpness is reduced to a certain extent and then blurred. Today, let's look at how sharpening works. The sharpening here is still smooth. Let's first observe the differences between the original image and the smooth image:

Source image raw

Subtract the Blur Image Blur

 

_________________________________________________________

 

Equal to mask

 

 

At this time, we found that the image after subtraction outlines the edge of the source image !! This provides us with an inspiration: If we add this mask to the source image, isn't it sharpening? (Do not understand? Sharpening means that the color difference between the edges is relatively large, and the generated images seem to have a clear effect.) do whatever you like. Let's create a new formula:

 

Old figure raw:

+ Add mask

 

_______________________________________________________

Equal to sharpen

 

How is it? Does it have a sharpening effect ?? So our actually sharpening effect is derived from such a simple idea. Therefore, the sharpen formula can be simply expressed as sharp = raw + (raw-blur). Let's look at our original Gaussian template as follows:

 

 

In this way, our sharpening algorithm is almost the same as the previous Gaussian smoothing, that is, the calculation of the weighted average of pixels can be obtained. What you can imagine is that the Code will surely be surprisingly consistent! This is the 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 of the input grayscale image

* @ Param 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

{

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 & gt; 255)

Sums = 255;

If (sum <0)

Sum = 0;

Smooth [J * width + I] = sum;

}

}

}

 

Of course, this sharpen algorithm or sharpen template is only calculated by myself based on the previous formula. In fact, there are still very mainstream sharpen templates available for use, such as the famous Laplace operator. you can also obtain useful information by clicking these similar web pages:

 

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

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

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hhygcy/archive/2009/07/08/4330939.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.