Image bilinear interpolation algorithm

Source: Internet
Author: User

Introduction to the principle of algorithms

Bilinear interpolation is a first-order interpolation, which is often used for image rotation and scaling processing.

It uses the pixel values of the corresponding four points in the original image to determine the pixel values in the target images.

For the sake of understanding, let's look at two images that are different in size:

Original

Transform diagram

Assuming the width of the original image is yw , the height isxh

The width of the transform graph is the jw height ofih

So for any pixel in the transform graph (j ', I ') we can map to the original image in the following ways:

y‘ = yw/jw * j‘x‘ = xh/ih * i‘
    • 1
    • 2
    • 3

Typically, y ' and X ' are not integers.

For example, the original size:

yw = 1000xh = 800
    • 1
    • 2
    • 3

Transform diagram Size:

jw = 700ih = 700
    • 1
    • 2
    • 3

For pixels in the transform graph (400, 400):

y‘ = 1000/700 * 400 = 571.42857x‘ = 800/700 * 400 = 457.14286
    • 1
    • 2
    • 3

We map the (400, 400) pixels in the transform graph to the original (571.42857, 457.14286).

So we use the corresponding 4 points (571,457), (572,457), (571,458), (572,458) in the original image to determine the pixel values of the (400,400) pixels in the transformed images.

t = 571.42857 - 571 = 0.42857u = 457.14286 - 457 = 0.14286
    • 1
    • 2
    • 3

We think that the closer the distance (y ', X ') points, the greater the weight of the target pixels, the farther the distance, the smaller the impact weight.

(571,457) The point weight is S4 area, (572,457) The point weight is S3 area, (571,458) The point weight is S2 area, (572,458) The point weight is S1 area.

So we get:

(400,400) Target graph = (571,457) Xs4 + (572,457) Xs3 + (571,458) XS2 + (572,458) XS1

wherein, S1+S2+S3+S4 = 1

The above is the bilinear interpolation method of the target pixel calculation formula.

C code implementation of the algorithm
/* * param: * Mat src original picture * Mat DST target Picture */void Bgrbilinearscale (Const Mat SRC, Mat DST) {Double dsth = dst.rows;Target Image HeightDouble DSTW = dst.cols;Target Image widthDouble SRCW = src.cols;Original image width, if used with int may cause (srcH-1)/(dstH-1) constant ZeroDouble SrcH = src.rows;Original image HeightDouble XM =0;X of the mapDouble ym =0;Y of the Mapint XI =0;Map x Integer partint Yi =0;Map y integer partint XL =0;XI + 1int YL =0;Yi + 1Double xs =0;Double ys =0;/* Assigns a value to each pixel of the target picture */Forint i =0; i < dsth; i + +) {ForInt J =0; J < DSTW; J + +) {Find the mapping coordinates of the target image (I,j) point to the original image (Mapx,mapy) XM = (SrcH-1)/(Dsth-1) * I; YM = (SRCW-1)/(DSTW-1) * J;/* Take the integer part of XM mapped to the original image */XI = (int) XM; Yi = (int) ym;/* Take offset */xs = xm-xi; ys = Ym-yi; XL = XI +1; YL = Yi +1;Edge Pointif ((xi+1) > (srch-1)) XL = xi-1;if ((yi+1) > (srcw-1)) YL = yi-1;b dst.at<vec3b> (I,J) [0] = (int) (src.at<vec3b> (Xi,yi) [0]* (1-XS) * (1-ys) + src.at<vec3b> (xi,yl) [0]* (1-XS) *ys + src.at<vec3b> (xl,yi) [0]*xs* (1-ys) + src.at<vec3b> (xl,yl) [0]*xs*ys);G Dst.at<vec3b> (I,J) [1] = (int) (src.at<vec3b> (Xi,yi) [1]* (1-xs) * (1-ys) + src.at<Vec3b > (xi,yl) [1]* (1-xs) *ys + src.at<vec3b> (xl,yi) [1]*xs* (1-ys) + src.at<vec3b> (xl,yl) [ 1]*xs*ys); //r dst.at<vec3b> (i,j) [2] = ( int) (Src.at<vec3b> (Xi,yi) [2]* (1-xs) * ( 1-ys) + src.at<vec3b> (xi,yl) [2]* ( 1-xs) *ys + src.at<vec3b> (xl,yi) [2]*xs* (1-ys" + src.at<vec3b> (xl,yl) [2]*xs*ys); } }} 

Preview of the Zoom effect

The original image is a 640 x x 800 image, which transforms the target image

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 50992610

Image bilinear interpolation algorithm

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.