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.

spielwiese.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //#include <opencv2/core/utils/logger.hpp>
  2. #include <opencv2/opencv.hpp>
  3. #include <iostream>
  4. #include <direct.h>
  5. #include <input.h>
  6. #include <processing.h>
  7. #include <control_module.h>
  8. #include <interpreter.h>
  9. #include <intersection_handler.h>
  10. void sweep_em_all(int thresholdBinary, int videoHeight, int videoWidth, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny)
  11. {
  12. Input input(videoHeight, videoWidth);
  13. Processing processing;
  14. namedWindow("Display window");
  15. while(true)
  16. {
  17. Mat image = input.readFile("C:\\Users\\User\\Desktop\\Studium\\02_Master_MSY\\2. Semester Winter 22 23\\Projekt\\Line-Following-Robot\\Test_data");
  18. Mat processedImage = image;
  19. processing.processImage(processedImage, thresholdBinary, gaussKernelSize, thresholdCanny1, thresholdCanny2 ,apertureSizeCanny);
  20. std::vector<Vec4i> lines = processing.calculateLineSegments(processedImage);
  21. for( size_t i = 0; i < lines.size(); i++ )
  22. {
  23. line( image, Point(lines[i][0], lines[i][1]),
  24. Point( lines[i][2], lines[i][3]), (0,0,255), 1, 8 );
  25. }
  26. imshow("Display window", image);
  27. char c = (char)waitKey(1);
  28. }
  29. destroyWindow("Display window");
  30. }
  31. void in_depth_processing_chain(int thresholdBinary, int videoHeight, int videoWidth, int gaussKernelSize, int thresholdCanny1, int thresholdCanny2, int apertureSizeCanny)
  32. {
  33. Input input(videoHeight, videoWidth);
  34. cv::String outputFolder = "C:\\Users\\tim-z\\Desktop\\temp";
  35. cv::String inputFolder = "C:\\Users\\tim-z\\Desktop\\Studium\\02_Master MSY\\2. Semester Winter 2022 2023\\Projekt\\Repo\\Line-Following-Robot\\AutonomousMode\\Test_data";
  36. std::vector<cv::String> filenames;
  37. cv::glob(inputFolder, filenames);
  38. //filenames.begin(), filenames.end()
  39. int i = 0;
  40. for(std::vector<cv::String>::iterator it = filenames.begin(); it != filenames.end(); it++)
  41. {
  42. std::string current_output = outputFolder + "\\" + to_string(i);
  43. std::cout << current_output << std::endl;
  44. const char* current_output_char = current_output.c_str();
  45. _mkdir(current_output_char);
  46. std::string inputFile = inputFolder + "\\image" + to_string(i+1) + ".jpeg";
  47. Mat original_image = input.readFile(inputFile);
  48. imwrite(current_output + "\\00_input.jpg", original_image);
  49. Point roiOrigin(0, original_image.rows*(7.5/12.0));
  50. Rect roi = Rect(roiOrigin.x, roiOrigin.y, original_image.cols, original_image.rows/12);
  51. Mat image = original_image(roi);
  52. imwrite(current_output + "\\01_roi.jpg", image);
  53. cvtColor(image, image, COLOR_BGR2GRAY);
  54. imwrite(current_output + "\\02_color_convert.jpg", image);
  55. GaussianBlur(image, image, Size(gaussKernelSize, gaussKernelSize), 0);
  56. imwrite(current_output + "\\03_gauss.jpg", image);
  57. threshold(image, image, thresholdBinary, 255, THRESH_BINARY);
  58. imwrite(current_output + "\\04_threshold.jpg", image);
  59. // Opening (reduces noise)
  60. Mat kernel(5,5, CV_8UC1,1);
  61. morphologyEx(image, image, 2, kernel);
  62. imwrite(current_output + "\\05_opening.jpg", image);
  63. //Canny(image, image, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  64. //imwrite(outputFolder + "\\06_canny.jpg", image);
  65. vector<vector<Point> > contours;
  66. vector<Vec4i> hierarchy;
  67. vector<Rect> vectorOfRects;
  68. vector<Point> vectorOfLeftEdges;
  69. findContours(image,contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
  70. int amountOfValidRects = 0;
  71. for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
  72. {
  73. double a = contourArea( contours[i],false); // Find the area of contour
  74. if(a > 3500)
  75. {
  76. drawContours(original_image, contours, i, Scalar(0,255,255), 1, 8, hierarchy, 0, roiOrigin);
  77. Rect currentBoundingRect = boundingRect(contours[i]);
  78. //Handle roi offset:
  79. currentBoundingRect.x += roiOrigin.x;
  80. currentBoundingRect.y += roiOrigin.y;
  81. vectorOfRects.push_back(currentBoundingRect);
  82. rectangle(original_image, currentBoundingRect, Scalar(0,255,0));
  83. // get mid-point of rect
  84. Point midRect = Point(currentBoundingRect.x+currentBoundingRect.width/2, currentBoundingRect.y+currentBoundingRect.height/2);
  85. // Draw middle as small rect instead of circle because for whatever reasons drawing a circle doesnt work.
  86. Rect testRect(Point(midRect.x-2, midRect.y-2), Point(midRect.x+2, midRect.y+2));
  87. rectangle(original_image, testRect, Scalar(0,0,255));
  88. // get the left edge of rect
  89. // used as offset as raspicam is not
  90. // mounted on mid of regbot
  91. Point leftEdge(currentBoundingRect.x, currentBoundingRect.y+currentBoundingRect.height/2);
  92. vectorOfLeftEdges.push_back(leftEdge);
  93. testRect = Rect(Point(leftEdge.x-2, leftEdge.y-2), Point(leftEdge.x+2, leftEdge.y+2));
  94. rectangle(original_image, testRect, Scalar(0,0,255));
  95. amountOfValidRects++;
  96. }
  97. }
  98. imwrite(current_output + "\\06_contours.jpg", original_image);
  99. i++;
  100. }
  101. }
  102. int main(void)
  103. {
  104. //Disable opencv logging messages
  105. //cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING);
  106. const int thresholdBinary = 140;
  107. const int videoHeight = 720;
  108. const int videoWidth = 960;
  109. const int gaussKernelSize = 11;
  110. const int thresholdCanny1 = 50;
  111. const int thresholdCanny2 = 100;
  112. const int apertureSizeCanny = 3;
  113. //sweep_em_all(thresholdBinary, videoHeight, videoWidth, gaussKernelSize, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  114. in_depth_processing_chain(thresholdBinary, videoHeight, videoWidth, gaussKernelSize, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  115. }