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.

lfr.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "lfr.h"
  2. #define right false
  3. #define left true
  4. LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize)
  5. : iAmLooping(false), input(videoHeight, videoWidth), processing(), controlModule(), interpreter(), intersectionHandler()
  6. {
  7. this->iAmLooping = false;
  8. this->thresholdBinary = thresholdBinary;
  9. this->gaussKernelSize = gaussKernelSize;
  10. this->videoFlag = false;
  11. this->saveOutputFlag = false;
  12. this->outputFileName = "";
  13. }
  14. LFR::~LFR()
  15. {
  16. if(iAmLooping)
  17. {
  18. this->endLoop();
  19. }
  20. }
  21. void LFR::loop()
  22. {
  23. if(this->videoFlag) {namedWindow("Display window");}
  24. while(iAmLooping)
  25. {
  26. Mat originalImage = input.readWebcam();
  27. Point roiOrigin(0, int(originalImage.rows*(3.25/6.0)));
  28. Rect roi(roiOrigin.x, roiOrigin.y, originalImage.cols, originalImage.rows/6);
  29. //Mat processedImage = originalImage(roi);
  30. Mat processedImage = originalImage;
  31. processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize);
  32. //processedImage = processedImage(roi);
  33. FrameData data = processing.calculateLineSegments(processedImage, roi);
  34. processing.filterReflections(data);
  35. processing.calcAngles(data, originalImage.cols, originalImage.rows, left);
  36. this->provideOutput(originalImage, processedImage, data, roi);
  37. }
  38. if(this->videoFlag) {destroyWindow("Display window");}
  39. input.freeWebcam();
  40. }
  41. void LFR::startLoop()
  42. {
  43. iAmLooping = true;
  44. this->loopThread=thread(&LFR::loop, this);
  45. }
  46. void LFR::endLoop()
  47. {
  48. iAmLooping = false;
  49. this->loopThread.join();
  50. return;
  51. }
  52. void LFR::provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi)
  53. {
  54. for(int i = 0; i < frameData.contours.size(); i++)
  55. {
  56. drawContours(originalImage, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
  57. rectangle(originalImage, frameData.boundingBoxes[i], Scalar(0,255,0));
  58. Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
  59. rectangle(originalImage, center, Scalar(0,0,255));
  60. Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
  61. rectangle(originalImage, leftRect, Scalar(0,0,255));
  62. }
  63. if(frameData.contours.size() > 0)
  64. {
  65. //Draw the Arrow for the check of the angle
  66. int length = 100;
  67. Point P1 = frameData.middlePoints[frameData.index];
  68. Point P2;
  69. P2.x = (int)round(P1.x + length * cos(frameData.angle * CV_PI / 180.0));
  70. P2.y = (int)round(P1.y + length * sin(frameData.angle * CV_PI / 180.0));
  71. cv::arrowedLine(originalImage, P1, P2, Scalar(0,0,255), 2, 8);
  72. }
  73. if(this->videoFlag)
  74. {
  75. imshow("Display window", originalImage);
  76. imshow("processed:", processedImage);
  77. char c = (char)waitKey(25);
  78. }
  79. if (this->saveOutputFlag && !(this->outputFileName.empty()))
  80. {
  81. imwrite(this->outputFileName, originalImage);
  82. }
  83. }