OpenCV 图像旋转

 

 1 RotateArbitrarilyAngle(Mat src, Mat &dst, float angle)
 2 {
 3     float radian = (float)(angle / 180.0 * CV_PI);
 4 
 5     //填充图像
 6     int maxBorder = (int)(max(src.cols, src.rows)* 1.414); //即为sqrt(2)*max
 7     int dx = (maxBorder - src.cols) / 2;
 8     int dy = (maxBorder - src.rows) / 2;
 9     copyMakeBorder(src, dst, dy, dy, dx, dx, BORDER_CONSTANT);
10 
11     //旋转
12     Point2f center((float)(dst.cols / 2), (float)(dst.rows / 2));
13     Mat affine_matrix = getRotationMatrix2D(center, angle, 1.0);//求得旋转矩阵
14     warpAffine(dst, dst, affine_matrix, dst.size());
15 
16     //计算图像旋转之后包含图像的最大的矩形
17     float sinVal = abs(sin(radian));
18     float cosVal = abs(cos(radian));
19     Size targetSize((int)(src.cols * cosVal + src.rows * sinVal),
20         (int)(src.cols * sinVal + src.rows * cosVal));
21 
22     //剪掉多余边框
23     int x = (dst.cols - targetSize.width) / 2;
24     int y = (dst.rows - targetSize.height) / 2;
25     Rect rect(x, y, targetSize.width, targetSize.height);
26     dst = Mat(dst, rect);
27 }

 

转载于:https://www.cnblogs.com/hsy1941/p/11563253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值