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