当前位置:   article > 正文

C++ OpenCV透视变换综合练习_四边形拟合算法

四边形拟合算法

学更好的别人,

做更好的自己。

——《微卡智享》

本文长度为3879,预计阅读9分钟

前言

以前的文章《C++ OpenCV之透视变换》介绍过透视变换,当时主要是自己固定的变换坐标点,所以在想可不可以做一个通过轮廓检测后自适应的透视变换,实现的思路通过检测主体的轮廓,使用外接矩形和多边形拟合的四个最边的点进行透视变换。

format,png

实现效果

format,png

 

#实现思路
1图像灰度图,高斯滤波、二值化
2形态学开操作,Canny边缘检测
3查找轮廓,遍历轮廓判断周长大于图像宽度的进行多边形拟合
4判断拟合的点大于4个的获取到最小旋转矩形
5通过多边形拟合的点计算出离最小旋转矩形最近的4个点
6找到轮廓最小外接矩形作为透视变换的坐标
7将5、6的步骤两个坐标点计算透视变换矩阵
8透视变换

 

重点说明

format,png

微卡智享

01

排序旋转矩形的坐标点

format,png

图片来自网络

获取旋转矩形的函数minAreaRect( )中,四个顶点中y值最大的顶点为p[0],p[0]围着center顺时针旋转,依次经过的顶点为p[1],p[2],p[3]。角度参数angle 是P[0]发出的平行于x轴的射线,逆时针旋转,与碰到的第一个边的夹角,取值范围[-90~0]。注:逆时针旋转角度为负。

在透视变换的4个顶点的顺序为左上,右上,右下,左下,所以根据上面的原理,我们要写一个4点的重新排序,把4个顶点的顺序按透视变换的需要修改过来。

  1. //重新排序旋转矩形坐标点
  2. void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect)
  3. {
  4. rect.points(vetPoints);
  5. cout << vetPoints[0] << vetPoints[1] << vetPoints[2] << vetPoints[3] << endl;
  6. cout << rect.angle << endl;
  7. Point2f curpoint;
  8. //根据Rect的坐标点,Y轴最大的为P[0],p[0]围着center顺时针旋转,
  9. //旋转角度为负的话即是P[0]在左下角,为正P[0]是右下角
  10. //重新排序坐标点
  11. if (rect.angle > 0) {
  12. curpoint = vetPoints[0];
  13. vetPoints[0] = vetPoints[2];
  14. vetPoints[2] = curpoint;
  15. curpoint = vetPoints[1];
  16. vetPoints[1] = vetPoints[3];
  17. vetPoints[3] = curpoint;
  18. }
  19. else if (rect.angle < 0) {
  20. curpoint = vetPoints[0];
  21. vetPoints[0] = vetPoints[1];
  22. vetPoints[1] = vetPoints[2];
  23. vetPoints[2] = vetPoints[3];
  24. vetPoints[3] = curpoint;
  25. }
  26. }

02

计算多边形拟合需要透视变换的点

format,png

通过多边形拟合出来的点比较多,而使用透视变换也是只要4个点,如果使用最小旋转矩形的4个点没有什么效果,如上图中红色是多边形拟合的点,蓝色框为最小旋转矩形的点,如果用这个点无法实现透视变换的效果,所以通过遍历了多边形拟合的点,计算每个点到最小旋转矩形的距离最近的4个点,形成了上图中的白色框,虽然不完美,但是可以透视变换的效果。

