Abstract: Chapter 1 Image Enhancement in Visual C ++ digital image processing, this chapter discusses in detail the smoothing and sharpening of the airspace in image enhancement. This section describes gradient sharpening.
- Labels: Visual C ++ image processing visual C ++ Digital Image Processing
5.5 image sharpening
Sharpening filtering can weaken or eliminate low-frequency components in an image, but does not affect high-frequency components. Because the low frequency component corresponds to the area where the gray value of the image changes slowly, it is related to the overall characteristics of the image, such as the overall contrast and average gray value. Sharpening filtering filters these components to increase the image contrast and increase the edge. In practical applications, sharpen filtering can be used to enhance blurred details or target edges of Low-contrast images.
There are two main purposes of image sharpening: one is to enhance the image edge, make the blurred image clearer, the color becomes highlighted, and the image quality is improved, produce images that are more suitable for human observation and recognition. Second, the target object has a clear edge after sharpening, it can be used to extract the edge of the target, divide the image, identify the target area, and extract the region shape, laying the foundation for further image understanding and analysis. There are two ways to sharpen an image: one is the micromethod and the other is the high-pass filtering method. The working principle of Qualcomm filtering is similar to that of low-pass filtering. The following describes two common differential sharpening methods: gradient sharpening and Laplace sharpening. However, as the sharpening improves the noise to a higher degree than the signal, the image to be sharpened must have a higher signal-to-noise ratio. Otherwise, the image to be sharpened has a lower signal-to-noise ratio.
5.5.1 gradient sharpening (1)
1. Basic Theory
The neighborhood or weighted average method can smooth the image, and the corresponding differential method can sharpen the image. The differential operation is used to calculate the signal change rate, which strengthens the high-frequency components, so that the image contour is clear.
Because the essence of Image Blur is that the image is subject to the average or integral operation, in order to clear the edges and blurred outlines that stretch in any direction in the image, you can perform inverse operations on the image, such as differential operations, to make the image clearer.
In image processing, first-order differentiation is implemented by gradient method. Functions for an imageF (x, F)Meaning, definitionF (x, F)In point(X, F)The gradient is a vector defined:
The gradient direction is in the functionF (x, F)In the direction of the maximum change rate, the gradient AmplitudeG [F (x, f)]It can be calculated from the following formula:
We can see from the above formula that the gradient value isF (x, F)The increase in the unit distance in the direction of the maximum change rate. For digital images, the difference is similar to the difference. Formula (5-6) the gradient expression after the difference calculation is:
To facilitate programming and increase the computing speed, the absolute difference algorithm can be used to approximate the computing accuracy as follows:
This gradient method is also known as the horizontal vertical difference method, and another gradient method is cross-ground Differential Calculation, known as the Robert gradient method, expressed:
Similarly, the absolute difference algorithm can be used to approximate the following:
When the last or last row of an image cannot calculate the gradient of a pixel, the gradient values of the previous row or column are generally replaced by the gradient values of the previous one.
To enhance the edge without damaging the image background, you can also use the gradient value instead of the gray value to improve the method, that is, use the threshold judgment to improve the gradient sharpening method. The specific formula is as follows:
You can use formula (5-8) or formula (5-9 ). For an image, the gradient changes between the object and the object, and between the background and the background are very small. The gray-scale changes are usually concentrated on the edge of the image, that is, the place where objects and backgrounds are handed over. When we set a threshold value, the pixel is considered to be at the edge of the image when the threshold value is greater than the threshold value, and the constant C is added to the result to make the edge brighter; if the pixel value is not greater than the threshold value, it is considered to be a similar pixel, that is, an object or background. The constant C can be selected based on the specific image features. In this way, the boundary of the object is brightened, and the original state of the image background is retained. This method has better enhancement effect and applicability than the traditional gradient sharpening method.
2. Algorithm Implementation
The member function gradsharp () in the cimgenhance class implements the gradient sharpening operation. This algorithm is programmed based on the gradient sharpening method determined by the threshold. The threshold is set to 20. If the gradient is increased by 100 or more than 255, set it to 255. You can directly call the gradsharp () function when you perform gradient sharpening on a gray image. The following is the code implementation of the gradsharp () function.
/********************************** *************************************** * Function Name: * Gradsharp () * Parameters: * Byte threshold-Threshold Value * Return value: * If bool is successful, true is returned. Otherwise, false is returned. * Description: This function is used to perform gradient sharpening on the image. * /************************************ ************************************/ Void cimgenhance: gradsharp (unsigned char threshold) { Unsigned char * psrc, * pdst, * psrc1, * psrc2; Long I, J; // cyclic variable Int btemp; If (m_pimgdataout! = NULL) { Delete [] m_pimgdataout; M_pimgdataout = NULL; } Int linebyte = (m_imgwidth * M_nbitcount/8 + 3)/4*4;
If (m_nbitcount! = 8) { Afxmessagebox ("only 8 gray images can be processed! "); Return; } // Create the image area to be copied M_nbitcountout = m_nbitcount; Int linebyteout = (m_imgwidth * M_nbitcountout/8 + 3)/4*4; If (! M_pimgdataout) { M_pimgdataout = new unsigned char [Linebyteout * m_imgheight]; }
Int pixelbyte = m_nbitcountout/8; For (I = 0; I <m_imgheight; I ++ ){ For (j = 0; j <m_imgwidth * pixelbyte; j ++) * (M_pimgdataout + I * linebyteout + J) = * (M_pimgdata + I * linebyteout + J ); }
For (I = 0; I <m_imgheight; I ++) // each row { For (j = 0; j <m_imgwidth; j ++) // each column { // Pointer to column J of the new Dib row I Pdst = m_pimgdataout + linebyte * (M_imgheight-1-I) + J;
// Perform gradient operations // Pointer to column J of row I of Dib Psrc = (unsigned char *) m_pimgdata + Linebyte * (m_imgheight-1-I) + J; // Pointer to the pixel in column J of the dib I + 1 Psrc1 = (unsigned char *) m_pimgdata + Linebyte * (m_imgheight-2-I) + J; // Pointer to the pixel in column J + 1 of row I of Dib Psrc2 = (unsigned char *) m_pimgdata + Linebyte * (m_imgheight-1-I) + J + 1; Btemp = ABS (* psrc)-(* psrc1) + ABS (* psrc)-(* psrc2 ));
// Determine whether the value is smaller than the threshold If (btemp + 120) <255) { // Determine whether the value is greater than the threshold. If the value is smaller than the threshold, the gray value remains unchanged. If (btemp> = threshold) { * Psrc = (btemp + 120 ); } } Else { * Psrc = 255; } // Generate a new Dib pixel value * Pdst = * psrc; } } } |
3. function call
Ongradesharp () is the processing function mapped to the "gradient sharpening" event in the View class cdemoview (). The following is the code implementation of the ongradesharp () function.
Void cdemoview: ongradesharp () { Cdemodoc * pdoc = getdocument (); Imgcenterdib * pdib = pdoc-> getpdib ();
If (pdib-> m_nbitcount! = 8 &{ : MessageBox (0, "Only grayscale images processed", mb_ OK, 0 ); Return; }
Cimgenhance imgnoise (pdib-> getdimensions (), Pdib-> m_nbitcount, Pdib-> m_lpcolortable, pdib-> m_pimgdata ); Unsigned char bthre = 30; Imgnoise. gradsharp (bthre ); Cmainframe * pframe = (cmainframe *) (afxgetapp ()-> m_pmainwnd ); Pframe-> sendmessage (wm_command, id_file_new ); Cdemoview * pview = (cdemoview *) pframe-> mdigetactive ()-> getactiveview (); Cdemodoc * pdocnew = pview-> getdocument (); Imgcenterdib * dibnew = pdocnew-> getpdib (); Dibnew-> replacedib (imgnoise. getdimensions (), imgnoise. m_nbitcountout, imgnoise. m_lpcolortable, imgnoise. m_pimgdataout ); Pdocnew-> setmodifiedflag (true ); Pdocnew-> updateallviews (pview ); Invalidate (); } |