[Reprint] bilinear interpolation algorithm for image scaling and performance optimization

Source: Internet
Author: User

Original address: Dual linear interpolation algorithm for image scaling and performance optimization

A) transfer from 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.

3. Summary:

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).

II) transfer from http://www.cnblogs.com/Imageshop/archive/2011/11/12/2246808.html

In image processing, bilinear interpolation algorithm is used very frequently, for example, in the image scaling, in all the distortion algorithm, can use this algorithm to improve the processing of visual effects. First, let's look at an introduction to the algorithm.

In mathematics, bilinear interpolation algorithm can be regarded as the extension of linear interpolation between two variables. The key idea to perform this procedure is to perform a linear interpolation in one direction before interpolating in another direction. Indicate the approximate meaning of the process.

A simple mathematical expression can be expressed as follows:

f (x, y) =f (0,0) (1-x) (1-y) +f (1,0) x (1-y) +f (0,1) (1-x) y+f (All) xy

Merge related items, can be written as: f (x, Y) = (f (0,0) (1-x) +f (1,0) x) (1-y) + (f (0,1) (1-x) +f (All) x) y

It can be seen from the above that there are a lot of floating-point numbers in this process, which is a more time-consuming process for computing users with such large images.

Considering the particularity of the image, his pixel value of the calculation results need to fall between 0 to 255, up to 256 kinds of results, from the above can be seen, in general, the calculated f (x, y) is a floating point number, we also need to be rounded up the floating point. Therefore, we can consider the process of all similar to 1-x, 1-y variable magnification appropriate multiples, the corresponding integer, and then divided by a suitable integer as the result of interpolation.

How to take this appropriate magnification, to consider from three aspects, the first: the accuracy of the problem, if the number is too small, then after the calculation may result in a large error. Second, this number cannot be too large, and too much of the assembly causes the computational process to be more than the range that long-shaped can express. Third: speed considerations. If the magnification is 12, then the calculation in the final result should be divided by 12*12=144, but if taken as 16, then the last divisor is 16*16=256, this number is good, we can use the right shift to achieve, and the right shift is much faster than the normal division.

Considering the above three articles comprehensively, we choose 2048 this number is more appropriate.

Below we assume that an algorithm obtains the coordinates we want to sample, respectively Posx and PosY, where posx=25.489,posy=58.698. The similar code snippet for this process is as follows:

1 newx = Int (PosX) ' Rounding down, newx=25
2 Newy = Int (PosY) ' Rounding down, newy=58
3 Partx = (posx-newx) * 2048 ' corresponds to an x in the expression
4 party = (posy-newy) * 2048 ' corresponds to Y in an expression
5 INVX = 2048-partx ' corresponds to 1-x in an expression
6 Invy = 2048-party ' corresponds to 1-y in an expression
7
8 Index1 = samstride * newy + newx * 3 ' calculates the memory address of the pixel adjacent to the upper-left corner of the sampling point
9 Index2 = Index1 + samstride ' lower left corner pixel address
10 imagedata (speed + 2)  =  (Sample (index1 + 2)  * invx +  sample (index1 + 5)  * partx)  * InvY +  (Sample (index2 +  2)  * InvX +                            Sample (index2 + 5)  *  PARTX)  * party)  \ 4194304        ' processing red component 11  ImageData (speed + 1)  =  (Sample (index1 + 1)  * invx + sample ( index1 + 4)  * partx)  * InvY +  (Sample (index2 + 1)  *  InvX +                           sample (index2 + 4)  *  PARTX)  * party)  \ 4194304        ' Processing of green components
ImageData (Speed) = ((sample (INDEX1) * INVX + sample (INDEX1 + 3) * partx) * Invy + (sample (INDEX2) * INVX + Sample (INDEX2 + 3) * partx) * Party) \ 4194304 ' Processing blue component

The variables involved in the above code are integers (POSX and posy, of course, are floating point types).

The sample array in the code holds the image data from which it was sampled, samstride the size of the scanned row for that image.

Observe the above code, except that there are 2 sentences involving floating-point calculations, the other is an integer between the operations.

In the basic language, if all advanced optimizations are checked at compile time, \ 4194304 is compiled to >>22, that is, 22 bits to the right, and if you are using C, write directly as >>22.

It is important to note that before this generation of code, it is necessary to ensure that posx and posy are within reasonable bounds, i.e. the width and height range of the sampled image cannot be exceeded.

With such an improvement, the speed is more than the direct use of floating-point type at least 100% faster, and the effect of treatment is basically no different.

[Reprint] bilinear interpolation algorithm for image scaling and performance optimization

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.