bilinear interpolation

bilinear interpolation

      Suppose the source image size is mxn and the destination image is axb. Then the side-length ratios of the two images are: m/a and n/b, respectively. Note that usually this scale is not an integer, and a floating point type is used when programming storage. The (i,j)th pixel (i row and j column) of the target image can be returned to the source image through the side length ratio. Its corresponding coordinates are (i*m/a, j*n/b). Obviously, the corresponding coordinates are generally not integers, and coordinates that are not integers cannot be used on discrete data such as images. Bilinear interpolation calculates the value (gray value or RGB value) of the point by finding the four closest pixels to the corresponding coordinate.

  If the image is a grayscale image, then the mathematical calculation model of the grayscale value of point (i, j) is:

f(x,y)=b1+b2x+b3y+b4xy

Where b1, b2, b3, b4 are the correlation coefficients. The calculation process for it is as follows:

      As shown in the figure, Q12, Q22, Q11, Q21 are known, but the point to be interpolated is point P, which requires bilinear interpolation. First, in the x-axis direction, the two points R1 and R2 are interpolated. This Very simple, then interpolate the P points according to R1 and R2, this is called bilinear interpolation.

clip_image001

Attached: Wikipedia -- bilinear interpolation:

      Bilinear interpolation, also known as bilinear interpolation. Mathematically , bilinear interpolation is a linear interpolation extension of an interpolation function with two variables . The core idea is to perform a linear interpolation in two directions respectively.

Suppose we want to get the value of the unknown function fat point P=\left( x, y\right), suppose we know the value of the function fat Q_{11} = \left( x_1, y_1 \right), Q_{12} = \left( x_1, y_2 \right), Q_{21} = \left( x_2, y_1 \right), and Q_{22} = \left( x_2, y_2 \right)four points.

First perform linear interpolation in the x direction to get

f(R_1) \approx \frac{x_2-x}{x_2-x_1} f(Q_{11}) + \frac{x-x_1}{x_2-x_1} f(Q_{21}) \quad\mbox{Where}\quad R_1 = (x,y_1),
f(R_2) \approx \frac{x_2-x}{x_2-x_1} f(Q_{12}) + \frac{x-x_1}{x_2-x_1} f(Q_{22}) \quad\mbox{Where}\quad R_2 = (x,y_2).

Then linearly interpolate in the y direction to get

f(P) \approx \frac{y_2-y}{y_2-y_1} f(R_1) + \frac{y-y_1}{y_2-y_1} f(R_2).

This gives the desired result f \left( x, y \right),

f(x,y) \approx \frac{f(Q_{11})}{(x_2-x_1)(y_2-y_1)} (x_2-x)(y_2-y) + \frac{f(Q_{21})}{(x_2-x_1)(y_2-y_1)} (x-x_1)(y_2-y)
+ \frac{f(Q_{12})}{(x_2-x_1)(y_2-y_1)} (x_2-x)(y-y_1) + \frac{f(Q_{22})}{(x_2- x_1)(y_2-y_1)} (x-x_1)(y-y_1).

If a coordinate system is chosen such that the coordinates fof the four known points of are (0, 0), (0, 1), (1, 0) and (1, 1), then the interpolation formula can be simplified as

f(x,y) \approx f(0,0) \, (1-x)(1-y) + f(1,0) \, x(1-y) + f(0,1) \, (1-x)y + f(1,1) xy.

Or expressed in matrix operations as

f(x,y) \approx \begin{bmatrix}1-x & x \end{bmatrix} \begin{bmatrix}f(0,0) & f(0,1) \\f(1,0) & f(1,1) \end{bmatrix} \begin{bmatrix}1-y \\y \end{bmatrix}

The result of this interpolation method is usually not linear, and the result of linear interpolation is independent of the order of interpolation. The interpolation in the y direction is performed first, and then the interpolation in the x direction is performed, and the result obtained is the same.

Bilinear interpolation in opencv and Matlab

   The premise of this part is that you already understand what bilinear interpolation is and that given the size of the source image and the target image, you can use the pen to calculate the value of a pixel in the target image. Of course, the best situation is that you have implemented the original or reprinted bilinear interpolation algorithm on a large number of blogs on the Internet in a certain language, and then found that the calculated results are the same as those obtained by the resize() function corresponding to matlab and openCV. totally different.

So what's going on with this?

In fact, the answer is very simple, it is the choice of the coordinate system, or the correspondence between the source image and the target image.

According to some blogs on the Internet, the origin (0, 0) of the source image and the target image is selected at the upper left corner, and then each pixel of the target image is calculated according to the interpolation formula. Suppose you need to reduce a 5x5 image to 3x3, then The correspondence between each pixel of the source image and the target image is as follows:

Only one line is drawn for illustration. It can be clearly seen from the figure that if the upper right corner is selected as the origin (0, 0), then the rightmost and bottommost pixels are not actually involved in the calculation, and the target image's The calculated gray value of each pixel is also left and upper relative to the source image.

So, how about adding 1 to the coordinates or choosing the bottom right corner as the origin? Unfortunately, it's still the same effect, but this time the resulting image will be skewed to the right and down.

The best way is that the geometric centers of the two images coincide, and each pixel of the target image is equally spaced and has a certain margin on both sides, which is also the practice of matlab and openCV. As shown below:

If you don't understand what I said above, it doesn't matter, just change to the following formula when calculating the corresponding coordinates,

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 result will be obtained

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077872&siteId=291194637