OpenCV怎么通过透视变换实现矫正图像

其他教程   发布日期:2023年07月11日   浏览次数:463

这篇“OpenCV怎么通过透视变换实现矫正图像”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“OpenCV怎么通过透视变换实现矫正图像”文章吧。

1、概述

案例:使用OpenCV将一张折射的图片给矫正过来

实现步骤:

1.载入图像

2.图像灰度化

3.二值分割

4.形态学操作去除噪点

5.轮廓发现

6.使用霍夫直线检测,检测上下左右四条直线(有可能是多条,但是无所谓)

7.绘制出直线

8.寻找与定位上下左右是条直线

9.拟合四条直线方程

10.计算四条直线的交点,ps:这四个交点其实就是我们最终要寻找的,用于透视变换使用的

11.进行透视变换

12.输出透视变换的结果

说明:

解释一下为啥是上面那些步骤。

1.其实我们的最终目的是通过透视矩阵getPerspectiveTransform+透视变换warpPerspective来完成图像的矫正

2.但是getPerspectiveTransform需要两个参数,输入矩阵参数和目标矩阵参数。

3.由于输入矩阵参数就是原图像是个角的顶点,由于我们没有所以要求出来

4.所以我们以上的所有步骤都是为11、12步打基础的

ps:核心就是利用透视矩阵做透视变换

重点:

1.直线方程y=kx+c

2.如果两条直线有交点,则必有k1x1+c1=k2x2+c2

