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.2KB

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