opencv图像旋转

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char ** argv)
{
        Mat inputImg,tempImg;
        inputImg = imread("cut.jpg");			//用opencv读取图像数据
        imshow("inputImg",inputImg);

        CV_Assert(!inputImg.empty());

        float angle = 15;
        float radian = (float) (angle /180.0 * CV_PI);

        //填充图像使其符合旋转要求
        int uniSize =(int) ( max(inputImg.cols, inputImg.rows)* 1.414 );
        int dx = (int) (uniSize - inputImg.cols)/2;
        int dy = (int) (uniSize - inputImg.rows)/2;

        copyMakeBorder(inputImg, tempImg, dy, dy, dx, dx, BORDER_CONSTANT);


        //旋轉中心
        Point2f center( (float)(tempImg.cols/2) , (float) (tempImg.rows/2));
        Mat affine_matrix = getRotationMatrix2D( center, angle, 1.0 );

        //旋轉
        warpAffine(tempImg, tempImg, affine_matrix, tempImg.size());


        //旋轉后的圖像大小
        float sinVal = fabs(sin(radian));
        float cosVal = fabs(cos(radian));

        
        Size targetSize( (int)(inputImg.cols * cosVal + inputImg.rows * sinVal),
                (int)(inputImg.cols * sinVal + inputImg.rows * cosVal) );

        //剪掉四周边框
        int x = (tempImg.cols - targetSize.width) / 2;
        int y = (tempImg.rows - targetSize.height) / 2;

        Rect rect(x, y, targetSize.width, targetSize.height);
        tempImg = Mat(tempImg, rect);

        imshow("Show",tempImg);

        cvWaitKey();
        return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小新识图

你的鼓励是我最大的分享动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值