OpenCV函数cv2DRotationMatrix实现图像旋转

本文介绍了如何利用OpenCV中的cv2DRotationMatrix函数来实现图像的旋转操作,适合初学者理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
#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) / 2map[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;}

           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值