OpenCV任意角度旋轉映像

來源:互聯網
上載者:User

#include "cv.h"  
#include "highgui.h"  

int main()  
{  
double degree = 30; // rotate 30 degree  
double angle = degree  * CV_PI / 180.; // angle in radian  
double a = sin(angle), b = cos(angle); // sine and cosine of angle  

// Load source image as you wish  
IplImage *imgSrc = cvLoadImage("D:\\images\\128.png");  
int w_src = imgSrc->width;  
int h_src = imgSrc->height;  
cvNamedWindow ("src", 1);  
cvShowImage ("src", imgSrc);  
// Make w_dst and h_dst to fit the output image  
int w_dst = int(h_src * fabs(a) + w_src * fabs(b));  
int h_dst = int(w_src * fabs(a) + h_src * fabs(b));  

// map matrix for WarpAffine, stored in statck array  
double map[6];  
CvMat map_matrix = cvMat(2, 3, CV_64FC1, map);  

// Rotation center needed for cv2DRotationMatrix  
CvPoint2D32f pt = cvPoint2D32f(w_src / 2, h_src / 2);  
cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);  

// Adjust rotation center to dst's center,  
// otherwise you will get only part of the result  
map[2] += (w_dst - w_src) / 2;  
map[5] += (h_dst - h_src) / 2;  

// We need a destination image  

IplImage *imgDst = cvCreateImage(cvSize(w_dst, h_dst), 8, 3);  
cvWarpAffine(  
imgSrc,   
imgDst,  
&map_matrix,  
CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,  
cvScalarAll(0)  
);  

// Don't forget to release imgSrc and imgDst if you no longer need them  
cvNamedWindow( "dst_big", 1 );  
cvShowImage( "dst_big", imgDst);  
cvWaitKey(0);  
cvReleaseImage(&imgSrc);  
cvReleaseImage(&imgDst);  
return 0;  


    

       原圖                     
30度旋轉                90度旋轉            
120度旋轉


   180度旋轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.