OpenCV rotate an image at any angle

Source: Internet
Author: User
Tags cos sin
Reproduced (http://www.cnblogs.com/HappyXie/archive/2011/03/02/1969434.html)//opencv 4 The rotation of the image at any angle

Need the support of inter Company's OPENCV.

OpenCV 4 rotation of an image at any angle
Image to be rotated iplimage* img_old
Returns the rotated image iplimage* img_tmp.
The angle of rotation, the unit degree.
Three different methods. Method Two is not fully tested, method one can meet most of the needs
Iplimage * Fitrotate (iplimage* img_old, double Angle,int method)

{

iplimage* img_tmp = NULL;

Double Anglerad = (cv_pi* (angle/180));
int Newheight =int (fabs ((Sin (anglerad) *img_old->width) + fabs ((cos (anglerad) *img_old->height));
int Newwidth =int (fabs ((Sin (anglerad) *img_old->height) + fabs ((cos (anglerad) *img_old->width));

Img_tmp = Cvcreateimage (Cvsize (newwidth,newheight), ipl_depth_8u, 3);
Cvfillimage (img_tmp,0);//destination image using an extended size

iplimage* DST = cvcloneimage (img_old);//The target image and the original image and other large


float m[6];
Cvmat m = Cvmat (2, 3, cv_32f, M);


if (1==method)
{
Method one extracts pixel quadrilateral, using sub-pixel precision

int w = img_old->width;
int h = img_old->height;

M[0] = (float) (cos (angle*cv_pi/180.));
M[1] = (float) (sin (angle*cv_pi/180.));
M[2] = w*0.5f;
M[3] =-m[1];
M[4] = m[0];
M[5] = h*0.5f;

Cvgetquadranglesubpix (Img_old, DST, &m, 1, cvscalarall (0));

Cvgetquadranglesubpix (Img_old, Img_tmp, &m, Cv_inter_linear, Cvscalarall (0));//+cv_warp_fill_outliers

Method one extracts pixel quadrilateral, using sub-pixel precision
}

if (2==method)
{
Method Two The problem of affine transformation matrix using two-dimensional rotation requires that the input and output images be the same large rotation center is wrong

CVPOINT2D32F Center;
Center.x=float (img_old->width/2.0+0.5);//float (img_tmp->width/2.0+0.5);
Center.y=float (img_old->height/2.0+0.5);//float (img_tmp->height/2.0+0.5);
Cv2drotationmatrix (center, angle,1, &m);

Cvwarpaffine (Img_old, DST, &m,cv_inter_linear,cvscalarall (0));//Small map
Small target image


To extend an image
Can only be treated differently in different quadrants within a certain angle
int Dx=int ((newwidth-img_old->width)/2+0.5);
int Dy=int ((newheight-img_old->height)/2+0.5);

uchar* old_ptr,*temp_ptr;

for (int y=0; yheight; y++)//In order not to cross the border
{

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.