The implementation of selective blur and its algorithm.

Source: Internet
Author: User

Our common fuzzy algorithm such as mean fuzzy, Gaussian blur and other basic process is to calculate a pixel periphery of a certain domain, the correlation pixel of a certain eigenvalues of the accumulation and corresponding weight, and then get the result value. For example, the weights of each pixel with the mean blur are the same, while the weights of the Gaussian blur and the distance of the pixels from the center point are Gaussian distributions. Such a process is unable to distinguish the edge of the image and other information, resulting in blurred image details seriously lost, a simple way to improve is to set a threshold, when the domain pixel and the center pixel gap is greater than the threshold value, set its weight is very small, even 0, so for itself relatively smooth area, And the original algorithm is not very different, but for the more obvious changes in the pixel value of the edge zone, it is able to effectively retain the original information, so as to reduce noise while preserving the edge of information.

In the actual processing, small radius of the field tend to deal with limited capacity, the results of careless processing, and with the increase in radius, the direct implementation of the algorithm time to increase the square relationship, the traditional optimization method due to the increase in the judging conditions, has been unable to continue to use, in order to solve the speed problem, We can use the optimization based on the histogram algorithm, if the histogram information in the field can be counted, the above judgment conditions and weight calculation can be easily implemented by the following code:

voidCalc (unsigned Short*hist,intIntensity, unsignedChar*&pixel,intThreshold) {    intK, Low, high, Sum =0, Weight =0; Low= Intensity-threshold; High = Intensity +Threshold; if(Low <0) Low =0; if(High >255) High =255;
for(K = low; K <= High; k++) {Sum+ = hist[k] *K; Weight+=Hist[k]; } if(Weight! =0) *pixel = Sum/Weight;}

Notice the cross-over judgment before for.

In the framework of the fast implementation of arbitrary RADIUS local histogram algorithm in PC, we have realized the acquisition of histogram information with arbitrary radius and constant time, so the execution time of the algorithm is only related to the loop amount in the For loop, that is, depends on the threshold parameter, when the threshold obtains the bigger, The final effect is closer to the standard fuzzy algorithm (the above code is close to mean fuzzy), and in the actual meaningful algorithm application and only threshold tend to get smaller to have the meaning of margin, therefore, the computational amount can be moderately controlled.

If you want to implement selective Gaussian blur, multiply a factor in the weight item in the For loop, which adds a certain amount of computation.

We have selected a number of other edge-preserving filter test images have been tested, in effect by adjusting the parameters can be quite good results, such as:

Original result diagram: Parameter R =10, Threshold = 16

Original result diagram: Parameter R =10, Threshold = 16

Original result diagram: Parameter R =10, Threshold = 16

Original result diagram: Parameter R =10, Threshold = 40

In the processing time, using the above parameters, on the I3 notebook computer test, a 1024x768 color image using time of about 250ms, if the use of YUV color space to deal with only the Y component, the speed can be increased to 100ms, in the results, the same parameters of the surface fuzzy comparison, Looks similar, but is nearly 3 times times faster than the surface blur.

Attach the main code of the engineering function:

/// <summary>     ///achieve image selective image blur effect, O (1) complexity, latest finishing time 2015.8.1. /// </summary>/// <param name= "SRC" >The data structure of the source image that needs to be processed. </param>/// <param name= "Dest" >saves the data structure of the processed image. </param>/// <param name= "Radius" >Specifies the size of the fuzzy sampling area, valid range [1,127]. </param>/// <param name= "Threshold" >The option controls how much the tonal value of adjacent pixels differs from the center pixel value in order to become part of the blur, and pixels with a tonal difference less than the threshold are excluded from the blur, valid range [1,255]. </param>Is_ret __stdcall Selectiveblur (Tmatrix *src, Tmatrix *dest,intRadius,intThreshold, EdgeMode Edge) {    if(SRC = = NULL | | Dest = = NULL)returnis_ret_err_nullreference; if(Src->data = = NULL | | Dest->data = = NULL)returnis_ret_err_nullreference; if(src->width! = Dest->width | | Src->height! = Dest->height | | Src->channel! = Dest->channel | | Src->depth! = Dest->depth | | Src->widthstep! = dest->widthstep)returnIs_ret_err_paramismatch; if(src->depth! = is_depth_8u | | Dest->depth! = is_depth_8u)returnis_ret_err_notsupported; if(Radius <0|| Radius >=127|| Threshold <2|| Threshold >255)returnIs_ret_err_argumentoutofrange; Is_ret RET=IS_RET_OK; if(Src->data = = dest->Data) {Tmatrix*clone =NULL; Ret= Is_clonematrix (SRC, &Clone); if(Ret! = IS_RET_OK)returnRet; Ret=Selectiveblur (Clone, Dest, Radius, Threshold, Edge); Is_freematrix (&Clone); returnRet; }    if(Src->channel = =1) {Tmatrix*row = NULL, *col =NULL; unsignedChar*lineps, *LINEPD; intX, Y, K, Width = src->width, Height = src->Height; int*rowoffset, *Coloffset; unsigned Short*colhist = (unsigned Short*) Is_allocmemory ( the* (Width +2* Radius) *sizeof(unsigned Short),true); if(colhist = = NULL) {Ret = is_ret_err_outofmemory;GotoDone8;} unsigned Short*hist = (unsigned Short*) Is_allocmemory ( the*sizeof(unsigned Short),true); if(Hist = = NULL) {Ret = is_ret_err_outofmemory;GotoDone8;} Ret= Getvalidcoordinate (Width, Height, radius, radius, radius, radius, Edge, &row, &col);//Get coordinate offset        if(Ret! = IS_RET_OK)GotoDone8; Colhist+ = Radius * the; RowOffset = ((int*) row->data) + Radius; Coloffset = ((int*) col->data) + Radius;//To be offset for operation         for(Y =0; Y < Height; y++)        {            if(Y = =0)//column histogram of the first row, to be calculated from the start            {                 for(K =-radius; K <= Radius; k++) {lineps= Src->data + coloffset[k] * src->Widthstep;  for(X =-radius; X < Width + Radius; X + +) {colhist[x* the+ lineps[rowoffset[x]]]++; }                }            }            Else                                                //column histogram for other rows, update on it.{lineps= Src->data + Coloffset[y-radius-1] * src->Widthstep;  for(X =-radius; X < Width + Radius; X + +)//Delete the histogram data for the row in the range that is moved out{colhist[x* the+ lineps[rowoffset[x]]]--; } lineps= Src->data + coloffset[y + Radius] * src->Widthstep;  for(X =-radius; X < Width + Radius; X + +)//increase the histogram data for the row within the range{colhist[x* the+ lineps[rowoffset[x]]]++; }} memset (Hist,0, the*sizeof(unsigned Short));//histogram data for each row clear 0 firstlineps= Src->data + Y * src->Widthstep; LINEPD= Dest->data + Y * dest->Widthstep;  for(X =0; X < Width; X + +)            {                if(X = =0)                {                     for(K =-radius; K <= Radius; k++)//line first pixel, need to recalculateHistgramaddshort (colhist + K * the, Hist); }                Else{histgramsubaddshort (colhist+ Rowoffset[x-radius-1] * the, colhist + rowoffset[x + Radius] * the, Hist);//other pixels in the row, delete and increment in sequence.} Calc (Hist, lineps[0], LINEPD, Threshold); Lineps++; LINEPD++; }} colhist-= Radius * the;//Resume offset OperationDone8:is_freematrix (&Row); Is_freematrix (&Col);        Is_freememory (colhist);        Is_freememory (Hist); returnRet; }    Else{Tmatrix*blue = null, *green = NULL, *red = NULL, *alpha = NULL;//because C variables are not initialized, their values are random values, which can cause errors at the time of release. Is_ret RET = Splitrgba (SRC, &blue, &green, &red, &Alpha); if(Ret! = IS_RET_OK)GotoDone24; Ret=Selectiveblur (blue, Blue, Radius, Threshold, Edge); if(Ret! = IS_RET_OK)GotoDone24; Ret=Selectiveblur (green, Green, Radius, Threshold, Edge); if(Ret! = IS_RET_OK)GotoDone24; Ret=Selectiveblur (red, red, Radius, Threshold, Edge); if(Ret! = IS_RET_OK)GotoDone24;//32-bit alpha does not do any processing, in fact, the 32-bit correlation algorithm is basically non-channel processingCopyalphachannel (SRC, Dest); Ret=Combinergba (Dest, Blue, Green, Red, Alpha); Done24:is_freematrix (&Blue); Is_freematrix (&Green); Is_freematrix (&Red); Is_freematrix (&Alpha); returnRet; }}

Test source code and Engineering (VS2010 development): Selectiveblur.rar

LAVIEWPBT Time: 2015.8.1 contact qq:33184777 Reprint Please keep our information **********************

The implementation of selective blur and its algorithm.

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.