2、代码演示

  1. //【1】载入图像
  2. Mat src = imread(filePath);
  3. if(src.empty()){
  4. qDebug()<<"图片为空";
  5. return;
  6. }
  7. imshow("src",src);
  8. //【2】图像灰度化
  9. Mat gray;
  10. cvtColor(src,gray,COLOR_BGR2GRAY);
  11. //【3】执行二值分割
  12. threshold(gray,gray,0,255,THRESH_BINARY_INV|THRESH_OTSU);
  13. imshow("threshold",gray);
  14. //【4】执行形态学开操作去除图像中的造点
  15. Mat kernel = getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1));
  16. morphologyEx(gray,gray,MORPH_CLOSE,kernel,Point(-1,-1),3);
  17. imshow("morphologyEx",gray);
  18. //【5】轮廓发现
  19. bitwise_not(gray,gray);
  20. imshow("bitwise_not",gray);
  21. vector<vector<Point>> contours;
  22. vector<Vec4i> hier;
  23. RNG rng(12345);
  24. findContours(gray,contours,hier,RETR_TREE,CHAIN_APPROX_SIMPLE);
  25. Mat colorImage = Mat::zeros(gray.size(),CV_8UC3);
  26. for(size_t i = 0;i<contours.size();i++){
  27. Rect rect = boundingRect(contours[i]);
  28. //过滤目标轮廓
  29. if(rect.width<src.cols-5&&rect.height<src.rows-5&&rect.width>src.cols/2){
  30. drawContours(colorImage,contours,i,Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),1);
  31. }
  32. }
  33. imshow("findContours",colorImage);
  34. //【6】使用霍夫直线检测
  35. vector<Vec4i> lines;
  36. cvtColor(colorImage,colorImage,COLOR_BGR2GRAY);
  37. kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
  38. dilate(colorImage,colorImage,kernel,Point(-1,-1),1);
  39. imshow("colorImage_gray",colorImage);
  40. int accu = min(src.cols*0.5, src.rows*0.5);
  41. HoughLinesP(colorImage,lines,1,CV_PI/180,accu,accu,0);
  42. //【7】绘制出直线
  43. Mat lineColorImage = Mat::zeros(gray.size(),CV_8UC3);
  44. qDebug()<<"line count:"<<lines.size();
  45. for(size_t i = 0;i<lines.size();i++){
  46. Vec4i ll = lines[i];
  47. line(lineColorImage,Point(ll[0],ll[1]),Point(ll[2],ll[3]),Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),2,LINE_8);
  48. }
  49. imshow("lines",lineColorImage);
  50. //【8】寻找与定位上下左右四条直线
  51. int deltah = 0;
  52. int width = src.cols;
  53. int height = src.rows;
  54. Vec4i topLine, bottomLine;
  55. Vec4i leftLine, rightLine;
  56. for(size_t i=0;i<lines.size();i++){
  57. Vec4i ln = lines[i];
  58. deltah = abs(ln[3]-ln[1]);//直线高度
  59. if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1) {
  60. if (topLine[3] > ln[3] && topLine[3]>0) {
  61. topLine = lines[i];
  62. } else {
  63. topLine = lines[i];
  64. }
  65. }
  66. if (ln[3] > height / 2.0 && ln[1] > height / 2.0 && deltah < accu - 1) {
  67. bottomLine = lines[i];
  68. }
  69. if (ln[0] < width / 2.0 && ln[2] < width/2.0) {
  70. leftLine = lines[i];
  71. }
  72. if (ln[0] > width / 2.0 && ln[2] > width / 2.0) {
  73. rightLine = lines[i];
  74. }
  75. }
  76. //直线方程y=kx+c
  77. // 【9】拟合四条直线方程
  78. float k1, c1;
  79. k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
  80. c1 = topLine[1] - k1*topLine[0];
  81. float k2, c2;
  82. k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
  83. c2 = bottomLine[1] - k2*bottomLine[0];
  84. float k3, c3;
  85. k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
  86. c3 = leftLine[1] - k3*leftLine[0];
  87. float k4, c4;
  88. k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
  89. c4 = rightLine[1] - k4*rightLine[0];
  90. // 【10】四条直线交点,其实最终的目的就是找这是条直线的交点
  91. Point p1; // 左上角
  92. p1.x = static_cast<int>((c1 - c3) / (k3 - k1));
  93. p1.y = static_cast<int>(k1*p1.x + c1);
  94. Point p2; // 右上角
  95. p2.x = static_cast<int>((c1 - c4) / (k4 - k1));
  96. p2.y = static_cast<int>(k1*p2.x + c1);
  97. Point p3; // 左下角
  98. p3.x = static_cast<int>((c2 - c3) / (k3 - k2));
  99. p3.y = static_cast<int>(k2*p3.x + c2);
  100. Point p4; // 右下角
  101. p4.x = static_cast<int>((c2 - c4) / (k4 - k2));
  102. p4.y = static_cast<int>(k2*p4.x + c2);
  103. // 显示四个点坐标
  104. circle(lineColorImage, p1, 2, Scalar(255, 0, 0), 2, 8, 0);
  105. circle(lineColorImage, p2, 2, Scalar(255, 0, 0), 2, 8, 0);
  106. circle(lineColorImage, p3, 2, Scalar(255, 0, 0), 2, 8, 0);
  107. circle(lineColorImage, p4, 2, Scalar(255, 0, 0), 2, 8, 0);
  108. line(lineColorImage, Point(topLine[0], topLine[1]), Point(topLine[2], topLine[3]), Scalar(0, 255, 0), 2, 8, 0);
  109. imshow("four corners", lineColorImage);
  110. // 【11】透视变换
  111. vector<Point2f> src_corners(4);
  112. src_corners[0] = p1;
  113. src_corners[1] = p2;
  114. src_corners[2] = p3;
  115. src_corners[3] = p4;
  116. vector<Point2f> dst_corners(4);
  117. dst_corners[0] = Point(0, 0);
  118. dst_corners[1] = Point(width, 0);
  119. dst_corners[2] = Point(0, height);
  120. dst_corners[3] = Point(width, height);
  121. // 【12】获取透视变换矩阵,并最终显示变换后的结果
  122. Mat resultImage;
  123. Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
  124. warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR);
  125. imshow("Final Result", resultImage);

以上就是OpenCV怎么通过透视变换实现矫正图像的详细内容,更多关于OpenCV怎么通过透视变换实现矫正图像的资料请关注九品源码其它相关文章!