bilinear interpolation algorithm and needing attention

Source: Internet
Author: User

Original Blog Address: http://handspeaker.iteye.com/blog/1545126

A bilinear interpolation algorithm was recently used in programming to scale the image. There is a lot of information on the Internet, the introduction is also clear. However, these articles only describe the algorithm, and do not specifically how to implement and how to achieve the best, for example, you can follow the online article of the algorithm to write a bilinear interpolation program, using it to process a picture, Then use MATLAB or OPENCV resize function to the same picture processing, the result is not the same, if the source picture is small, the effect gap is greater. The following is a brief explanation of bilinear interpolation and the above phenomenon:

1. 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 your corresponding coordinates are (2.5,4.5), then the nearest four pixels are (2,4), (2,5), (3,4), (3,5).

If the image is a grayscale image, then the gray value of the (I,J) point can be calculated by a formula:

F (i,j) =w1*p1+w2*p2+w3*p3+w4*p4;

where PI (i=1,2,3,4) is the nearest four pixels, WI (i=1,2,3,4) is the corresponding weighted value for each point. On the calculation of weights, written on Wikipedia and Baidu Encyclopedia is very clear.

2. Problems that exist

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

Instead of

int x=i*m/a

int y=j*n/b

Using the above formula, the correct bilinear interpolation results are obtained.


Summarize:

To sum up, I have got a few lessons.

1. Some of the information on the Internet is sometimes not reliable, and I still have to do more experiments.

2. Do not underestimate some simple, basic algorithms, so that you can write you may not write, and there may be some mysterious hidden.

3. To do more hands-on programming, more experience algorithms, look at the source of Daniel write (although sometimes very difficult, but to stick to see).

bilinear interpolation algorithm and needing attention

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.