使用opencv仿射变换warpaffine旋转图像

opencv提供了一个简单的旋转图像算子cv::rotate(inimage,outimage,rotate_code),但是该算子仅能选择90°的整数倍,也就是说图像只能正交旋转。

通用仿射变换warpaffine方法可以实现自由旋转,但是需要注意默认的旋转矩阵中,平移分量是原图像的width/2和height/2,也就是说旋转后图像尺寸的变化需要自行考虑处理。

插代码

int width = inimg.cols; int height = inimg.rows;
double r_angle = angle * M_PI / 180;
int new_width = width * cos(r_angle) + height * sin(r_angle);
int new_height = width * sin(r_angle) + height * cos(r_angle);
cv::Mat RotateMat = cv::getRotationMatrix2D(cv::Point2f(width /2, height /2), angle, 1);
RotateMat.at<double>(0, 2) += (new_width / 2 - width / 2);
RotateMat.at<double>(1, 2) += (new_height / 2 - height / 2);
cv::warpAffine(inimg, outimg, RotateMat, cv::Size(new_width, new_height));

旋转效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值