// 图像旋转 void Rotate(const cv::Mat &srcImage, cv::Mat &dstImage, double angle, cv::Point2f center, double scale) { cv::Mat M = cv::getRotationMatrix2D(center, angle, scale);//计算旋转的仿射变换矩阵 cv::warpAffine(srcImage, dstImage, M, cv::Size(srcImage.cols, srcImage.rows));//仿射变换 }
getRotationMatrix2D用来获得旋转的仿射变换矩阵。
Mat getRotationMatrix2D(Point2f center, double angle, double scale);
warpAffine用来仿射变换。
void warpAffine( InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar());
#include <iostream> #include <opencv.hpp> using namespace std; using namespace cv; // 图像旋转 void Rotate(const cv::Mat &srcImage, cv::Mat &dstImage, double angle, cv::Point2f center, double scale) { cv::Mat M = cv::getRotationMatrix2D(center, angle, scale);//计算旋转的仿射变换矩阵 cv::warpAffine(srcImage, dstImage, M, cv::Size(srcImage.cols, srcImage.rows));//仿射变换 } int main() { // 载入图像 cv::Mat src = imread("0.jpg"); cv::Mat dst; // 定义参数 int row = src.rows; int col = src.cols; double angle = 30; cv::Point2f center(col / 2, row / 2); double scale = 0.5; // 图像旋转 Rotate(src, dst, angle, center, scale); // 显示图像 imshow("src", src); imshow("result", dst); waitKey(0); system("pause"); return 0; }
图原图
图2 旋转30°
图3原图大小旋转45
不难看出,旋转后原图的尺寸已经无法满足图像要求了,此时可以缩小比例,也可以扩展整图尺寸使其完全覆盖。
作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处