Linear interpolation and bilinear interpolation

Source: Internet
Author: User
recently learning the problem of rotation transformation in Digital image processing, it is found that after the rotation of the image there are some discontinuous points, so try to use bilinear interpolation method to solve. Here's how the interpolation works: linear interpolation

If you only work with detached data and want to know some of the values between the points of separation, you need to use some kind of interpolation. This is shown in the coordinates of figure 5-17. For some separated (integer) x values, you know the Y value. When x=2, you know y=10,x=3 when y=30. But you don't know the Y value when x=2.7.

Figure 1 Linear interpolation: an example of a simple routine

Using linear interpolation, you find the x=2.7 corresponding y value by connecting a segment of two points, as shown in Figure 1. Using linear interpolation, the x=2.7 corresponding y value is found by connecting a segment of two points. Linear interpolation always expresses x between 0 and 1, 0 corresponds to the minimum value of x (you know the corresponding Y value, in this case 2), and 1 corresponds to the maximum value of x (in this case, 3). In this example you want to find the Y value of x=2.7, the result is 0.7, which means "2 and 3 to 70%." ”

In the left figure of Figure 1, the 0% corresponds to the Y value 10,100% corresponds to the Y value of 20, so 70% corresponds to y=17. This is easy to see, but what is the picture on the right. 14 corresponds to 0.33, since it is 33% between 13 and 16. But the 33% between 35 and 46 is how much. Obviously, you want the code to calculate the results for you.

The first thing to do is to have the code find the corresponding values for 0 and 1. Starting with the X value, the minimum x value is subtracted first, so that the minimum value becomes 0. Then, scale the maximum value to 1, which you can do by dividing it by the difference between the maximum and the minimum value.

Here is the practice of Figure 1 left: 2.7→ (2.7-min)/(Max-min) = (2.7-2)/(3-2) =0.7

Then, do inverse to get the corresponding Y value: first scale this value (by multiplying the difference between the maximum and minimum values) and add to the minimum Y value:

0.7* (Maxy-min Y) +miny=0.7* (20-10) +10=0.7*10+10=17

Here you take the rules of the simple example of Figure 1 left, but you can use this method to calculate any linear interpolation. Take a look at Figure 1 the more difficult example on the right, in this case, you know that the x=13 corresponds to y=35,x=16 corresponds to y=46, but you want to know x=14 corresponds to the Y value. So, first get the corresponding value between 0 and 1:

14→ (14-minx)/(Maxx-minx) = (14-13)/(16-13) =0.33

When you know the corresponding value, you are ready to get the corresponding Y value:

0.33* (Maxy-miny) +miny=0.33* (46-35) +35=0.33*11+35=3.67+35=38.67

Finally, a floating-point calculation is required. Figure 5-17 on the right of the figure found x=14 corresponds to y=38.67. In fact, almost all interpolation calculations return a floating-point number. bilinear interpolation

You do not know the exact Y value for all (x,z) values between these independent vertices, so you need to interpolate. This time you need to get the values between 0 and 1, including X and Z.

With these values, you can calculate the exact y value in two steps. The X and z here represent the horizontal ordinate of an image, and the Z-value represents the pixel value of the picture under that coordinate.

Given any (x,z) coordinates, you need to find the exact y-grayscale value on the image. First use the previous formula to find the corresponding x and z values, you need to use two times:

int xlower = (int) Xcoord; 
int xhigher = xlower + 1; 
float xrelative = (xcoord-xlower)/((float) xhigher-(float) xlower); 
int zlower = (int) Zcoord; 
int zhigher = zlower + 1; 
float zrelative = (zcoord-zlower)/((float) zhigher-(float) zlower);

Each x and z integer value in the terrain you define a vertex, so you know the exact y value. So for each x floating-point number, you convert them to integers to get the smallest x value (for example, 2.7 becomes 2). Add this value to 1 to get the maximum x value (2.7 corresponds to 3 as the maximum value). Knowing the boundary, it is easy to find the corresponding value using the previous formula. Z-value is similar to the method of finding.

Knowing the corresponding value between 0 and 1, the next step is to find the exact y value. However, you first need to know the miny and Maxy values. These values represent the heights in the vertices. You need to know which triangle the point is in to know which vertex's height to use as the Y value.

You know the x and Z coordinates of the point P, so you know the four vertices around the point and it's easy to get their Y values:

float Heightlxlz = Heightdata[xlower, zlower]; 
float heightlxhz = Heightdata[xlower, zhigher]; 
float Heighthxlz = Heightdata[xhigher, zlower]; 

The lxhz represents the "low X-coordinate, high Z-coordinate" decision (X,Z).

The two triangles in which a point is used to draw the terrain. There are two ways to define the two triangles, as shown in Figure 5-18. The way the triangles are drawn affects the height of the P-point, as shown in the figure.

Figure 5-18 Two ways to draw two triangles from four vertices

Although the four vertices have the same coordinates, the points in both cases are not of the same height, and you can make a noticeable difference in the figure.

For the reasons I'm about to discuss, the better way is the right figure in figure 5-18.

Using this method of rotation, it is easy to determine which triangle the point is above. The boundary between the two triangles is given by a diagonal line. In the right image, if xrelative + zrelative is 1, this line corresponds to points with X and Z coordinates.

