歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV函數cv2DRotationMatrix實現圖像旋轉

OpenCV函數cv2DRotationMatrix實現圖像旋轉

日期:2017/3/1 9:43:49   编辑:Linux編程

OpenCV函數cv2DRotationMatrix實現圖像旋轉

#include <cv.h>
#include <highgui.h>

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")

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("test.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;
}

Objective-C中@property的所有屬性詳解 http://www.linuxidc.com/Linux/2014-03/97744.htm

Objective-C 和 Core Foundation 對象相互轉換的內存管理總結 http://www.linuxidc.com/Linux/2014-03/97626.htm

使用 Objective-C 一年後我對它的看法 http://www.linuxidc.com/Linux/2013-12/94309.htm

10個Objective-C基礎面試題,iOS面試必備 http://www.linuxidc.com/Linux/2013-07/87393.htm

Objective-C適用C數學函數 <math.h> http://www.linuxidc.com/Linux/2013-06/86215.htm

Copyright © Linux教程網 All Rights Reserved