Image rotation C + + implementation

Source: Internet
Author: User

Reprint: http://blog.csdn.net/einsdrw/article/details/43525751

Image rotation is to rotate the image at a certain angle, according to the current point coordinates calculated after the rotation of the coordinates is not an integer, so the need for interpolation. Commonly used interpolation methods have the nearest neighbor interpolation method, linear interpolation method and spline interpolation method (this I do not understand). The nearest neighbor interpolation speed is fast, the effect is poor, bidirectional interpolation method has good effect, the speed is OK. Only the image rotation using reverse mapping and bilinear interpolation is discussed here.

Rotation principle:

The reverse mapping is based on the coordinates of the rotated image, and the coordinates in the original image are obtained.

As shown, rotate the image counterclockwise one angle

After the rotated image is computed, the (3) type can be used to calculate its coordinates in the original figure with the rotated coordinates, and to use the adjacent pixel interpolation to the current pixel value.

The origin of the image is generally in the upper-left corner, so that the calculated coordinates will have a negative value, generally the image of the original point of translation to the center of the image. Remember the original image of the width of SRCW, SrcH, after the rotation of the image width of DSTW, Dsth, then (3) the type will become:

bilinear interpolation

The original image coordinates computed by the rotated image coordinates are often not integers, and bilinear interpolation is performed to ensure the rotation effect. bilinear interpolation is the linear interpolation of two variables, and the final interpolation result is obtained by linear interpolation of each variable respectively.

Detailed information Reference

Http://www.cnblogs.com/linkr/p/3630902.html

The code is as follows, using OPENCV to load the image.

Call method[CPP]  View Plain  copy void main (int argc, char** argv)    {        iplimage* iplorg=cvloadimage (argv[1]);   //Loading Image         unsigned char* pColorImg=NULL;       int width=iplOrg-> width;       int height=iplOrg->height;        pcolorimg= (unsigned char*) malloc (width*height*3*sizeof (Unsigned char));           cvcvtcolor (IPLORG,IPLORG,CV_BGR2RGB);        Ipltouchar (iplorg,pcolorimg);    //arrays represent images        cvreleaseimage ( &iplorg);          double degree=15;   // Counterclockwise rotation angle 0~180          int templength=sqrt (double) width *  width +  (DouBLE) height *height  + 10;//guarantee that the original artwork can rotate at any angle of the minimum size        unsigned  char* ptemp= (unsigned char*) malloc (templength*templength*3*sizeof (Unsigned char));                     //rotation         myimgrotate (pcolorimg,width,height,ptemp,templength,templength,degree,3);        displaypicture (templength,templength,ptemp, "Rotate.bmp", 3);  //Save Image                free (ptemp);        pTemp=NULL;       free (pcolorimg);        pcolorimg=null;         }  


Rotate function [CPP] view plain copy//counterclockwise rotate to center of PDST, other 0 fills//psrc,srcw,srch original and size//pdst,dstw,dsth rotated image and its dimension//rotation angle Channel number void myimgrotate (unsigned char* psrc,int srcw,int srcH, unsigned char* pdst,int ds       Tw,int dsth, double degree,int nchannel) {int k;  Double angle = degree * 3.1415926/180.; Rotation angle

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.