opencv图像仿射变换和普通旋转

背景:今天需要对程序生成的图像进行旋转90度和下采样操作,当然还有改变图像类型的操作,就是把原来.png的图像转换为.jpg的图像,主要是我目前使用libharu库,无法成功从本地加载png图像到pdf中去,不得不使用jpg图像。我的图像是横向的,为了能够更大的呈现在pdf中,我需要将图像旋转90度,得到竖向的图像。

我最初使用的方法是这样的

 

cv::Mat temp, dest;
cv::Mat img1 = cv::imread("dancer.png");
		
cv::imshow("org", img1);
对读入的图像进行旋转90度
cv::Point2f center(img1.cols / 2, img1.rows / 2);
cv::Mat M = getRotationMatrix2D(center, 90, -1);
warpAffine(img1, dest, M, cv::Size(img1.cols, img1.rows));
		
cv::imshow("dest", dest);
//将旋转后的图像降分辨率

cv::imwrite("img1.png", dest);
cv::waitKey(0);

经过上面的方式对图像旋转90度后,得到的图像如下图,硬生生被截掉一截,

 

后来找到了这种方法,直接可以旋转90度,180度

cv::Mat temp, dest;
cv::Mat cover = cv::imread("dancer.png");
cv::imshow("org", cover);
transpose(cover, temp);
flip(temp, dest, 1);
cv::imshow("temp", temp);
cv::imshow("dest", dest);
cv::imwrite("temp.png", temp);
cv::imwrite("dest.png", dest);
cv::waitKey(0);

原始图像

 

经过tranpose进行变换的图像,达到的效果是对原图像顺时针旋转90度,且进行镜面变换。

 

既然进行了镜面变换,那我再给他镜面回来不久好了。使用flip函数进行镜面变换,我们可以看到,下面的图像就是将原始图像顺时针旋转90度的结果了。

 

下面实现将图像旋转180度。

cv::Mat temp, dest;
cv::Mat cover = cv::imread("dancer.png");
cv::imshow("org", cover);
flip(cover, dest, -1);
cv::imshow("dest", dest);
cv::imwrite("dest.png", dest);
cv::waitKey(0);

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值