距离的计算用的是欧几里德距离,然后对比找到最近的4个点。

  1. //根据最小矩形点找最近的四边形点
  2. //第一参数为输出的点,第二个参数为矩形的4个点,第三个为多边形拟合的点
  3. void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector<Point> convex)
  4. {
  5. //定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
  6. float ltdist = 99999999.9f; //左上的最大距离
  7. float rtdist = 99999999.9f; //右上的最大距离
  8. float rbdist = 99999999.9f; //右下的最大距离
  9. float lbdist = 99999999.9f; //左下的最大距离
  10. float curdist = 0.0f; //当前点的计算距离
  11. for (auto curpoint : convex) {
  12. //计算左上点的距离
  13. curdist = CalcPointDistance(rectPoints[0], curpoint);
  14. if (curdist < ltdist) {
  15. ltdist = curdist;
  16. vetPoints[0] = curpoint;
  17. }
  18. //计算右上角的点距离
  19. curdist = CalcPointDistance(rectPoints[1], curpoint);
  20. if (curdist < rtdist) {
  21. rtdist = curdist;
  22. vetPoints[1] = curpoint;
  23. }
  24. //计算右下角点的距离
  25. curdist = CalcPointDistance(rectPoints[2], curpoint);
  26. if (curdist < rbdist) {
  27. rbdist = curdist;
  28. vetPoints[2] = curpoint;
  29. }
  30. //计算左下角点的距离
  31. curdist = CalcPointDistance(rectPoints[3], curpoint);
  32. if (curdist < lbdist) {
  33. lbdist = curdist;
  34. vetPoints[3] = curpoint;
  35. }
  36. }
  37. }
  38. //计算两点间的距离
  39. float CalcPointDistance(Point2f point1, Point2f point2)
  40. {
  41. //计算两个点的Point差值
  42. Point2f tmppoint = point1 - point2;
  43. //利用欧几里德距离计算H
  44. return sqrt(pow(tmppoint.x, 2) + pow(tmppoint.y, 2));
  45. }

TIPS

距离计算时一开始用的是旋转矩形的中心点离多边形拟合按左上,右上,右下,左下的方向找最远的4个,但是在某些斜的角度比较厉害的时候,这个计算问题不小,所以后来改为离最小旋转矩形的点最近的来找了。按中心点找最远距离的函数代码没删,一并贴上来。

