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

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

先计算出图像倾斜矩形的长宽,而后将矫正后的形状的矩形坐标确定,通过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函数

posted @ 2017-11-20 17:41  鸭子船长  阅读(569)  评论(0编辑  收藏  举报