Enlarge an image using bilinear interpolation (MATLAB implementation)

Source: Internet
Author: User

bilinear interpolation concept and formula can refer to Baidu, here only the principle of the algorithm simple description:

bilinear interpolation Calculation formula:

F (i+u,j+v) = (1-u) (1-v)f (i,j)+u (1-v)F (i+1,j) +(1-u) vF (i,j+1) + UVF (i+1,j+1)

This formula shows how the new pixel values are calculated using the four pixel values in the matrix, which make up the enlarged image.

How to magnify a 3x3 image into a 4x4 image:

The original image is represented as a 3x3 matrix (the pixel value is at the intersection of the black line), how to calculate the value of the 4x4 matrix? (The pixel value is at the intersection of the red dashed line and the red and black line)

For example, the first column of the new image B corresponds to the first column of the original image a:

B (A) = A (A)

B (from) = A (1,1.66667)

B (1,3) = A (1,2.33334)

B (1,4) = A (1,3.00001)

Is it magical to use the value of the original image A to calculate the value of the magnified B?

In fact, it can be said that bilinear interpolation is to compress the enlarged image to the original size of the image, calculate the virtual pixel value in the original image, equivalent to calculate the magnified image pixel value,

For this example, the step size of the B image is equal to the step of a image (3-1)/(4-1) =0.66667 times. Below we can use this ratio to correspond to the pixel position in B and the location of the virtual pixel in a.

B (1,1) = A (1,1) (1-1) *0.66667+1=1

B (1,2) = A (1,1.66667) (2-1) *0.66667+1=1.66667

B (1,3) = A (1,2.33334) (3-1) *0.66667+1=2.33334

B (1,4) = A (1,3.00001) (4-1) *0.66667+1=3.00001

Based on the correspondence above, we can implement it in code.

Now there is one more question:

We calculate the virtual pixel value is required around four of the original pixel values, such as the above (in red circled part)

A (1,3) = (1-0) (1-0) A (1,3) + (1-0) 0A (1,4) + 0 (1-0) A (2,3) + xxA (2,4)

Obviously a (1,4) and a (2,4) are not indexed here because the original image is a 3x3 matrix.

To solve this problem, in the last line of a, add 0, respectively, to the last column, so that a becomes a 4x4 matrix.

The black dashed line in the illustration is the added 0 rows and 0 columns, and the red diagonal arrows mark the locations of the virtual pixels that need to be used to extend the a matrix.

Code implementation:

Main program code:

Clear; Close all;Clcimage= Imread (' bird.png ');% Load Image value R= Image (:,:, 1);% due to true color graph is red blue green three pixels overlay g= Image (:,:, 2);% here separate the r,g,b and call the function calculation B.= Image (:,:, 3);% After the calculation is completed and then assembled, you need to manually set the magnification in multiples of W= 4;%w The vertical direction L= 4;%l is horizontally oriented R.= Extenrgb (r,w,l);% Call function calculates the enlarged R value G= Extenrgb (g,w,l);The% Call function calculates the enlarged G-value B= Extenrgb (b,w,l);The % Call function calculates the magnified B-value% and then assembles the finished RGB after the calculation Outrgb(:,:, 1) = R;Outrgb(:,:, 2) = g;Outrgb(:,:, 3) = B;Outrgb= Uint8 (OUTRGB);% format conversion, otherwise cannot display imshow(OUTRGB);% display enlarged image

Functions called by the main program:

%pixel magnification computational function extenrgb () function Output=Extenrgb (a,w,l)%a matrix represents the R,g,b matrix, respectively [M,n]= Size (A); %read A's row and column a= [A;zeros (1,n)]; %add two lines of 0A to the last line of a= [A zeros (m+1,1)]; %add two columns to the last column of a 0% so that a becomes (m+1) x (n+1) matrix, which is to solve the boundary overflow problem when indexing a matrix Ini_u= (m-1)/(W*M-1); % Step ratio If the original step A (2,1) to a () as 1, the ini_v after the amplification= (n-1)/(l*n-1); % Image B (2,1) is equivalent to computing a (1+ini_u,1), which equals 1 Output per step= Zeros (w*m,l*n); %initializing the output matrix forj = 1:l*n; %the function of the two statement on the left is: Z_u,z_v to the left, u,v to take decimal, the principle is as follows Z_v= Floor ((j-1) *ini_v+1); % For example A is a 3x3 matrix, to zoom in to output is a 4x4 size, which is magnified by 4/3 times times, v= (j-1) *ini_v+1-z_v; The distance of the new step is equivalent to the original (3-1)/(4-1) =0.66667 fori = 1:w*m; %output (total) = A (total)% (1-1) *0.66667+1=1Z_u= Floor ((i-1) *ini_u+1); %output = A (1,1.66667)% (2-1) *0.66667+1=1.66667u= (i-1) *ini_u+1-z_u; %output (1,3) = a (1,2.33334)% (3-1) *0.66667+1=2.33334%output (1,4) = A (1,3.00001)% (4-1) *0.66667+1=3.00001%=================== The following is a code implementation of bilinear interpolation ================================Output (i,j)= (1-u) * (1-v) *a (Z_u, Z_v) + ... (1-u) * v *a (Z_u, Z_v + 1) +... u* (1-V) *a (Z_u + 1, z_v) +... u* V *a (Z_u + 1, Z_v + 1); EndEnd

Enlarge an image using bilinear interpolation (MATLAB implementation)

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.