图像旋转 OpenCV实现

经常对一幅图像进行旋转操作,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);

}

效果

原图

 

旋转后的图片

posted @ 2016-12-05 13:59  WillWu  阅读(16206)  评论(2编辑  收藏  举报