For example, if this point is in the center of four points, as shown in Figure 5-18, both xrelative and zrelative are 0.5f, so and for 1, the description is on the diagonal. If this point is biased to the left, the xrelative will be smaller and less than 1, which is similar to the z-coordinate. So if and less than 1, the (x,z) coordinates are inside the triangle in the lower-left corner; otherwise, the point is inside the triangle in the upper-right corner:

get Precise Heights

When you know the corresponding height, the height of the four surrounding vertices and the triangle in which the points are located, you can calculate the exact height.

If the point is in the lower-left triangle, then Pointabovelowertriangle is true, the following is the code that uses bilinear interpolation to get the height of any point of the triangle:

Finalheight = Heightlxlz; 
Finalheight + = zrelative * (HEIGHTLXHZ-HEIGHTLXLZ); 

Start with the Y value of the LOWESTX, based on the interpolation method explained earlier. Since this is a "double" interpolation, you should start with the Y value of Lowestxlowestz.

In a single interpolation, you add a height difference between maxy and multiply the corresponding x value. In the double interpolation, you multiply zrelative and xrelative.

In other words, starting at the height of the lower left vertex, you add the height difference between this vertex and the vertices with higher z-coordinates, and multiply the proximity of the z-coordinate from the second vertex. The last line of code is similar: for this height, you add the height difference between the lower left and right vertices, multiplied by the proximity of the x-coordinate of the lower-right vertex.

If the point is inside the upper-right triangle, then Pointabovelowertriangle is false and the situation is different, you need the following code:

Finalheight = heighthxhz; 
Finalheight + = (1.0f-zdifference) * (heighthxlz-heighthxhz); 

Starting from the height, start with the top right vertex and follow the same steps: Add the height difference and multiply the corresponding distance. Code

This method contains all the code explained earlier. This method returns the exact height of the point, whether it is an integer or a floating-point number, based on any (x,z) coordinates. First check whether the point is on the terrain. If not, returns the default height of 10.

public float Getexactheightat (float xcoord, float zcoord) {bool invalid = Xcoord < 0; 
    Invalid |= Zcoord < 0; 
    Invalid |= xcoord > heightdata.getlength (0)-1; 
    
    Invalid |= zcoord > heightdata.getlength (1)-1; 
    
    if (invalid) return 10; 
    int xlower = (int) Xcoord; 
    int xhigher = xlower + 1; 
    float xrelative = (xcoord-xlower)/((float) xhigher-(float) xlower); 
    int zlower = (int) Zcoord; 
    int zhigher = zlower + 1; 
    float zrelative = (zcoord-zlower)/((float) zhigher-(float) zlower); 
    float Heightlxlz = Heightdata[xlower, zlower]; 
    float heightlxhz = Heightdata[xlower, Zhigher]; 
    float Heighthxlz = Heightdata[xhigher, zlower]; 
    
    float heighthxhz = Heightdata[xhigher, Zhigher]; 
    BOOL Pointabovelowertriangle = (xrelative + zrelative < 1); 
    float Finalheight; 
        if (pointabovelowertriangle) {finalheight = Heightlxlz; Finalheight + = zrelative * (heightlxhz-Heightlxlz); 
    Finalheight + = xrelative * (HEIGHTHXLZ-HEIGHTLXLZ); 
        } else {finalheight = heighthxhz; 
        Finalheight + = (1.0f-zrelative) * (heighthxlz-heighthxhz); 
    Finalheight + = (1.0f-xrelative) * (heightlxhz-heighthxhz); } return finalheight;  }
In the OpenCV picture rotation, after the image rotation, there will be a lot of blank spots, these blank spots must be filled, otherwise the screen effect is not good. This operation is called interpolation processing.
Here is a calculation formula for the rotation, (x, y) indicates that after the rotation of the picture on the image, (× ', Y ') is the rotation of the money on the image of the corresponding point, the idea of bilinear interpolation is to reverse the rotation of the image to find the corresponding point in the original picture, but this may not coincide with the grid point value, that is, do not fall on the Therefore, it is necessary to transform the gray value of the non-grid point into the gray value of the mesh point by interpolation method, which has achieved a more smooth effect.
          
                              
We generally believe that the transformation process requires two independent algorithms when scaling the image, rotating the transformation, and so on:
An algorithm for gray level interpolation;
             

Digital image processing can only transform the values of coordinate grid points (discrete points). The new coordinate values generated by the coordinate transformation are often not coincident with the grid point values, so it is necessary to transform the gray value of the non-grid point into the gray value of the grid point by means of interpolation, which is called Gray interpolation. Common grayscale interpolation is:

1. Nearest neighbor interpolation Method 2. bilinear interpolation (first-order interpolation)

The nearest neighbor interpolation method is well understood, from who recently interpolated values. bilinear interpolation is interpolated using a grayscale value of four mesh points around (x, y):

But bilinear interpolation also has his shortcomings in the place: 1. The computational amount is large, but the image quality is high after zooming, and the image discontinuity is not seen. 2. With low-pass filter properties, so that the high-frequency components weakened, so that the contour of the image to a certain extent damaged. We compare the nearest neighbor interpolation to bilinear interpolation:

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.