Delphi Image Processing-surface blur

Source: Internet
Author: User

Reading Tips:

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

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

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

The code in this article must include the imagedata. Pas unit in "Delphi Image Processing-data type and public process.

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:

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

After deformation, you can:

2) mij = (2.5 TB-([Pij-P0])/2.5 TB

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 the surface of argb image data. // parameter: image data, blur radius, threshold procedure surfaceblur (VAR data: timagedata; radius, threshold: integer); const _ fc2_5: single = 2.5; var width, height, size: integer; srcoffset, dstoffset: integer; SRC: timagedata; begin if not (radius in [1 .. 100]) or not (threshold in [2 .. 255]) Then exit; if data. alphaflag then // if the image data contains Alpha information, convert it to pargb format argbconvertpargb (data); SRC: = _ getexpanddata (data, radius ); // obtain the backup image data after the extended radius. ASM push ESI push EDI push EBX mov eax, data Lea edX, Src call _ setcopyregs mov width, ECx mov height, EDX mov srcoffset, eax mov dstoffset, EBX mov eax, SRC. stride mov EBX, radius shl ebx, 1 Inc EBX mov size, ebx shl ebx, 2 neg EBX add EBX, eax add eax, 4 Mul radius pxor xmm7, xmm7 cvtsi2ss xmm6, threshold // xmm6 = 4 * (threshold * 2.5) movss xmm0, _ fc2_5 mulss xmm6, xmm0 pshufd xmm6, xmm6, 0 @ yloop: Push width @ xloop: push ESI mov edX, size pxor xmm0, xmm0 // total pixels pxor xmm5, xmm5 // nuclear movd xmm4, [ESI + eax] // xmm4 = p0 = 4 * Word (A, R, G, B) punpcklbw xmm4, xmm7 @ iloop: mov ECx, size @ jloop: movd xmm3, [esi] punpcklbw xmm3, xmm7 movaps xmm1, xmm3 movaps xmm2, xmm4 // calculate the element values of the fuzzy matrix psubw xmm1, xmm4 // xmm1 = Xij-x0 psubw xmm2, xmm3 // xmm2 = x0-Xij packuswb xmm1, xmm7 // remove the negative value packuswb xmm2 In the difference value of each component in xmm1, xmm7 // remove the negative por xmm2, xmm1 // xmm2 = | Xij-x0 | punpcklbw xmm2, xmm7 punpcklwd xmm2, xmm7 punpcklwd xmm3, xmm7 punpcklwd xmm3, xmm7 merge xmm3, xmm3 merge xmm2, xmm2 movaps xmm1, xmm6 subps xmm1, xmm2 divps merge, xmm6 // xmm1 = (xmm6-xmm2)/xmm6 maxmm1, xmm7 // remove the negative value mulps xmm3, xmm1 // Xij * = xmm1 // The cumulative value of the fuzzy matrix element and the product of the corresponding pixel value addps xmm5, xmm1 // nuclear + = xmm1 addps xmm0, xmm3 // total pixels + = Xij add ESI, 4 loop @ jloop add ESI, EBX dec edX jnz @ iloop divps xmm0, xmm5 // p0 = total pixels/= nuclear cvtps2dq xmm0, xmm0 packssdw xmm0, xmm7 packuswb xmm0, xmm7 movd [EDI], xmm0 pop ESI add ESI, 4 Add EDI, 4 Dec width jnz @ xloop pop width add ESI, srcoffset add EDI, dstoffset dec height jnz @ yloop pop EBX pop EDI pop ESI end; freeimagedata (SRC); If data. alphaflag then // if the image data contains Alpha information, restore it to the argb format pargbconvertargb (data); end;

Example running effect:

For details about the use of GDI + units and descriptions in the "Delphi image processing" series, see the article 《GDI + for VCL basics-GDI + and VCL.

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

Here, you can access "Delphi 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.