Algorithm encapsulation of bilinear and bicubic

Source: Internet
Author: User

By chuckgao 2009

Recently, we have studied bilinear and bicubic algorithms for image scaling during video stream playback. Here is an algorithm that doubles the image in the latest project. For algorithm optimization, you can consider using the method of creating a scaling ing table.

Bilinear algorithm:

Int bilinear_scale
(Long newx,
Long newy,
Unsigned long oldbytesperline,
Unsigned long newbytesperline,
Int format,
Unsigned char * newimage,
Image image)
{
Unsigned char * PTR;

If (newx = 0 | newy = 0 ){
Return size_error;
}

If (image. width = newx & image. Height = newy ){
Newimage = image. Bits;
Return not_trans;
}

Float XScale, yscale, FX, FY;
XScale = (float) 0.5;
Yscale = (float) 0.5;

Long ifx, ify, ifx1, ify1, xmax, Ymax;
Float ir1, ir2, ig1, ig2, ib1, ib2, dx, Dy;
Unsigned char R, G, B;
Scale_rgbquad rgb1, rgb2, rgb3, rgb4;
Xmax = image. Width-1;
Ymax = image. Width-1;
For (long y = 0; y <newy; y ++) {/* algorithm optimization by chuckgao: If the Image Height> width, modify to reduce the program for cross-layer computing */

FY = y * yscale;
Ify = (INT) FY;
Ify1 = min (Ymax, ify + 1 );
DY = FY-ify;
For (long x = 0; x <newx; X ++ ){
FX = x * XScale;
Ifx = (INT) FX;
Ifx1 = min (xmax, ifx + 1 );
DX = Fx-ifx;
// Interpolate using the four nearest pixels in the source


Unsigned char * PTR;
PTR = image. Bits + ify * oldbytesperline + ifx * format;
Rgb1.rgbred = * PTR ++;
Rgb1.rgbgreen = * PTR ++;
Rgb1.rgbblue = * PTR;
PTR = image. Bits + ify * oldbytesperline + ifx1 * format;
Rgb2.rgbred = * PTR ++;
Rgb2.rgbgreen = * PTR ++;
Rgb2.rgbblue = * PTR;
PTR = image. Bits + ify1 * oldbytesperline + ifx * format;
Rgb3.rgbred = * PTR ++;
Rgb3.rgbgreen = * PTR ++;
Rgb3.rgbblue = * PTR;
PTR = image. Bits + ify1 * oldbytesperline + ifx1 * format;
Rgb4.rgbred = * PTR ++;
Rgb4.rgbgreen = * PTR ++;
Rgb4.rgbblue = * PTR;

// Interplate in X direction:
Ir1 = rgb1.rgbred + (rgb3.rgbred-rgb1.rgbred) * dy;
Ig1 = rgb1.rgbgreen + (rgb3.rgbgreen-rgb1.rgbgreen) * dy;
Ib1 = rgb1.rgbblue + (rgb3.rgbblue-rgb1.rgbblue) * dy;
Ir2 = rgb2.rgbred + (rgb4.rgbred-rgb2.rgbred) * dy;
Ig2 = rgb2.rgbgreen + (rgb4.rgbgreen-rgb2.rgbgreen) * dy;
Ib2 = rgb2.rgbblue + (rgb4.rgbblue-rgb2.rgbblue) * dy;
// Interpolate in Y:
R = (unsigned char) (ir1 + (ir2-ir1) * dx );
G = (unsigned char) (ig1 + (ig2-ig1) * dx );
B = (unsigned char) (ib1 + (ib2-ib1) * dx );
// Set output

PTR = newimage + y * newbytesperline + x * format;


* PTR ++ = (unsigned char) R;
* PTR ++ = (unsigned char) g;
* PTR = (unsigned char) B;

 

}
}
Return 0;

}

 

 

Bicubic algorithm:

Int bicubic_scale
(Long newx,
Long newy,
Unsigned long oldbytesperline,
Unsigned long newbytesperline,
Int format,
Unsigned char * newimage,
Image image)
{
Unsigned char * PTR;

If (newx = 0 | newy = 0 ){
Return size_error;
}

If (image. width = newx & image. Height = newy ){
Return not_trans;
}

Float XScale, yscale, FX, FY;
XScale = (float) 0.5;
Yscale = (float) 0.5;

// Bicubic interpolation by chuckgao

Float f_x, f_y, A, B, RR, GG, BB, R1, R2;
Int I _x, I _y, XX, YY;
Scale_rgbquad RGB;

For (long y = 0; y <newy; y ++ ){

F_y = (float) y * yscale-0.5f;
I _y = (INT) floor (f_y );
A = f_y-(float) floor (f_y );
For (long x = 0; x <newx; X ++ ){
F_x = (float) x * XScale-0.5f;
I _x = (INT) floor (f_x );
B = f_x-(float) floor (f_x );

RR = Gg = BB = 0.0f;
For (int m =-1; m <3; m ++ ){
R1 = kernelbspline (float) m-);
YY = I _y + m;
If (yy <0) YY = 0;
If (yy> = image. Height) YY = image. Height-1;
For (INT n =-1; n <3; n ++ ){
R2 = R1 * kernelbspline (B-(float) N );
Xx = I _x + N;
If (XX <0) xx = 0;
If (XX> = image. width ){
Xx = image. Width-1;
}

PTR = image. Bits + YY * oldbytesperline + XX * format;
RGB. rgbred = * PTR ++;
RGB. rgbgreen = * PTR ++;
RGB. rgbblue = * PTR;

RR + = RGB. rgbred * R2;
Gg + = RGB. rgbgreen * R2;
BB + = RGB. rgbblue * R2;
}
}


PTR = newimage + y * newbytesperline + x * format;
* PTR ++ = (unsigned char) RR;
* PTR ++ = (unsigned char) GG;
* PTR = (unsigned char) BB;

}
}

Return 0;
}

 

Float kernelbspline (const float X)
{
If (x> 2.0f) return 0.0f;
// Thanks to Kristian kratzenstein
Float a, B, c, d;
Float xm1 = x-1.0f; // was calculatet anyway cause the "If (x-1.0f) <0 )"
Float xp1 = x + 1.0f;
Float xp2 = x + 2.0f;

If (xp2) <= 0.0f) A = 0.0f; else a = xp2 * xp2 * xp2; // only float, not float-> double-> float
If (xp1) <= 0.0f) B = 0.0f; else B = xp1 * xp1 * xp1;
If (x <= 0) C = 0.0f; else c = x * X;
If (xm1) <= 0.0f) d = 0.0f; else d = xm1 * xm1 * xm1;

Return (0.16666666666666666667f * (a-(4.0f * B) + (6.0f * C)-(4.0f * D )));

}

 

On Windows, I wrote an opencv video collection program with QT. the result of today's test is:

1. For 1280x960 x, use the bicubic algorithm, FPS = 1 or 2, bilinear algorithm, FPS is 6, and FPS is 12 for image playback without scaling

2. The expected display size is 320x240. the FPS is enhanced when the screen size is increased to X.

3. the FPS value here is only approximate, not absolute, and can only be compared.

 

 

(Supplement on April 1, September 22)

To a large extent, the image scaling algorithm is optimized at the C or even Assembly level for its operation speed. First, we need to optimize the C language. We generally use two methods:

1. Optimize the operation flow to minimize cross-layer computing

2. Change the time to space and sacrifice the memory space to speed up computing

 

On the premise that the optimization principles are clearly understood, we should also have a clear understanding of the following concepts:

1. Understanding of int, long, short (including unsigned), float, double, and other numerical types involved in the algorithm. You must be aware of the precision control after image scaling. For example, when assigning values to float data (assuming the variable is RGB:

Float RGB = 0.5 and float RGB = 0.5f differ in precision. The former is automatically converted to the double type, with 64-bit data while the latter being float 32-bit.

 

2. In 32-bit computers, the size of long and INT is generally equal, that is, 4 bytes and 32 bits. However, we should use sizeof (long) with caution when calculating, especially in malloc, to determine the size of sizeof (long ).

 

3. Aside from the algorithm itself, the computation of the Data Type of unsigned char is certainly faster than that of float, but the accuracy is definitely inferior to that of the latter. The trade-off in actual programming can be determined based on the Image Display Effect and speed.

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.