C ++ Image Processing-surface blur

Source: Internet
Author: User

Reading Tips:

The C ++ image processing series focuses on code clarity and readability, all using C ++ code.

Delphi Image ProcessingThe series focuses on efficiency. The general code is Pascal, and the core code is BaSm.

Make sure that the two items are consistent and can be compared with each other.

The code in this article must include "C ++ Image Processing-data types and common functions"The header file of BMP data. h in this article.

 

Image surface blur is a new function provided after Photoshop CS2. Its function is to blur the image surface while retaining the image edge. It is more effective than Gaussian blur in skin processing. Gaussian Blur blurs the skin of a person and blurts some edge features, such as the face's eyebrows and lips, so he has to use a mask to carefully erase the blurred parts of these places.

In terms of processing techniques, surface blur is also different from other Convolution Processing Methods. For example, Gaussian Blur uses a unified convolution matrix for image processing, surface blur means that each pixel has its own convolution matrix, and it is still 3 (4) sets, which correspond to the R, G, B (A, R, G, B) of pixels) component. Therefore, surface fuzzy processing is more complex and time-consuming than other convolution operations, because it needs to calculate its own convolution matrix for each pixel. The difficulty of surface fuzzy programming is also in the calculation of convolution matrix, and the others are the same as general image Convolution Processing.

Surface Blur has two parameters: blur radius and blur threshold. The former determines the Blur range and the latter determines the Blur degree. The Fuzzy range is the size of the convolution matrix. If the Blur radius is 1, the diameter of the fuzzy matrix is 1X2 + 1 equals 3, and the number of matrix elements is 3x3 equals 9, the intermediate element of the matrix is the current pixel.

The formula for calculating the matrix element value is:

Mij = 1-(| Pij-P0 |)/2.5 t

Among them, mij is the value of each element in the matrix, PIJ is the pixel component value corresponding to the matrix element, P0 is the pixel component value corresponding to the matrix center element, and T is the threshold value, | Pij-P0 | absolute difference between the pixel component value corresponding to the matrix element and the pixel component value corresponding to the central element. If mij is <0, mij = 0.

For argb image data, four sets of convolution matrices are required because there are four components.

After the matrix element is determined, it can be processed according to the general image convolution operation, that is, the product of the cumulative matrix element value and the corresponding pixel component value respectively, you can use the accumulated pixel component value divided by the accumulated element value to obtain the value of the current pixel component after fuzzy processing.

The following is the surface fuzzy processing code:

// Blur // argb image data surface blur processing // parameter: image data, blur radius (1-100), threshold (2-255) void surfaceblur (bitmapdata * data, uint radius, uint threshold) {If (radius <1 | radius> 100 | threshold <2 | threshold> 255) return; // set the float * matrixitems = new float [255*2 + 1]; float * items = & matrixitems [255]; float FV = threshold * 2.5; int I; for (I = 1; I <256; I ++) {items [-I] = items [I] = 1-I/FV; if (items [I] <0) break;} For (; I <256; I ++) items [-I] = items [I] = 0; * Items = 1; // obtain the border pixel extended image data to srcbitmapdata SRC; getexpenddata (data, radius, & SRC); // obtain the data processing parameters pargbquad PD, PS; uint width, height; int dstoffset, srcoffset; getdatacopyparams (data, & SRC, width, height, PD, PS, dstoffset, srcoffset); int size = (radius <1) + 1; int poffset = (SRC. stride> 2) + 1) * radius; int ioffset = (SRC. stride> 2)-size; float pixela, pixelr, pixelg, pixelb; float nucleus ara, nuclearr, nuclearg, nuclearb; float Iva, IVR, IVG, IVB; for (uint y = 0; y 

Surfaceblur uses floating-point numbers for Matrix Element Calculation and pixel fuzzy processing. To reduce the amount of computing for Fuzzy Matrix Elements in a loop, the function pre-Defines and calculates the matrixitems of the 255*2 + 1 Fuzzy Matrix Element table. The element pointer items points to the position of the matrixitems [255] element, the element value when this position is Pij-P0 = 0. If Pij-P0> 0, the corresponding element value is on the right of items. Otherwise, if Pij-P0 <0, the corresponding element value is on the left side of items, so that absolute value processing in | Pij-P0 | is removed.

To speed up operation, images without Alpha information may not process Alpha components. You can also change the processing function to a fixed point operation, but the calculation accuracy is somewhat lower.

The following is an example of using the surfaceblur function to blur the surface of the GDI + Bitmap (using bcb2010 ):

//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){Gdiplus::Bitmap *bmp =  new Gdiplus::Bitmap(L"..\\..\\media\\Source1.jpg");Gdiplus::Graphics *g = new Gdiplus::Graphics(Canvas->Handle);g->DrawImage(bmp, 0, 0);BitmapData data;LockBitmap(bmp, &data);SurfaceBlur(&data, 3, 10);UnlockBitmap(bmp, &data);g->DrawImage(bmp, data.Width + 8, 0);delete g;delete bmp;}//---------------------------------------------------------------------------

Example running effect:

Due to limited levels, errors are inevitable. Correction and guidance are welcome. Email Address:Maozefa@hotmail.com

Here, you can access "C ++ Image Processing-Article Index".

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.