图像旋转 OpenCV实现

转自:https://www.cnblogs.com/willwu/p/6133696.html

经常对一幅图像进行旋转操作,OpenCV中提供了很方便易用的仿射变换函数warpAffine, 通过getRotationMatrix2D可以得到放射变换矩阵(矩阵大小2x3)

#include <iostream>
#include <opencv.hpp>

using namespace std;



int main(void){
    cv::Mat src = cv::imread("test.png");
    cv::imshow("src", src);
    double angle = -45;
    cv::Point2f center(src.cols / 2, src.rows / 2);
    cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1);
    cv::Rect bbox = cv::RotatedRect(center, src.size(), angle).boundingRect();

    rot.at<double>(0, 2) += bbox.width / 2.0 - center.x;
    rot.at<double>(1, 2) += bbox.height / 2.0 - center.y;

    cv::Mat dst;
    cv::warpAffine(src, dst, rot, bbox.size());
    cv::imshow("dst", dst);
    cv::waitKey(0);

}

效果

原图

旋转后的图片

猜你喜欢

转载自blog.csdn.net/Song_Esther/article/details/83186426