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

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "lfr.h"
  2. #define right false
  3. #define left true
  4. LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, ExceptionCallback cb):
  5. stop(false),
  6. input(videoHeight, videoWidth),
  7. processing(),
  8. controlModule(),
  9. interpreter(),
  10. intersectionHandler(),
  11. cb(cb)
  12. {
  13. this->thresholdBinary = thresholdBinary;
  14. this->gaussKernelSize = gaussKernelSize;
  15. }
  16. LFR::~LFR()
  17. {
  18. endLoop();
  19. thread->join();
  20. }
  21. void LFR::removeListener(LFR::ListenerKey key)
  22. {
  23. std::lock_guard<std::mutex> lock(mutex);
  24. auto it = std::find_if(std::begin(listeners), std::end(listeners), [&](auto const &val){
  25. return val.first == key;
  26. });
  27. if(it != std::end(listeners))
  28. {
  29. listeners.erase(it);
  30. }
  31. }
  32. void LFR::addListener(LFR::ListenerCallback cb, LFR::ListenerKey key)
  33. {
  34. std::lock_guard<std::mutex> lock(mutex);
  35. listeners.emplace_back(key, std::move(cb));
  36. }
  37. void LFR::setStop(bool val)
  38. {
  39. std::lock_guard<std::mutex> lock(mutex);
  40. stop = val;
  41. }
  42. void LFR::createThread()
  43. {
  44. thread = std::make_unique<std::thread>([this](){
  45. while(true)
  46. {
  47. LFR_Result result;
  48. if(!stop && !listeners.empty())
  49. {
  50. try
  51. {
  52. std::lock_guard<std::mutex> lock(mutex);
  53. Mat originalImage = input.readWebcam();
  54. Point roiOrigin(0, int(originalImage.rows*(3.25/6.0)));
  55. Rect roi(roiOrigin.x, roiOrigin.y, originalImage.cols, originalImage.rows/6);
  56. Mat processedImage = originalImage;
  57. processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize);
  58. FrameData data = processing.calculateLineSegments(processedImage, roi);
  59. processing.filterReflections(data);
  60. processing.calcAngles(data, originalImage.cols, originalImage.rows, left);
  61. processedImage = provideOutput(originalImage, processedImage, data, roi);
  62. result.rawImage = originalImage;
  63. result.processedImage = processedImage;
  64. result.data = data;
  65. }
  66. catch(std::exception const &ex)
  67. {
  68. cb(ex);
  69. }
  70. //Invoke the callback method (ListenerPair second -> ListenerCallback)
  71. for(auto &val : listeners)
  72. {
  73. val.second(result);
  74. }
  75. }
  76. else
  77. {
  78. break;
  79. }
  80. }
  81. });
  82. }
  83. void LFR::startLoop()
  84. {
  85. if(thread)
  86. {
  87. //Restart thread if it is running
  88. setStop(true);
  89. thread->join();
  90. setStop(false);
  91. }
  92. createThread();
  93. }
  94. void LFR::endLoop()
  95. {
  96. setStop(true);
  97. }
  98. cv::Mat LFR::provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi)
  99. {
  100. for(int i = 0; i < frameData.contours.size(); i++)
  101. {
  102. drawContours(originalImage, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
  103. rectangle(originalImage, frameData.boundingBoxes[i], Scalar(0,255,0));
  104. Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
  105. rectangle(originalImage, center, Scalar(0,0,255));
  106. Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
  107. rectangle(originalImage, leftRect, Scalar(0,0,255));
  108. }
  109. if(frameData.contours.size() > 0)
  110. {
  111. //Draw the Arrow for the check of the angle
  112. int length = 100;
  113. Point P1 = frameData.middlePoints[frameData.index];
  114. Point P2;
  115. P2.x = (int)round(P1.x + length * cos(frameData.angle * CV_PI / 180.0));
  116. P2.y = (int)round(P1.y + length * sin(frameData.angle * CV_PI / 180.0));
  117. cv::arrowedLine(originalImage, P1, P2, Scalar(0,0,255), 2, 8);
  118. }
  119. return originalImage;
  120. }