Understanding super fast blur algorithm

后端 未结 3 750
情书的邮戳
情书的邮戳 2021-02-03 11:47

I\'m trying to understand the algorithm behind the super fast blur algorithm. Below is the port to java that works with android as a test. Looks like this version makes some opt

3条回答
  •  感动是毒
    2021-02-03 12:26

    Since I wrote that one I guess I can explain best :-)

     int dv[]=new int[256*div]; 
     for (i=0;i<256*div;i++){
         dv[i]=(i/div); 
    }
    

    This line precalculates a lookup table for all the possible mean values that can occur. This is to avoid costly division in the inner loop. On some systems doing the division directly instead of a doing an array lookup might actually be faster nowadays, but when I wrote it the lookup was the faster way.

    for(i=-radius;i<=radius;i++){
                int ind = yi+Math.min(wm,Math.max(i,0));
                p=pix[ind];
                rsum+=(p & 0xff0000)>>16;
                gsum+=(p & 0x00ff00)>>8;
                bsum+= p & 0x0000ff;
            }
    

    The reason why this algorithm is fast is that it uses a sliding window and thus reduces the number of required pixel lookups. The window slides from the left edge to the right (and in the second pass from top to bottom) and only adds one pixel at the right and removes one from the left. The code above initializes the window by prefilling the window with the leftmost edge pixel depending on the kernel size.

     if(y==0){
       vmin[x]=Math.min(x+radius+1,wm);
       vmax[x]=Math.max(x-radius,0);
      } 
    
      p1=pix[yw+vmin[x]];
      p2=pix[yw+vmax[x]]; 
    

    This is the part that adds a new pixel but at the same time handles the border conditions (when the window tries to read or remove pixels outside the bitmap).

     rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
      gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
      bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
    

    rsum, gsum and bsum is the accumulated sum of pixels inside the sliding window. What you see is the new pixel on the right side being added to the sum and the leftmost pixel i nthe window being removed from the sum.

提交回复
热议问题