opencv_手动旋转图片

#include <iostream>  
#include <opencv2\core\core.hpp>  
#include <opencv2\highgui\highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>  

using namespace std;
using namespace cv;

int degree = 173;
double a, b;
Mat srcImage, dstImage;

void on_degree(int, void*)
{
    dstImage.create(srcImage.size(), srcImage.type());

    double a = sin(degree * CV_PI / 180);
    double b = cos(degree * CV_PI / 180);

    int width = srcImage.cols;
    int height = srcImage.rows;

    int rotate_width = int(height * fabs(a) + width * fabs(b));
    int rotate_height = int(width * fabs(a) + height * fabs(b));

    Point center = Point(srcImage.cols / 2, srcImage.rows / 2);

    Mat map_matrix = getRotationMatrix2D(center, degree, 1.0);
    cout << "M= " << " " << map_matrix << endl;
    map_matrix.at<double>(0, 2) += (rotate_width - width) / 2;     // 修改坐标偏移  
    map_matrix.at<double>(1, 2) += (rotate_height - height) / 2;   // 修改坐标偏移  

    warpAffine(srcImage, dstImage, map_matrix, { rotate_width, rotate_height }, CV_INTER_CUBIC);

    imshow("变换图像", dstImage);
}
int main()
{
    srcImage = imread("1.png");
    if (!srcImage.data)
    {
        cout << "读入图片有误!" << endl;
        return -1;
    }
    namedWindow("原图像");
    namedWindow("变换图像");

    createTrackbar("角度:", "变换图像", &degree, 360, on_degree);
    on_degree(0, 0);

    imshow("原图像", srcImage);

    waitKey();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/csdn_dzh/article/details/79219165