Research and implementation of image scaling algorithm based on bilinear interpolation

Source: Internet
Author: User

Reference: http://www.cnblogs.com/okaimee/archive/2010/08/18/1802580.html

First, Introduction

The object of digital image processing is more and more concerned because it involves all fields of society, and image scaling is especially important in the process of digital image processing, and it needs to enlarge and reduce the image in many fields of society. The STRETCHBLT function in the MFC class Library of VC + + can easily enlarge and reduce the image, but it is prone to distortion when the ratio of magnification or reduction is large, so it must be improved. In this paper, a bilinear interpolation algorithm is proposed to improve the image scaling quality.

Second, spatial transformation

The spatial transformation of images, also called geometric transformations or geometric operations, includes translation, rotation, image transformation, transpose, zooming, and so on. Geometric operations can change the spatial relationship between objects in an image, which can be seen as moving objects within an image.

The spatial transformation can be represented as: set (U,V) as the point on the source image, (x, y) as the point on the target image, the spatial transform is the color value on the source image (U,V) corresponding to the color at the target image (x, y).

(u,v) ß----------------à (x, y)

and has the following relationships:

X=x (U,v), Y=y (u,v) (i.e. (U,V) compute the corresponding point (x, y)) (1.1)

Or

U=u (x, y), v=v (x, y) (that is, the corresponding point (x, y) is reversed (U,V)) (1.2) where x (u,v), Y (u,v), U (x, y), V (x, y) are transformations. The transformation referred to by (1.1) is called the forward mapping method, also known as the pixel handover method, and the (1.2) corresponding transformation called the backward mapping method is called the pixel fill method, the backward mapping method is the inverse of the forward mapping method.

For forward mapping, because many of the input pixels may be mapped outside the bounds of the output image, the forward mapping method is wasteful, and the grayscale value of each output pixel may be determined by the grayscale values of many input pixels, and therefore involves multiple operations. If the spatial transformation includes reduced processing, there will be more than four input pixels to determine the grayscale value of the output pixel, and some output pixels may be omitted if magnified processing is included. The backward mapping algorithm produces output images on a pixel-by-line basis. The gray level of each pixel is determined by the interpolation of up to four pixels, although the backward mapping method is more complex than the forward mapping method, but the backward mapping method is more realistic for the general application. In this paper, the backward mapping method is adopted to realize the image scaling.

Three bilinear interpolation

The simplest interpolation algorithm is the nearest interpolation, also known as the 0-step interpolation. The pixel grayscale value of the output is equal to the gray value of the input pixel closest to the location it maps to, the nearest interpolation algorithm is simple, and in many cases the results are satisfactory, but when the image contains subtle structures with varying levels of gray between pixels, the nearest neighbor algorithm produces artifacts in the image. bilinear interpolation algorithm is larger than zero-order interpolation, but the image quality is high after zooming, so there is no discontinuity of pixel value, so a satisfactory result can be obtained.

bilinear interpolation is calculated by bilinear algorithm using the correlation of four pixel points around the pixel points of the original image to be processed. For a destination coordinate, the corresponding floating point coordinates (I+U,J+V) of the original image are obtained by the backward mapping method, where i,j are non-negative integers and u,v is the floating-point number of the [0,1] interval, then the value F (i+u,j+v) of this pixel can be from the coordinates of the original image (I,j), (I+1,J), (i,j+1), (i+1,j+1) corresponds to the value of the surrounding four pixels, namely: f (i+u,j+v) = (1-u) x (1-v) XF (i,j) + (1-u) xvxf (i,j+1) +ux (1-v) XF (i+1,j) +uxvxf (i+1,j+) 1), where F (i,j) represents the pixel value at the source image (I,j), and so on, this is the bilinear interpolation method.

As shown in Figure 1, the known (0,0), (0,1), (1,0), (four) grayscale, can be the gray value of the neighboring pixels f (0,0) and F (1,0) in the X-direction of the linear interpolation (x,0) of the gray scale F (x,0), by the other two adjacent pixels f (0,1) and F (the Linear interpolation in the x direction can be obtained (x,1) of the gray level F (x,1), and finally by F (x,0), F (x,1) in the Y-direction of the linear interpolation can be (x, y) of the gray-scale f (x, y).

Four. Algorithms

1. Algorithms

Suppose the original image size is SIZE=MXN, where M and n are respectively the number of rows and columns < of the original image. If the scaling factor of the image is T (t>0), the size of the target image is size=txmxtxn. For a pixel of the target image p (x, y) can be obtained by p*1/t the corresponding original image coordinates P ' (x1,y1), wherein x1=x/t,y1=y/t, because x1,y1 is not an integer so there is no such point, so that it can find the four points adjacent to the grayscale F1, F2, F3, F4, uses bilinear interpolation algorithm to get this pixel point P ' (x1,y1) grayscale, that is, pixel point P (x, y) grayscale.

A complete bilinear interpolation algorithm can be described as follows:

(1) The size of the new image is obtained by the original image and scale factor, and a new image is created.

(2) A pixel (x, y) of the new image is mapped to the original image (× ', "

(3) The X ', y ' rounding is obtained (XX,YY) and the values (XX,YY), (Xx+1,yy), (xx,yy+1), and (xx+1,yy+1) are obtained.

(4) Use bilinear interpolation to value the pixel point (x, y) and write back the new image.

(5) Repeat step (2) until all pixels of the new image are finished.

2. Core code

[CPP]  View plain  copy  /function name bilinear  //Parameter float k  //return value no   // function uses bilinear interpolation to achieve image scaling    void cchildview::bilinear (float k)    {         int nbpp=m_impicture . getbpp  ();          int widthnew,heightnew;//the width and height of the new image        float widthscale= (float) (1.0/k), heightscale= (float) (1.0/k);        float xx,yy;       int a,b;       int rr,gg,bb;//Save R, G, b component        //to get the width and height of the new image         widthnew= (int) (M_IMPICTURE&NBSP;. getwidth  () *k);       heightnew = (int) (M_IMPICTURE&NBSP;. getheight  () *k);       //Create new images with the width and height of the new image         m_imnewpicture . destroy  ();       m_imnewpicture . create  (WIDTHNEW&NBSP;,HEIGHTNEW&NBSP;,NBPP);       //Gets the number of bytes per line of new and old images         int npitch=m_impicture . getpitch  ();       int npitchnew=m_imnewpicture . getpitch  ();       //get data pointers for new and old images            lpbyte pbitsnew= (LPBYTE) m_imnewpicture . getbits  ();          lpbyte pbits= (LPBYTE) m_impicture . getbits  ();            if (m_impicture.getbpp  ()!=24) {              MessageBox  ("must be a 24-bit image or 8-bit image");               m_imnewpicture . destroy  ();     

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.