Image scaling--bilinear interpolation algorithm

Source: Internet
Author: User

In mathematics, bilinear interpolation is a linear interpolation extension of an interpolation function with two variables, and its core idea is to perform a linear interpolation in two directions respectively. If you select a coordinate system so that the four known point coordinates are (0, 0), (0, 1), (1, 0), and (1, 1), then the interpolation formula can be reduced to:

The expression by matrix operation is:

The spatial transformation of images, also called geometric transformations or geometric operations, includes translation, rotation, image transformation, transpose, zooming, and so on. The spatial transformation can be represented as: set (U,V) as the point on the source image, (x, y) as the point on the target image, the spatial transform is the color value on the source image (U,V) corresponding to the color at the target image (x, y).

The image processed by the computer is a pointing pattern, that is, a pixel matrix is used to describe a pair of images. To give a simple image: 3x3 256-level grayscale, that is, 3 pixels high, Width is 3 pixels of the image, the value of each pixel can be 0-255, representing the brightness of the pixel, 255 is the brightest, that is, white, 0 is the darkest, that is, black.

The pixel matrix of the fake image is as follows: (In this matrix, the most commonly used coordinate systems in image processing are: X from left to right, starting from 0, y from top to bottom, and starting from 0)
234 38 22
67 44 12
89 65 63

If you want to enlarge this image to a 4x4 size, then the first step is to think of a 4x4 matrix first, and then the matrix is drawn, as shown below, of course, each pixel of the matrix is unknown, waiting for us to fill:

?        ?       ? ?
?        ?       ? ?
?        ?       ? ?
?        ?       ? ?
Then we have to fill in the empty matrix, the value to be filled out from where? is from the source map, good, first fill in the top left corner of the target map of the pixel, coordinates (0,0), then the coordinates of the corresponding source map can be derived from the following formula:
SRCX = DstX * (srcwidth/dstwidth), Srcy = Dsty * (srcheight/dstheight)
By applying a formula, you can find the coordinates of the corresponding original image (0* (3/4), 0* (3/4)) and (0*0.75,0*0.75) = (0,0)
When the corresponding coordinates of the source map are found, the 234 pixel values at coordinates (0,0) in the source map can be filled in the position of the target graph (0,0).

Next, look for the coordinates in the target graph (1,0) corresponding to the pixel in the source map, and apply the formula:
(1*0.75,0*0.75) = (0.75,0)
The results found that there is a decimal in the coordinates, what can I do? The image in the computer is a digital image, the pixel is the smallest unit, the pixel coordinates are integers, and there is never a decimal coordinate. At this time a strategy is to use the rounding method (also can use the method of directly drop the decimal place), the non-integer coordinates into an integer, good, then according to rounding the method to get the coordinates (1,0), the complete operation process is this:
(1*0.75,0*0.75) = > (0.75,0) = (1,0)
then you can fill in a pixel to the target matrix, as well as the coordinates in the source map (1,0) of the pixel value 38 into the target map coordinates.       
fill in each pixel sequentially, a magnified image is born, and the pixel matrix is as follows:
234    38     22&nbs P    22  
     12       12    
     65     63     63  
89     ;  65     63     63  

This magnified image method is called the nearest interpolation algorithm, which is one of the most basic and simplest image scaling algorithm, the effect is the most bad, the enlarged image has a very serious mosaic, reduced the image has a very serious distortion; the root cause of the poor effect is that its simple nearest interpolation method introduces serious image distortion, for example, When the coordinates of the target graph are reversed, the coordinates of the source graph are a floating-point number, using the rounding method, directly using the value of the nearest pixel to the floating-point, this method is very unscientific, when the coordinate value of 0.75 is pushed, should not be simply taken as 1, Since it is 0.75, 1 smaller than 0.25, than 0 to 0.75, then the target pixel value in fact should be based on the source map of the virtual point around the four real points to be calculated according to certain rules, so as to achieve a better zoom effect.

The bilinear interpolation algorithm is a good image scaling algorithm, which makes full use of the four real pixel values around the virtual point in the source graph to jointly determine a pixel value in the target graph, so the scaling effect is much better than the simple nearest neighbor interpolation, the computational amount is larger than the zero-order interpolation, but the image quality is high after zooming. There is no case of discontinuous pixel values.

Bilinear interpolation algorithms are described as follows:
For a destination pixel, set the coordinates by the inverse transformation of the floating point coordinates (I+U,J+V) (where I, J are the integer portion of floating-point coordinates, u, V is the fractional part of floating point coordinates, is the value of the [0,1) interval floating point number), then this pixel value f (i+u,j+v) can be from the original image coordinates (I,j), (I+1,j), (i,j+1), (i+1,j+1) corresponds to the value of the surrounding four pixels, namely:
F (i+u,j+v) = (1-u) (1-v) F (i,j) + (1-u) VF (i,j+1) + u (1-v) f (i+1,j) + UVF (i+1,j+1)
where F (i,j) represents the pixel value at the source image (I,j), and so on.

If the pixel coordinates of the target graph are (in), then the coordinates corresponding to the source map are (0.75, 0.75), which is actually just a conceptual virtual pixel, actually does not exist in the source map such a pixel, then the target map of the pixel (max) value can not be determined by this virtual image, It can only be determined by the four pixels of the source graph: (0,0) (0,1) (1,0), and because (0.75,0.75) is closer to (), then the decision to play a greater role, which is derived from the coefficients in equation 1 uv=0.75x 0.75 can be manifested, and (0.75,0.75) away from (0,0) farthest, so (0,0) The decision role will be smaller, the formula coefficient is (1-u) (1-v) =0.25x0.25 also reflects this feature;

The algorithm steps are detailed:

