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:
- //Function name bilinear
- //parameter float K
- //return value None
- //function using bilinear interpolation to achieve image scaling
- void Cchildview::bilinear (float k)
- {
- int nbpp=m_impicture. GETBPP ();
- int widthnew,heightnew; //width and height of new image
- float widthscale= (float) (1.0/k), heightscale= (float) (1.0/k);
- float Xx,yy;
- int A, b;
- int rr,gg,bb; //Save R, G, b components
- //Get the width and height of the new image
- widthnew= (int) (m_impicture. GetWidth () *k);
- Heightnew = (int) (m_impicture. GetHeight () *k);
- //Use the width and height of the new image to create a new image
- M_imnewpicture. Destroy ();
- M_imnewpicture. Create (Widthnew, Heightnew, NBPP);
- //Gets the number of bytes per line of the new and old images
- int npitch=m_impicture. GetPitch ();
- int npitchnew=m_imnewpicture. GetPitch ();
- //Get data pointers for new and old images
- Lpbyte pbitsnew= (LPBYTE) m_imnewpicture. GetBits ();
- Lpbyte pbits= (LPBYTE) m_impicture. GetBits ();
- if (M_IMPICTURE.GETBPP ()! =){
- MessageBox ("must be a 24-bit image or 8-bit image");
- M_imnewpicture. Destroy ();
- Invalidate ();
- return;
- }
- For (int x= (int) k;x<widthnew-k;x++) {
- For (int y= (int) k;y
- Xx=x*widthscale;
- Yy=y*heightscale;
- if (xx<=1e-8) {
- xx=0;
- }
- if (xx>m_impicture. GetWidth ()-2)
- xx= (float) (m_impicture. GetWidth ()-2);
- if (yy<=1e-8)
- yy=0;
- if (yy>m_impicture. GetHeight ()-2)
- yy= (float) (m_impicture. GetHeight ()-2);
- a= (int) xx;
- b= (int) yy;
- 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
- int r11,r12,r21,r22;
- r11=* (pbits+b*npitch+3*a+2);
- r12=* (pbits+b*npitch+(A +1) +2);
- R21=* (pbits+ (b +1) *npitch+3*a+2);
- r22=* (b +1) *npitch+ pbits+ (a+1) +2);
- rr= (int) (r11* (A +1-xx) * (b +1-yy) +r12* (A +1-xx) * (yy-b)
- +r21* (xx-a) * (b +1-yy) +r22* (xx-a) * (Yy-b));
- int g11,g12,g21,g22;
- g11=* (pbits+b*npitch+3*a+1);
- g12=* (pbits+b*npitch+(A +1) +1);
- G21=* (pbits+ (b +1) *npitch+3*a+1);
- g22=* (b +1) *npitch+ pbits+ (a+1) +1);
- gg= (int) (g11* (A +1-xx) * (b +1-yy) +g12* (A +1-xx) * (yy-b)
- +g21* (xx-a) * (b +1-yy) +g22* (xx-a) * (Yy-b));
- int b11,b12,b21,b22;
- b11=* (pbits+b*npitch+3*a);
- b12=* (pbits+b*npitch+(A +1));
- B21=* (pbits+ (b +1) *npitch+3*a);
- b22=* (b +1) *npitch+ pbits+ (+1));
- bb= (int) (b11* (A +1-xx) * (b +1-yy) +b12* (A +1-xx) * (yy-b)
- +b21* (xx-a) * (b +1-yy) +b22* (xx-a) * (Yy-b));
- //The new R, G, and B values are written to the new image
- * (pbitsnew +y*npitchnew +x*3) =min (255,bb);
- * (pbitsnew +y*npitchnew +x*1) =min (255,gg);
- * (pbitsnew +y*npitchnew +x*2) =min (255,rr);
- }
- }
- M_impicture. Destroy ();
- Invalidate ();
- }
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