Implementation of bilinear interpolation image rotation on mobile

Source: Internet
Author: User

After the image is stretched, we naturally want to rotate the image. We found the formula for image rotation:

 

X' = x cos θ-y sin θ;

Y' = x sin θ + y cos θ;

 

This image formula can be used in high school mathematics. Then we need to expand because we do not rotate at the origin. We need to rotate around the original image center. Then we assume that the original image center is oldcenterx and oldcentery. after rotation, we need to adjust the image position to the new coordinate center. Then we need a new newcenterx and newcentery. The new coordinate is the center of the new image. Then our formula can be transformed:

 

X' = (X-oldcenterx) COS θ-(Y-oldcentery) sin θ + newcenterx;

Y' = (X-oldcenterx) sin θ + (Y-oldcentery) COS θ + newcentery;

 

Of course, the key issue is not the position after rotation, but the relationship between the positions after rotation to the original position. That is to say, what we need more is an X, Y expressions about X' and Y. It is very simple. We have changed the problem to an equation of RMB 2!

X = y 'sin θ + x' cos θ + oldcentery-newcenterx cos θ-newcentery sin θ;

Y = y 'cos θ-x' sin θ + oldcentery-newcentery cos θ + newcenterx sin θ;

 

In this way, it is easy to write a suitable code. But another notable problem is that there is no trigonometric function. What should I do? Just as we use a large number during interpolation, we use a 13-digit left shift to describe first, as shown below:

// Test interface for math <br/> const int k_cosinetable [24] = <br/>{< br/> 8192, <br/> 8172, <br/> 8112, <br/> 8012, <br/> 7874, <br/> 7697, <br/> 7483, <br/> 7233, <br/> 6947, <br/> 6627, <br/> 6275, <br/> 5892, <br/> 5481, <br/> 5043, <br/> 4580, <br/> 4096, <br/> 3591, <br/> 3068, <br/> 2531, <br/> 1981, <br/> 1422, <br/> 856, <br/> 285, <br/>-285 <br/>}; <br/> int shiftcos (INT y) <br/> {<br/> If (Y <0) y * =-1; <br/> Y % = 360; <br/> If (Y> 270) <br/>{< br/> return shiftcos (360-y); <br/>}< br/> else if (Y> 180) <br/>{< br/> return-shiftcos (Y-180); <br/>}< br/> else if (Y> 90) <br/>{< br/> return-shiftcos (180-y); <br/>}< br/> int Index = (Y> 2 ); <br/> int offset = (Y % 4); <br/> // on the borderline of overflowing if use jint16 <br/> int cosval = (4-offset) * k_cosinetable [Index] <br/> + offset * k_cosinetable [index + 1]; <br/> return cosval> 2; <br/>}< br/> int shiftsin (INT y) <br/>{< br/> return shiftcos (Y + 270); <br/>}

With the help of this trigonometric function: our final code looks like this:

/** <Br/> ** method to remove sharp the raw image with unsharp mask <br/> * @ Param SRC input grayscale binary array <br/> * @ Param srcwidth width of the input grayscale image <br/> * @ Param srcheight height of the input grayscale image <br/> * @ Param [Output] DST output gray-scale image. <br/> * @ Param [Output] dstwidth width of the output grayscale image <br/> * @ Param [Output] dstheight height of the output grayscale image <br/> *@ param angle, rotate angle. <br/> */<br/> void rotateimage (const unsigned char * SRC, int srcwidth, int srcheight, unsigned char * & DST, Int & dstwidth, Int & dstheight, int angle) <br/>{</P> <p> // first calculate the new width and height; <br/> const int shift = 13; <br/> dstwidth = (ABS (srcwidth * shiftcos (angle) + ABS (srcheight * shiftsin (angle)> shift; <br/> dstheight = (ABS (srcwidth * shiftsin (angle) + ABS (srcheight * shiftcos (angle)> shift; <br/> DST = new unsigned char [dstwidth * dstheight]; <br/> int xcenter = srcwidth> 1; <br/> int ycenter = srcheight> 1; <br/> int xnew = dstwidth> 1; <br/> int ynew = dstheight> 1; <br/> const int xfix = (xcenter <8) -(ynew * shiftsin (angle)> 5)-(xnew * shiftcos (angle)> 5 ); <br/> const int yfix = (ycenter <8) + (xnew * shiftsin (angle)> 5)-(ynew * shiftcos (angle)> 5); </P> <p> int ox; <br/> int Oy; <br/> int X; <br/> int y; <br/> int kx; <br/> int ky; <br/> int color [2] [2]; <br/> for (Int J = 0; j <dstheight; j ++) <br/>{< br/> for (INT I = 0; I <dstwidth; I ++) <br/>{< br/> ox = (I * shiftcos (angle) + J * shiftsin (angle)> 5) + xfix; <br/> Oy = (-1) * I * shiftsin (angle) + J * shiftcos (angle)> 5) + yfix; <br/> If (OX> 8) <= srcwidth & (OX> 8)> = 0 & (Oy> 8) <= srcheight & (Oy> 8)> = 0) <br/>{< br/> kx = ox> 8; <br/> Ky = Oy> 8; <br/> X = ox & 0xff; <br/> Y = Oy & 0xff; <br/> color [0] [0] = SRC [Ky * srcwidth + kx]; <br/> color [1] [0] = SRC [Ky * srcwidth + kx + 1]; <br/> color [0] [1] = SRC [(KY + 1) * srcwidth + kx]; <br/> color [1] [1] = SRC [(KY + 1) * srcwidth + kx + 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/> else <br/> {<br/> DST [J * dstwidth + I] = 0xff; <br/>}< br/>}

The interface definition is described here. parameters related to the target grayscale map are reference types. It indicates that all parameters are output. because the size of the image changes after rotation, it is not convenient to allocate memory in advance, so the local allocation mode is used here. Memory Allocation is completed within the function. Although ticks is not used for the final speed test, the efficiency here is still relatively high if you want to calculate a floating point number. Of course, you can optimize some details here, for example, this constant is 5 !!! Majic number. In fact, all the original numbers want to be shifted to 8, so the numbers in the trigonometric function need to be shifted to 5 !! In addition, the formula is applied.

Finally, let's take a look at each angle:

20 degrees

40 degrees

60 degrees

80 degrees

100 degrees

120 degrees

 

 

 

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.