经常对一幅图像进行旋转操作,OpenCV中提供了很方便易用的仿射变换函数warpAffine, 通过getRotationMatrix2D可以得到放射变换矩阵(矩阵大小2×3)
#include <iostream>
#include <opencv.hpp>using namespace std;int main(void){
cv::Mat src = cv::imread("test.png");
cv::imshow("src", src);
double angle = -;
cv::Point2f center(src.cols / , src.rows / );
cv::Mat rot = cv::getRotationMatrix2D(center, angle, );
cv::Rect bbox = cv::RotatedRect(center, src.size(), angle).boundingRect(); rot.at<double>(, ) += bbox.width / 2.0 - center.x;
rot.at<double>(, ) += bbox.height / 2.0 - center.y; cv::Mat dst;
cv::warpAffine(src, dst, rot, bbox.size());
cv::imshow("dst", dst);
cv::waitKey();}
效果
原图
旋转后的图片