The principle and realization of bilinear interpolation

Source: Internet
Author: User

In the process of spatial transformation of images, the typical situation is that when the image is enlarged, the image will appear distorted. This is because in the image after the transformation there are some pixel positions that are not in the image before the transformation. To illustrate this problem, suppose you have a grayscale image a of size 64x64, and now zoom in to 256x256, as shown in the image b,1. Obviously, according to the simple geometric conversion relationship, you can know that the pixel value in the B image (x, y) should correspond to the pixel value at (X/4,Y/4) in the a image, i.e.

B (x, y) = A (X/4,Y/4) (Type 1)

For b (BIS), (4,8), (4,16) ... (256,256) These positions, through the Formula 1 can calculate its position in a, so that the gray value can be obtained. However, for B (1,3), (...) ... And so on, these coordinate points, if computed by Formula 1, then their corresponding coordinates in a are no longer integers. For example, for a coordinate point in B (0.25,0.25), its corresponding coordinate in a is changed to (X-y). For digital images, fractional coordinates are meaningless. Therefore, it is necessary to consider a method to get the gray level of the pixel point in B in the corresponding position in a. The method of dealing with this problem is called image gray level interpolation. There are three kinds of interpolation methods commonly used: Nearest neighbor interpolation, bilinear interpolation, double three-time interpolation. In theory, the nearest neighbor interpolation has the worst effect, with a double three-time interpolation that works best, and the effect of bilinear interpolation is in between. However, bilinear interpolation is often sufficient for image interpolation that is not strictly required.

In this paper, we will implement a bilinear interpolation program using MATLAB. Bilinear interpolation is shown in principle 2. There are two ways of mapping coordinates between images: If you are mapping from the coordinates of the original image to the target image, it is referred to as a forward mapping and vice versa. It is obvious that bilinear interpolation uses a back-mapping method. The specific meanings of Figure 2 are described below. First, based on the relations, the coordinates (x, y) in the B image are obtained from the coordinates in the A image (X/4,Y/4), but the resulting coordinate (X/4,Y/4) is not exactly in the integer coordinates of the a image, but is mapped to four pixel coordinates (a, b), (A+1,B), (A, b+1), (a+1,b+1) between the rectangles, where a, B is an image of the integer coordinates. The question now is how to find the grayscale level at a (X/4,Y/4) based on the gray level on the four points of a (a, b), A (A+1,b), A (a,b+1), A (a+1,b+1). Bilinear interpolation technique adopts the method of assuming that the gray level of a image changes linearly in the longitudinal direction, so that the gray level A (A,Y/4) and A (A+1,Y/4) at (A,Y/4) and (A+1,Y/4) coordinates can be obtained according to the linear equation or geometric proportional relationship. Then, assuming that the line is determined by the two points ((A,Y/4), A (A,Y/4)) and (A+1,Y/4), A (A+1,Y/4)), the gray level is still linearly variable. The linear equation is obtained, so the gray level A (X/4,Y/4) at (X/4,Y/4) can be calculated. This is the basic idea of bilinear interpolation. The two basic assumptions used are: first, the gray level is linearly variable in the longitudinal direction, and then the gray level is assumed to be linearly variable in the transverse direction.

Figure 1 Image scaling

Figure 2 bilinear interpolation

Appendix:

I=imread (' image1.bmp '); [M,n]=size (I); K=3;width = k * M;height = k * N; J = uint8 (zeros (width,height));% width scale and height Scalewidthscale = M/width;heightscale = n/height;% bilinear interp  Lotfor x = 5:width-5for y = 5:height-5xx = x * widthscale;yy = y * heightscale;if (xx/double (UInt16 (xx)) = = 1.0) & (Xx/double (UInt16 (xx)) = = 1.0) J (x, y) = I (Int16 (XX), Int16 (yy)), else% A or b is not Integera = double (UInt16 (xx)); % (A, b) is the BASE-DOTB = double (UInt16 (yy)); x11 = double (I (A, b)); % X11 <-I (A, b) x12 = double (I (a,b+1)); % X12 <-I (a,b+1) x21 = Double (I (a+1,b)); % x21 <-I (a+1,b) x22 = Double (I (a+1,b+1)); % x22 <-I (a+1,b+1) J (x, y) = Uint8 ((b+1-yy) * ((XX-A) *x21 + (A+1-XX) *x11) + (yy-b) * ((XX-A) *x22 + (A+1-XX) * x12)); % calculate J (x, y) endendend% display image Imwrite (j, ' magnified image. jpg ', ' jpg '), imshow (I), title (' original '), Figureimshow (j), title (' enlarged View ');

  

I=imread (' image1.bmp '); [M,n]=size (I); K=3;width = k * M;height = k * N; J = uint8 (zeros (width,height));% width scale and height Scalewidthscale = M/width;heightscale = n/height;% bilinear interp  Lotfor x = 5:width-5for y = 5:height-5xx = x * widthscale;yy = y * heightscale;if (xx/double (UInt16 (xx)) = = 1.0) & (Xx/double (UInt16 (xx)) = = 1.0) J (x, y) = I (Int16 (XX), Int16 (yy)), else% A or b is not Integera = double (UInt16 (xx)); % (A, b) is the BASE-DOTB = double (UInt16 (yy)); x11 = double (I (A, b)); % X11 <-I (A, b) x12 = double (I (a,b+1)); % X12 <-I (a,b+1) x21 = Double (I (a+1,b)); % x21 <-I (a+1,b) x22 = Double (I (a+1,b+1)); % x22 <-I (a+1,b+1) J (x, y) = Uint8 ((b+1-yy) * ((XX-A) *x21 + (A+1-XX) *x11) + (yy-b) * ((XX-A) *x22 + (A+1-XX) * x12)); % calculate J (x, y) endendend% display image Imwrite (j, ' magnified image. jpg ', ' jpg '), imshow (I), title (' original '), Figureimshow (j), title (' enlarged View ');

  

The principle and realization of 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.