Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

processing.cpp 7.3KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "processing.h"
  2. Processing::Processing(/* args */)
  3. {
  4. }
  5. Processing::~Processing()
  6. {
  7. }
  8. void Processing::filterReflections(FrameData& frameData)
  9. {
  10. //Try to filter reflections from the frame data.
  11. std::vector<int> indicesToDelete;
  12. for(int i = 0; i < frameData.contours.size(); i++)
  13. {
  14. // First approach: correct contours nearly fill their bounding box.
  15. // So delete all contours with an area smaller than 75% of their bounding box
  16. double contourArea = cv::contourArea(frameData.contours[i], false);
  17. double boundingBoxArea = double(frameData.boundingBoxes[i].width*frameData.boundingBoxes[i].height);
  18. double minRatio = 0.75;
  19. if(boundingBoxArea/contourArea < minRatio)
  20. {
  21. indicesToDelete.push_back(i);
  22. continue;
  23. }
  24. // Second approach: The contour should be nearly convex
  25. // So delete all contours with an area smaller than 95% of their convex hull.
  26. std::vector<Point> hull;
  27. cv::convexHull(frameData.contours[i], hull);
  28. double hullArea = cv::contourArea(hull, false);
  29. double minRatioHull = 0.95;
  30. if(contourArea/boundingBoxArea < minRatio)
  31. {
  32. indicesToDelete.push_back(i);
  33. continue;
  34. }
  35. }
  36. //reverse the vector with the indices so the order isn't messed up when deleting:
  37. std::reverse(indicesToDelete.begin(), indicesToDelete.end());
  38. for(int index : indicesToDelete)
  39. {
  40. frameData.boundingBoxes.erase(frameData.boundingBoxes.begin() + index);
  41. frameData.contours.erase(frameData.contours.begin() + index);
  42. frameData.leftEdges.erase(frameData.leftEdges.begin() + index);
  43. frameData.middlePoints.erase(frameData.middlePoints.begin() + index);
  44. }
  45. return;
  46. }
  47. bool Processing::checkIntersection(const std::vector<std::vector<Point>>& lines, const std::vector<Point>& outline)
  48. {
  49. /* TODO:
  50. * Was ich mir hier vorstelle:
  51. * Wir haben die konturen und die hough linien
  52. * Konturen haben schwäche mit Reflektionen, hough linien sind unvollständig.
  53. * Irgendwie sollten die beiden Datensätze gemerged werden, um das "gute" aus beiden zu vereinen.
  54. * Evtl in richtung "Schau den Bereich der Contour (+ Toleranz?) im hough bild an. Wenn keine Linie im Bereich ist, ist die Contour eine Reflektion"
  55. *
  56. * Aktuell nehmen wir die abkürzung, die segmente rauszuwerfen, sobald keine Linie im Bild ist.
  57. * Funktioniert für unsere Daten auch ziemlich gut
  58. */
  59. if(lines.size() == 0)
  60. {
  61. return false;
  62. }
  63. return true;
  64. }
  65. void Processing::calcAngles(FrameData &data, int imageColums, int imageRows, bool turnLeft)
  66. {
  67. if(data.contours.size() > 0)
  68. {
  69. int index;
  70. // get the most left/right contour
  71. int leftmostEdge = imageColums;
  72. int rightmostEdge = 0;
  73. for (int i = 0; i < data.leftEdges.size(); i++)
  74. {
  75. int edge = data.leftEdges[i].x;
  76. if (edge <= leftmostEdge && turnLeft)
  77. {
  78. leftmostEdge = edge;
  79. index = i;
  80. }
  81. else if(edge >= rightmostEdge && !turnLeft)
  82. {
  83. rightmostEdge = edge;
  84. index = i;
  85. }
  86. }
  87. // Find the Top-Left and Bottom-Left Point of the Contour
  88. std::vector<cv::Point> leftMostContour = data.contours[index];
  89. /*
  90. Not going to dive into that part to fix it.
  91. Trying to use centroid instead.
  92. int xleftBottom = imageColums;
  93. int xleftTop = imageColums;
  94. int yBottom = data.boundingBoxes[index].y;
  95. int yTop = data.boundingBoxes[index].height;
  96. for (int i = 0; i < leftMostContour.size(); i++)
  97. {
  98. if(leftMostContour[i].y == 0 && leftMostContour[i].x < xleftBottom)
  99. {
  100. xleftBottom = leftMostContour[i].x;
  101. }
  102. else if(leftMostContour[i].y > yTop -4 && leftMostContour[i].x < xleftTop)
  103. {
  104. xleftTop = leftMostContour[i].x;
  105. }
  106. }
  107. // calculate angle
  108. int deltaX = abs(xleftBottom - xleftTop);
  109. int deltaY = yTop;
  110. angle = Calcs::calcAngle(deltaX, deltaY);
  111. */
  112. //Get the contours center using moments:
  113. cv::Moments moments=cv::moments(leftMostContour);
  114. LFRPoint contourCenter(moments.m10/moments.m00, moments.m01/moments.m00);
  115. LFRPoint imageCenter(double(imageColums)/2.0, double(imageRows)/2.0);
  116. LFRPoint focusPoint(imageCenter.x, imageCenter.y+1000.0);
  117. LFRVector a(imageCenter-focusPoint);
  118. LFRVector b(contourCenter-focusPoint);
  119. double angle = a.angle(b);
  120. //Write to Data
  121. data.angle = angle;
  122. data.index = index;
  123. }
  124. }
  125. void Processing::processImage(Mat& inputPicture, int thresholdBinary, int gaussKernelSize)
  126. {
  127. //Idea here is: Processing module consists of two methods:
  128. // One (this) to do all kinds of stuff to the picture (grayscale conversion, threshold, gauss etc etc)
  129. // And one (the other one) to segment the lines.
  130. // No return value here as the input is passed by reference -> directly modified.
  131. cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY);
  132. GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0);
  133. threshold(inputPicture, inputPicture, thresholdBinary, 255, THRESH_BINARY);
  134. //Perform an opening
  135. Mat kernel(5,5, CV_8UC1,1);
  136. morphologyEx(inputPicture, inputPicture, 2, kernel);
  137. }
  138. FrameData Processing::calculateLineSegments(Mat& inputPicture, const cv::Rect& roi)
  139. {
  140. FrameData data;
  141. Mat inputPictureRoi = inputPicture(roi);
  142. cv::findContours(inputPictureRoi, data.contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
  143. vector<Vec4i> lines;
  144. std::vector<std::vector<Point>> houghLines;
  145. Canny(inputPicture, inputPicture, 50, 100, 3);
  146. HoughLinesP(inputPicture, lines, 1, CV_PI/180, 100, 30, 150);
  147. //Draw lines
  148. inputPicture = Mat::zeros(inputPicture.size().height, inputPicture.size().width, CV_8UC1);
  149. for( size_t i = 0; i < lines.size(); i++ )
  150. {
  151. line( inputPicture, Point(lines[i][0], lines[i][1]), Point(lines[i][2], lines[i][3]), Scalar(255,255,255), 3, 8 );
  152. std::vector<Point> line;
  153. line.push_back(Point(lines[i][0], lines[i][1]));
  154. line.push_back(Point(lines[i][2], lines[i][3]));
  155. houghLines.push_back(line);
  156. }
  157. //Delete the areas that are too small
  158. auto iterator = data.contours.begin();
  159. while(iterator != data.contours.end())
  160. {
  161. if (contourArea(*iterator) < 7000)
  162. {
  163. iterator = data.contours.erase(iterator);
  164. }
  165. else if (!checkIntersection(houghLines, *iterator))
  166. {
  167. iterator = data.contours.erase(iterator);
  168. }
  169. else
  170. {
  171. //Check for intersection with lines:
  172. Rect boundingBox = boundingRect(*iterator);
  173. boundingBox.x += roi.x;
  174. boundingBox.y += roi.y;
  175. data.boundingBoxes.push_back(boundingBox);
  176. data.middlePoints.push_back(Point(boundingBox.x+boundingBox.width/2, boundingBox.y+boundingBox.height/2));
  177. data.leftEdges.push_back(Point(boundingBox.x, boundingBox.y+boundingBox.height/2));
  178. ++iterator;
  179. }
  180. }
  181. return data;
  182. }