Image enlargement and reduction (2) -- bilinear interpolation amplification and mean reduction

Source: Internet
Author: User
Overview

Disadvantages of image enlargement and reduction based on the "same distance sampling method" in the previous section. To improve the image, the local mean method can be used to reduce the image, and the bilinear interpolation method can be used to enlarge the image ".

The effect is as follows:

2048*1536 reduced to 100*80
100*80 to 600*400
Reduce image size by local mean (1) Calculate sampling interval

Set the source image size to w * H, and zoom in (down) to (K1 * W) * (K2 * h). The sampling interval is

II = 1/K1; JJ = 1/K2;

When k1 = k2, the proportional scaling is reduced; When K1! = K2 is scaled up or down in an unequal proportion; k1 <1 & K2 <1 is scaled up, k1 <1 & k2> 1 is scaled down from time to time

(2) obtain the local sub-block

Set the source image to f (x, y) (I = 1, 2 ,...... W; j = 1, 2 ,...... H). The scaled down image is g (x, y) (I = 1, 2 ,...... M; j = 1, 2 ,...... N, where M = W * K1, n = H * K2), the local sub-block of the original image is

F' (x, y) = f (II * I, JJ * j )...... F (II * I + II-1, JJ * j)

...... ......

F (II * I, JJ * j + jj-1 )...... F (II * I + II-1, JJ * j + jj-1)

(3) Find the reduced image

Mean of g (x, y) = f' (x, y)

Example:

Reduced Image

 

 

For example, G11 = (F11 + F12 + F21 + f22)/4

 

Algorithm source code (Java)