Suppose the original image size is SIZE=MXN, where M and n are the number of rows and columns of the original image, respectively. If the scaling factor of the image is T (t>0), the size of the target image is size=txmxtxn. For a pixel of the target image p (x, y) can be obtained by p*1/t the corresponding original image coordinates P ' (x1,y1), wherein x1=x/t,y1=y/t, because x1,y1 is not an integer so there is no such point, so that it can find the four points adjacent to the grayscale F1, F2, F3, F4, uses bilinear interpolation algorithm to get this pixel point P ' (x1,y1) grayscale, that is, pixel point P (x, y) grayscale.

A complete bilinear interpolation algorithm can be described as follows:

(1) The size of the new image is obtained by the original image and scale factor, and a new image is created.

(2) A pixel (x, y) of the new image is mapped to the original image (× ', "

(3) The X ', y ' rounding is obtained (XX,YY) and the values (XX,YY), (Xx+1,yy), (xx,yy+1), and (xx+1,yy+1) are obtained.

(4) Use bilinear interpolation to value the pixel point (x, y) and write back the new image.

(5) Repeat step (2) until all pixels of the new image are finished.

The core section code is as follows:

  1. //Function name bilinear
  2. //parameter float K
  3. //return value None
  4. //function using bilinear interpolation to achieve image scaling
  5. void Cchildview::bilinear (float k)
  6. {
  7. int nbpp=m_impicture. GETBPP ();
  8. int widthnew,heightnew; //width and height of new image
  9. float widthscale= (float) (1.0/k), heightscale= (float) (1.0/k);
  10. float Xx,yy;
  11. int A, b;
  12. int rr,gg,bb; //Save R, G, b components
  13. //Get the width and height of the new image
  14. widthnew= (int) (m_impicture. GetWidth () *k);
  15. Heightnew = (int) (m_impicture. GetHeight () *k);
  16. //Use the width and height of the new image to create a new image
  17. M_imnewpicture. Destroy ();
  18. M_imnewpicture. Create (Widthnew, Heightnew, NBPP);
  19. //Gets the number of bytes per line of the new and old images
  20. int npitch=m_impicture. GetPitch ();
  21. int npitchnew=m_imnewpicture. GetPitch ();
  22. //Get data pointers for new and old images
  23. Lpbyte pbitsnew= (LPBYTE) m_imnewpicture. GetBits ();
  24. Lpbyte pbits= (LPBYTE) m_impicture. GetBits ();
  25. if (M_IMPICTURE.GETBPP ()! =){
  26. MessageBox ("must be a 24-bit image or 8-bit image");
  27. M_imnewpicture. Destroy ();
  28. Invalidate ();
  29. return;
  30. }
  31. For (int x= (int) k;x<widthnew-k;x++) {
  32. For (int y= (int) k;y
  33. Xx=x*widthscale;
  34. Yy=y*heightscale;
  35. if (xx<=1e-8) {
  36. xx=0;
  37. }
  38. if (xx>m_impicture. GetWidth ()-2)
  39. xx= (float) (m_impicture. GetWidth ()-2);
  40. if (yy<=1e-8)
  41. yy=0;
  42. if (yy>m_impicture. GetHeight ()-2)
  43. yy= (float) (m_impicture. GetHeight ()-2);
  44. a= (int) xx;
  45. b= (int) yy;
  46. The R, G, b values of the corresponding pixels are obtained with bilinear interpolation, and the R, G, b values of the new pixels are
  47. int r11,r12,r21,r22;
  48. r11=* (pbits+b*npitch+3*a+2);
  49. r12=* (pbits+b*npitch+(A +1) +2);
  50. R21=* (pbits+ (b +1) *npitch+3*a+2);
  51. r22=* (b +1) *npitch+ pbits+ (a+1) +2);
  52. rr= (int) (r11* (A +1-xx) * (b +1-yy) +r12* (A +1-xx) * (yy-b)
  53. +r21* (xx-a) * (b +1-yy) +r22* (xx-a) * (Yy-b));
  54. int g11,g12,g21,g22;
  55. g11=* (pbits+b*npitch+3*a+1);
  56. g12=* (pbits+b*npitch+(A +1) +1);
  57. G21=* (pbits+ (b +1) *npitch+3*a+1);
  58. g22=* (b +1) *npitch+ pbits+ (a+1) +1);
  59. gg= (int) (g11* (A +1-xx) * (b +1-yy) +g12* (A +1-xx) * (yy-b)
  60. +g21* (xx-a) * (b +1-yy) +g22* (xx-a) * (Yy-b));
  61. int b11,b12,b21,b22;
  62. b11=* (pbits+b*npitch+3*a);
  63. b12=* (pbits+b*npitch+(A +1));
  64. B21=* (pbits+ (b +1) *npitch+3*a);
  65. b22=* (b +1) *npitch+ pbits+ (+1));
  66. bb= (int) (b11* (A +1-xx) * (b +1-yy) +b12* (A +1-xx) * (yy-b)
  67. +b21* (xx-a) * (b +1-yy) +b22* (xx-a) * (Yy-b));
  68. //The new R, G, and B values are written to the new image
  69. * (pbitsnew +y*npitchnew +x*3) =min (255,bb);
  70. * (pbitsnew +y*npitchnew +x*1) =min (255,gg);
  71. * (pbitsnew +y*npitchnew +x*2) =min (255,rr);
  72. }
  73. }
  74. M_impicture. Destroy ();
  75. Invalidate ();
  76. }

Reference Blog Material: http://blog.csdn.net/qiqi5521/article/details/2207562

Wikipedia Http://zh.wikipedia.org/wiki/%E5%8F%8C%E7%BA%BF%E6%80%A7%E6%8F%92%E5%80%BC

Image scaling--bilinear interpolation 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.