Implementation of bilinear interpolation image stretching on mobile

Source: Internet
Author: User

Before changing the frequency domain, let's take a look at the changes on the point plane. This selects bilinear interpolation to implement bilinear interpolation. This is due to seeing other people's problems on csdn. we can implement a function at the right for your convenience.

Bilinear interpolation simply means that when the pixel coordinates of the extended image are mapped back to the original coordinate space, if there is no corresponding integer point. At this time, we need to perform two linear interpolation to calculate the pixel value of the new coordinate, for example:

Here we can see that this P point falls within the ABCD range. If we adopt the principle of "who is closest to the most simple P point", the weighted average will be larger. We can easily get this conclusion:

The Influence of Point A on P is the sa area, B is Sb, C is SC, and D is SD. The closer it is, the larger the weight is. Basically, this logic is used.

In this way, P pixels can be simply used (A * Sa + B * Sb + C * SC + D * SD)/(SA + Sb + SC + SD. I immediately wrote the following code with my strong popularity:

/** <Br/> ** method to remove sharp the raw image with unsharp mask <br/> * @ Param SRC input grayscale binary array <br/> * @ Param DST output grayscale result, the memory need to be allocated outside of the function <br/> * @ Param srcwidth width of the input grayscale image <br/> * @ Param srcheight height of the input grayscale image <br /> * @ Param scalepercent, scale percentage (0-xxx) <br/> */<br/> void stretchimage (const unsigned char * SRC, unsigned char * DST, int srcwidth, int srcheight, int scalepercent) <br/>{< br/> If (scalepercent <0) <br/> return; <br/> int X, Y; <br/> int ox, oy; <br/> int tmpx, tmpy; <br/> int ratio = (100 <8)/scalepercent; <br/> int dstwidth = srcwidth * scalepercent/100; <br/> int dstheight = srcheight * scalepercent/100; <br/> unsigned char color [2] [2]; <br/> for (Int J = 0; j <dstheight; j ++) <br/>{< br/> for (INT I = 0; I <dstwidth; I ++) <br/>{< br/> tmpx = I * ratio; <br/> tmpy = J * ratio; <br/> ox = tmpx> 8; <br/> Oy = tmpy> 8; <br/> X = tmpx & 0xff; <br/> Y = tmpy & 0xff; <br/> color [0] [0] = SRC [Oy * srcwidth + ox]; <br/> color [1] [0] = SRC [Oy * srcwidth + ox + 1]; <br/> color [0] [1] = SRC [(Oy + 1) * srcwidth + ox]; <br/> color [1] [1] = SRC [(Oy + 1) * srcwidth + ox + 1]; <br/> int final = (0x100-x) * (0x100-y) * color [0] [0] + x * (0x100-y) * color [1] [0] + (0x100-x) * y * color [0] [1] + x * y * color [1] [1]; <br/> final = final> 16; <br/> If (final> 255) <br/> final = 255; <br/> If (final <0) <br/> final = 0; <br/> DST [J * dstwidth + I] = (unsigned char) final; <br/>}< br/>

 

It should be noted that floating point numbers need to be introduced with a certain degree of efficiency loss. Of course, here we will use large numbers for harmony. However, the Code is just written at will. We didn't add any checks or instructions beyond the int range, so we can only do this for the time being :). The effect of using this function is good. We can see the effect of 75%, 125%, and 250%:

Source image:

% 75:

125%:

250%:

In fact, we can see from the number of points that the image sharpness actually decreases with the image stretching, which is easy to imagine, because our stretching method is linear, without a doubt, the sharp edge is blurred when the image is expanded, so the effect is not very good when the image is expanded many times.

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.