/*** Scale down the local mean image * @ Param IMG the image object to be scaled down * @ Param m scale down the image width * @ Param n scale down the image height * @ return processed image object */public static bufferedimage shrink (bufferedimage IMG, int M, int N) {float k1 = (float) m/IMG. getwidth (); float k2 = (float) N/IMG. getheight (); Return shrink (IMG, K1, K2 );} /*** reduce the image of the local mean * @ Param IMG the image object to be reduced * @ Param K1 the column ratio column to be reduced * @ Param K2 the row ratio column to be reduced * @ return returns processed image object */public static bufferedimage shrin K (bufferedimage IMG, float K1, float K2) {If (K1> 1 | k2> 1) {// If k1> 1 | k2> 1, the image is enlarged, not to narrow down the system. err. println ("this is shrink image funcation, please set k1 <= 1 and K2 <= 1! "); Return NULL;} float II = 1/K1; // The row spacing of the sample float JJ = 1/K2; // The column spacing of the sample int dd = (INT) (II * JJ); // int m = 0, n = 0; int imgtype = IMG. getType (); int W = IMG. getwidth (); int H = IMG. getheight (); int M = (INT) (K1 * w); int n = (INT) (K2 * H ); int [] pix = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); system. out. println (W + "*" + H); system. out. println (m + "*" + n); int [] newpix = new int [M * n]; for (Int J = 0; j <n; j ++) {for (INT I = 0; I <m; I ++) {int r = 0, G = 0, B = 0; colormodel CM = colormodel. getrgbdefault (); For (int K = 0; k <(INT) JJ; k ++) {for (int l = 0; L <(INT) II; l ++) {r = R + cm. getred (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]); G = G + cm. getgreen (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]); B = B + cm. getblue (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]) ;}} r = r/DD; G = g/DD; B = B/DD; newpix [J * m + I] = 255 <24 | r <16 | G <8 | B; // 255 <24 | r <16 | G <8 | B. The RGB color is saved in binary format in the memory, from right to left 1-8 bits represent blue, 9-16 represent green, 17-24 represent red // so "<24" "<16" "<8" represent shift left 24, 16 respectively, 8-bit // newpix [J * m + I] = new color (R, G, B ). getrgb () ;}} bufferedimage imgout = new bufferedimage (m, n, imgtype); imgout. setrgb (0, 0, M, N, newpix, 0, m); Return imgout ;}

 

/*** Scale down the local mean image * @ Param IMG the image object to be scaled down * @ Param m scale down the image width * @ Param n scale down the image height * @ return processed image object */public static bufferedimage shrink (bufferedimage IMG, int M, int N) {float k1 = (float) m/IMG. getwidth (); float k2 = (float) N/IMG. getheight (); Return shrink (IMG, K1, K2 );} /*** reduce the image of the local mean * @ Param IMG the image object to be reduced * @ Param K1 the column ratio column to be reduced * @ Param K2 the row ratio column to be reduced * @ return returns processed image object */public static bufferedimage shrin K (bufferedimage IMG, float K1, float K2) {If (K1> 1 | k2> 1) {// If k1> 1 | k2> 1, the image is enlarged, not to narrow down the system. err. println ("this is shrink image funcation, please set k1 <= 1 and K2 <= 1! "); Return NULL;} float II = 1/K1; // The row spacing of the sample float JJ = 1/K2; // The column spacing of the sample int dd = (INT) (II * JJ); // int m = 0, n = 0; int imgtype = IMG. getType (); int W = IMG. getwidth (); int H = IMG. getheight (); int M = (INT) (K1 * w); int n = (INT) (K2 * H ); int [] pix = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); system. out. println (W + "*" + H); system. out. println (m + "*" + n); int [] newpix = new int [M * n]; for (Int J = 0; j <n; j ++) {for (INT I = 0; I <m; I ++) {int r = 0, G = 0, B = 0; colormodel CM = colormodel. getrgbdefault (); For (int K = 0; k <(INT) JJ; k ++) {for (int l = 0; L <(INT) II; l ++) {r = R + cm. getred (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]); G = G + cm. getgreen (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]); B = B + cm. getblue (pix [(INT) (JJ * j + k) * w + (INT) (II * I + l)]) ;}} r = r/DD; G = g/DD; B = B/DD; newpix [J * m + I] = 255 <24 | r <16 | G <8 | B; // 255 <24 | r <16 | G <8 | B. The RGB color is saved in binary format in the memory, from right to left 1-8 bits represent blue, 9-16 represent green, 17-24 represent red // so "<24" "<16" "<8" represent shift left 24, 16 respectively, 8-bit // newpix [J * m + I] = new color (R, G, B ). getrgb () ;}} bufferedimage imgout = new bufferedimage (m, n, imgtype); imgout. setrgb (0, 0, M, N, newpix, 0, m); Return imgout ;}

Bilinear difference method for image placement

The coordinates of the four sub-blocks are set to (), (), and the coordinates of the processed pixels (C1, C2 ), 0 <C1 <1, 0 <Y <1. then, f (x, y) is obtained from top to bottom.


F (x, 0) = f () + C1 * (f ()-f ))

F (x, 1) = f (0, 1) + C1 * (f (1, 1)-f (0, 1) f (x, y) = f (x, 0) + C2 * F (f (x, 1)-f (x, 0 ))

For example, the pixel matrix of the source image is as follows.

Extend it to 2.5*1.2 times, and use bilinear interpolation. The filling vertex is as follows:

(1)

 

(2)

1 2 3 4 5 6 7 7

2 3 4 5 7 8 8

3 4 5 6 7 8 9 9

3 4 5 6 7 8 9 9

(3)

Algorithm source code (Java)

/*** Bilinear interpolation method: Image enlargement * @ Param IMG: image object to be reduced * @ Param K1: column to be reduced * @ Param K2: column to be reduced *@ return returns the processed image object */public static bufferedimage amplify (bufferedimage IMG, float K1, float K2) {If (k1 <1 | K2 <1) {// If k1 <1 | K2 <1, the image is reduced, not system. err. println ("this is shrink image funcation, please set k1 <= 1 and K2 <= 1! "); Return NULL;} float II = 1/K1; // The row spacing float JJ = (1/K2) of the sample; // the spacing of the sampled column int dd = (INT) (II * JJ); // int m = 0, n = 0; int imgtype = IMG. getType (); int W = IMG. getwidth (); // int H = IMG of the original image. getheight (); // int M = math. round (K1 * w); // int n = math. round (K2 * H); // The width int of the enlarged image [] pix = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W);/* system. out. println (W + "*" + H); system. out. println (m + "*" + n); */INT [] newpix = new int [M * n]; for (Int J = 0; j 

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.