完整代码

  1. #include<iostream>
  2. #include<opencv2/opencv.hpp>
  3. using namespace std;
  4. using namespace cv;
  5. //根据中心点找四角最远的点
  6. void GetRectPoints(Point2f vetPoints[], Point2f center, vector<Point> convex);
  7. //根据最小矩形点找最近的四边形点
  8. void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector<Point> convex);
  9. //排序旋转矩形坐标点
  10. void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect);
  11. //计算距离
  12. float CalcPointDistance(Point2f point1, Point2f point2);
  13. int main(int argc, char** argv) {
  14. Mat src = imread("E:/DCIM/tsnew.jpg");
  15. Mat gray, dst, dst2, result;
  16. //图像缩放
  17. resize(src, gray, Size(0, 0), 0.2, 0.2);
  18. imshow("src", gray);
  19. //灰度图
  20. cvtColor(gray, dst, COLOR_BGRA2GRAY);
  21. //高斯滤波
  22. GaussianBlur(dst, dst, Size(3, 3), 0.5, 0.5);
  23. imshow("gray", dst);
  24. //二值化
  25. threshold(dst, dst2, 0, 255, THRESH_BINARY | THRESH_OTSU);
  26. imshow("thresh", dst2);
  27. //形态学开操作
  28. Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
  29. morphologyEx(dst2, dst2, MORPH_OPEN, kernel);
  30. imshow("morph", dst2);
  31. //Canny边缘检测
  32. Canny(dst2, result, 127, 255, 7, true);
  33. imshow("canny", result);
  34. //查找轮廓
  35. vector<vector<Point>> contours;
  36. findContours(result, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
  37. cout << contours.size() << endl;
  38. vector<vector<Point>> contours_poly(contours.size());
  39. Mat tmpgray;
  40. gray.copyTo(tmpgray);
  41. for (int i = 0; i < contours.size(); ++i) {
  42. //计算轮廓周长,大于图像宽度的才算主体
  43. double dlen = arcLength(contours[i], true);
  44. if (dlen > gray.cols) {
  45. //多边形拟合
  46. approxPolyDP(Mat(contours[i]), contours_poly[i], 10, true);
  47. cout << "当前:" << i << " 点数:" << contours_poly[i].size() << endl;
  48. if (contours_poly[i].size() >= 4) {
  49. //获取最小旋转矩形
  50. RotatedRect rRect = minAreaRect(contours_poly[i]);
  51. Point2f vertices[4];
  52. //重新排序矩形坐标点,按左上,右上,右下,左下顺序
  53. SortRotatedRectPoints(vertices, rRect);
  54. cout << vertices[0] << vertices[1] << vertices[2] << vertices[3] << endl;
  55. //根据获得的4个点画线
  56. for (int k = 0; k < 4; ++k) {
  57. line(gray, vertices[k], vertices[(k + 1) % 4], Scalar(255, 0, 0));
  58. }
  59. //多边形拟合的画出轮廓
  60. drawContours(gray, contours_poly, i, Scalar(0, 0, 255));
  61. //计算四边形的四点坐标
  62. Point2f rPoints[4];
  63. //GetRectPoints(rPoints, rRect.center, contours_poly[i]);
  64. GetPointsFromRect(rPoints, vertices, contours_poly[i]);
  65. for (int k = 0; k < 4; ++k) {
  66. line(gray, rPoints[k], rPoints[(k + 1) % 4], Scalar(255, 255, 255));
  67. }
  68. //根据最小矩形和多边形拟合的最大四个点计算透视变换矩阵
  69. //重新排序多边形拟合的4点
  70. Rect rect = rRect.boundingRect();
  71. rectangle(gray, rect, Scalar(0, 0, 0));
  72. Point2f rectPoint[4];
  73. rectPoint[0] = Point2f(rect.x, rect.y);
  74. rectPoint[1] = Point2f(rect.x + rect.width, rect.y);
  75. rectPoint[2] = Point2f(rect.x + rect.width, rect.y + rect.height);
  76. rectPoint[3] = Point2f(rect.x, rect.y + rect.height);
  77. //vector<Point> vecpt(vertices, vertices + 4);
  78. //GetRectPoints(vertices, rRect.center, vecpt);
  79. //计算透视变换矩阵
  80. Mat warpmatrix = getPerspectiveTransform(rPoints, rectPoint);
  81. Mat resultimg;
  82. //透视变换
  83. warpPerspective(tmpgray, resultimg, warpmatrix, resultimg.size(), INTER_LINEAR);
  84. imshow("resultimg", resultimg);
  85. }
  86. }
  87. }
  88. imshow("src2", gray);
  89. waitKey(0);
  90. return 0;
  91. }
  92. //根据中心点找最远的四个点
  93. void GetRectPoints(Point2f vetPoints[], Point2f center, vector<Point> convex)
  94. {
  95. //定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
  96. float ltdist = 0.0f; //左上的最大距离
  97. float rtdist = 0.0f; //右上的最大距离
  98. float rbdist = 0.0f; //右下的最大距离
  99. float lbdist = 0.0f; //左下的最大距离
  100. for (auto curpoint : convex) {
  101. //计算点的距离
  102. float curdist = CalcPointDistance(center, curpoint);
  103. if (curpoint.x < center.x && curpoint.y < center.y)
  104. {
  105. //判断是否在左上
  106. if (curdist > ltdist) {
  107. ltdist = curdist;
  108. vetPoints[0] = curpoint;
  109. }
  110. }
  111. else if (curpoint.x > center.x && curpoint.y < center.y) {
  112. //判断在右上
  113. if (curdist > rtdist) {
  114. rtdist = curdist;
  115. vetPoints[1] = curpoint;
  116. }
  117. }
  118. else if (curpoint.x > center.x && curpoint.y > center.y) {
  119. //判断在右下
  120. if (curdist > rbdist) {
  121. rbdist = curdist;
  122. vetPoints[2] = curpoint;
  123. }
  124. }
  125. else if (curpoint.x < center.x && curpoint.y > center.y) {
  126. //判断在左下
  127. if (curdist > lbdist) {
  128. lbdist = curdist;
  129. vetPoints[3] = curpoint;
  130. }
  131. }
  132. }
  133. }
  134. //根据最小矩形点找最近的四边形点
  135. //第一参数为输出的点,第二个参数为矩形的4个点,第三个为多边形拟合的点
  136. void GetPointsFromRect(Point2f vetPoints[], Point2f rectPoints[], vector<Point> convex)
  137. {
  138. //定义最远的4个点,0--左上, 1--右上, 2--右下 3--左下
  139. float ltdist = 99999999.9f; //左上的最大距离
  140. float rtdist = 99999999.9f; //右上的最大距离
  141. float rbdist = 99999999.9f; //右下的最大距离
  142. float lbdist = 99999999.9f; //左下的最大距离
  143. float curdist = 0.0f; //当前点的计算距离
  144. for (auto curpoint : convex) {
  145. //计算左上点的距离
  146. curdist = CalcPointDistance(rectPoints[0], curpoint);
  147. if (curdist < ltdist) {
  148. ltdist = curdist;
  149. vetPoints[0] = curpoint;
  150. }
  151. //计算右上角的点距离
  152. curdist = CalcPointDistance(rectPoints[1], curpoint);
  153. if (curdist < rtdist) {
  154. rtdist = curdist;
  155. vetPoints[1] = curpoint;
  156. }
  157. //计算右下角点的距离
  158. curdist = CalcPointDistance(rectPoints[2], curpoint);
  159. if (curdist < rbdist) {
  160. rbdist = curdist;
  161. vetPoints[2] = curpoint;
  162. }
  163. //计算左下角点的距离
  164. curdist = CalcPointDistance(rectPoints[3], curpoint);
  165. if (curdist < lbdist) {
  166. lbdist = curdist;
  167. vetPoints[3] = curpoint;
  168. }
  169. }
  170. }
  171. //重新排序旋转矩形坐标点
  172. void SortRotatedRectPoints(Point2f vetPoints[], RotatedRect rect)
  173. {
  174. rect.points(vetPoints);
  175. cout << vetPoints[0] << vetPoints[1] << vetPoints[2] << vetPoints[3] << endl;
  176. cout << rect.angle << endl;
  177. Point2f curpoint;
  178. //根据Rect的坐标点,Y轴最大的为P[0],p[0]围着center顺时针旋转,
  179. //旋转角度为负的话即是P[0]在左下角,为正P[0]是右下角
  180. //重新排序坐标点
  181. if (rect.angle > 0) {
  182. curpoint = vetPoints[0];
  183. vetPoints[0] = vetPoints[2];
  184. vetPoints[2] = curpoint;
  185. curpoint = vetPoints[1];
  186. vetPoints[1] = vetPoints[3];
  187. vetPoints[3] = curpoint;
  188. }
  189. else if (rect.angle < 0) {
  190. curpoint = vetPoints[0];
  191. vetPoints[0] = vetPoints[1];
  192. vetPoints[1] = vetPoints[2];
  193. vetPoints[2] = vetPoints[3];
  194. vetPoints[3] = curpoint;
  195. }
  196. }
  197. //计算两点间的距离
  198. float CalcPointDistance(Point2f point1, Point2f point2)
  199. {
  200. //计算两个点的Point差值
  201. Point2f tmppoint = point1 - point2;
  202. //利用欧几里德距离计算H
  203. return sqrt(pow(tmppoint.x, 2) + pow(tmppoint.y, 2));
  204. }

format,png

format,png

扫描二维码

获取更多精彩

微卡智享

format,png

「 往期文章 」

.Net5 Windows Form App中Linq的分组查询使用

Android CameraX NDK OpenCV(四)-- 二维码检测与识别

.Net5下定时任务Quartz的使用

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/153494?site
推荐阅读
相关标签
  

闽ICP备14008679号