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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "lfr.h"
  2. LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny)
  3. : iAmLooping(false), input(videoHeight, videoWidth), processing(), controlModule(), interpreter(), intersectionHandler()
  4. {
  5. this->iAmLooping = false;
  6. this->thresholdBinary = thresholdBinary;
  7. this->gaussKernelSize = gaussKernelSize;
  8. this->thresholdCanny1 = thresholdCanny1;
  9. this->thresholdCanny2 = thresholdCanny2;
  10. this->apertureSizeCanny = apertureSizeCanny;
  11. this->videoFlag = false;
  12. this->saveOutputFlag = false;
  13. this->outputFileName = "";
  14. }
  15. LFR::~LFR()
  16. {
  17. if(iAmLooping)
  18. {
  19. this->endLoop();
  20. }
  21. }
  22. void LFR::loop()
  23. {
  24. if(this->videoFlag) {namedWindow("Display window");}
  25. while(iAmLooping)
  26. {
  27. Mat originalImage = input.readWebcam();
  28. Point roiOrigin(0, int(originalImage.rows*(7.5/12.0)));
  29. Rect roi(roiOrigin.x, roiOrigin.y, originalImage.cols, originalImage.rows/12);
  30. Mat processedImage = originalImage(roi);
  31. processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize, this->thresholdCanny1, thresholdCanny2, this->apertureSizeCanny);
  32. FrameData data = processing.calculateLineSegments(processedImage, roi);
  33. processing.filterReflections(data);
  34. this->provideOutput(originalImage, data, roi);
  35. }
  36. if(this->videoFlag) {destroyWindow("Display window");}
  37. input.freeWebcam();
  38. }
  39. void LFR::startLoop()
  40. {
  41. iAmLooping = true;
  42. this->loopThread=thread(&LFR::loop, this);
  43. }
  44. void LFR::endLoop()
  45. {
  46. iAmLooping = false;
  47. this->loopThread.join();
  48. return;
  49. }
  50. void LFR::provideOutput(Mat image, const FrameData& frameData, const Rect& roi)
  51. {
  52. for(int i = 0; i < frameData.contours.size(); i++)
  53. {
  54. drawContours(image, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
  55. rectangle(image, frameData.boundingBoxes[i], Scalar(0,255,0));
  56. Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
  57. rectangle(image, center, Scalar(0,0,255));
  58. Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
  59. rectangle(image, leftRect, Scalar(0,0,255));
  60. }
  61. if(this->videoFlag)
  62. {
  63. imshow("Display window", image);
  64. char c = (char)waitKey(25);
  65. }
  66. if (this->saveOutputFlag && !(this->outputFileName.empty()))
  67. {
  68. imwrite(this->outputFileName, image);
  69. }
  70. }