Recently in the video splicing project, which used the image of the single-matrix transformation, in the final image remapping, because the target image coordinates are non-integers, so need to use the interpolation method, with bilinear interpolation, the following post is mainly to view the blog of the predecessor of the bilinear interpolation algorithm principle of a summary, Thank you for some of Daniel's blog posts here.
Http://www.cnblogs.com/linkr/p/3630902.html
Http://www.cnblogs.com/funny-world/p/3162003.html
bilinear interpolation
assuming the source image size is MXN, the target image is AXB. Then the proportionments of the two images are: m/a and n/b respectively. Note that this ratio is usually not an integer and is used in floating-point programming when storing. The pixel (i,j) pixels (Row J column) of the target image can be mapped back to the source image by the edge length ratio. Its corresponding coordinates are (i*m/a,j*n/b). Obviously, this correspondence coordinate is generally not an integer, and the coordinates of non-integers cannot be used on the discrete data of the image. Bilinear interpolation calculates the value of the point (grayscale value or RGB value) by finding the nearest four pixels from the corresponding coordinate.
If the image is a grayscale image, then the mathematical model for the gray value of the (I,J) point is:
f (x, y) =b1+b2x+b3y+b4xy
Where b1,b2,b3,b4 is the coefficient of correlation. The calculation process for it is as follows:
, known q12,q22,q11,q21, but to Interpolate point is P point, this will be used bilinear interpolation, first in the x-axis direction, the R1 and R2 two points interpolation, this is very simple, and then according to R1 and R2 to interpolate p points, this is called bilinear interpolation.
Attached: Wikipedia-bilinear interpolation:
Bilinear interpolation, also known as bilinear interpolating . 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 we want the value of the unknown function at the point, suppose we know the value of the function,,, and four points.
First, linear interpolation in the x direction is performed to obtain
And then linear interpolation in the y direction to get
So you get the results you want,
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 simplified to
Or it is represented by a matrix operation as
The results of this interpolation method are usually not linear, and the results of linear interpolation are independent of the order of interpolation. First, the y -direction interpolation, and then the x -direction interpolation, the result is the same.
bilinear interpolation in OpenCV and matlab
The premise of this is that you already know what bilinear interpolation is and that, given the source image and the target image size, you can use the pen to calculate the value of a pixel in the target image. Of course, the best thing is that you have implemented a large number of online blogs on the internet, the original or reproduced bilinear interpolation algorithm, and then found that the results of MATLAB and OPENCV corresponding to the resize () function results are completely different.
What the hell is going on here?
In fact, the answer is very simple, is the choice of coordinate system, or the source image and the target image of the corresponding problem.
According to some blog on the Internet, the source image and the target image origin (0,0) are selected in the upper left corner, and then based on the interpolation formula to calculate the target image per pixel, assuming you need to reduce a 5x5 image to 3x3, then the corresponding relationship between the source image and the target image of each pixel is as follows:
Only draw a line, used as a schematic, it is obvious to see, if the upper right corner is selected as the origin (0,0), then the rightmost and the bottom of the pixel is not actually involved in the calculation, and the target image of each pixel point computed gray value is also relative to the left side of the source image.
So, how about adding 1 to the coordinates or choosing the lower-right corner as the origin? Unfortunately, the same effect, but this time the image will be on the lower right.
The best approach is that the geometric center of the two images is coincident, and the target image is spaced between each pixel, and there is a margin between the two sides, which is also the practice of MATLAB and OPENCV. Such as:
If you do not understand what I said above, it is OK, as long as the corresponding coordinates in the calculation of the following formula can be changed,
int x= (i+0.5) *m/a-0.5
int y= (j+0.5) *n/b-0.5
Replace
int x=i*m/a
int y=j*n/b
Using the above formula, the correct bilinear interpolation results are obtained.
A detailed summary of bilinear interpolation algorithm