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 2.1KB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "processing.h"
  2. Processing::Processing(/* args */)
  3. {
  4. }
  5. Processing::~Processing()
  6. {
  7. }
  8. static double angle( Point pt1, Point pt2, Point pt0 )
  9. {
  10. double dx1 = pt1.x - pt0.x;
  11. double dy1 = pt1.y - pt0.y;
  12. double dx2 = pt2.x - pt0.x;
  13. double dy2 = pt2.y - pt0.y;
  14. return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
  15. }
  16. void Processing::processImage(Mat& inputPicture, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny)
  17. {
  18. //Idea here is: Processing module consists of two methods:
  19. // One (this) to do all kinds of stuff to the picture (grayscale conversion, threshold, gauss etc etc)
  20. // And one (the other one) to segment the lines.
  21. // No return value here as the input is passed by reference -> directly modified.
  22. cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY);
  23. GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0);
  24. threshold(inputPicture, inputPicture, thresholdBinary, 255, THRESH_BINARY);
  25. //Perform a opening
  26. Mat kernel(5,5, CV_8UC1,1);
  27. morphologyEx(inputPicture, inputPicture, 2, kernel);
  28. }
  29. FrameData Processing::calculateLineSegments(const Mat& inputPicture, const cv::Rect& roi)
  30. {
  31. FrameData data;
  32. cv::findContours(inputPicture, data.contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
  33. //Delete the areas that are too small
  34. auto iterator = data.contours.begin();
  35. while(iterator != data.contours.end())
  36. {
  37. if (contourArea(*iterator) < 3500)
  38. {
  39. iterator = data.contours.erase(iterator);
  40. }
  41. else
  42. {
  43. Rect boundingBox = boundingRect(*iterator);
  44. boundingBox.x += roi.x;
  45. boundingBox.y += roi.y;
  46. data.boundingBoxes.push_back(boundingBox);
  47. data.middlePoints.push_back(Point(boundingBox.x+boundingBox.width/2, boundingBox.y+boundingBox.height/2));
  48. data.leftEdges.push_back(Point(boundingBox.x, boundingBox.y+boundingBox.height/2));
  49. ++iterator;
  50. }
  51. }
  52. return data;
  53. }