图像处理-图像的旋转及变换

对于倾斜的图像,可以通过矩阵转换将其进行矫正。

先计算出图像倾斜矩形的长宽,而后将矫正后的形状的矩形坐标确定,通过getPerspectiveTransform获取矫正前与矫正后坐标的变换矩阵,而后利用warpPerspective将图像进行变换

 1                         float dstSize1 = dist(Final_poly[i][0], Final_poly[i][1]);
 2             float dstSize2 = dist(Final_poly[i][1], Final_poly[i][2]);
 3             float dstSizeW, dstSizeH;
 4             Point2f pts0[4], pts1[4];
 5             pts0[0].x = x1; pts0[0].y = y1;
 6             pts0[1].x = x2; pts0[1].y = y2;
 7             pts0[2].x = x3; pts0[2].y = y3;
 8             pts0[3].x = x4; pts0[3].y = y4;
 9             //cout<<pts0[0].x<<" "<<pts0[0].y<<" "<<pts0[1].x<<" "<<pts0[1].y<<" "<<pts0[2].x<<" "<<pts0[2].y<<" "<<pts0[3].x<<" "<<pts0[3].y<<endl;
10             //cout<<dstSize1<<" "<<dstSize2<<endl;
11             //if(dstSize1 > dstSize2 && fabs(Final_theta[i]) > 45.0)
12             if (fabs(Final_theta[i]) > 45.0)
13             {
14                 dstSizeW = dstSize1;
15                 dstSizeH = dstSize2;
16                 pts1[0].x = dstSizeW; pts1[0].y = dstSizeH;
17                 pts1[1].x = 0; pts1[1].y = dstSizeH;
18                 pts1[2].x = 0; pts1[2].y = 0;
19                 pts1[3].x = dstSizeW; pts1[3].y = 0;
20             }
21             //else if(dstSize1 < dstSize2 && fabs(Final_theta[i]) < 45.0)
22             else if (fabs(Final_theta[i]) <= 45.0)
23             {
24                 dstSizeW = dstSize2;
25                 dstSizeH = dstSize1;
26                 pts1[0].x = 0; pts1[0].y = dstSizeH;
27                 pts1[1].x = 0; pts1[1].y = 0;
28                 pts1[2].x = dstSizeW; pts1[2].y = 0;
29                 pts1[3].x = dstSizeW; pts1[3].y = dstSizeH;
30             }
31             Mat M_perspective = getPerspectiveTransform(pts0, pts1);
32             Mat img_perspective;
33             warpPerspective(image, img_perspective, M_perspective, Size(dstSizeW, dstSizeH), cv::INTER_CUBIC, cv::BORDER_CONSTANT);

 

getPerspectiveTransform函数

warpPerspective函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值