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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. std::string outputFolder = "C:\\Users\\User\\Desktop\\temp";
  35. std::string inputFolder = "C:\\Users\\User\\Desktop\\Studium\\02_Master_MSY\\2. Semester Winter 22 23\\Projekt\\Line-Following-Robot\\AutonomousMode\\Test_data";
  36. std::vector<std::string> filenames;
  37. cv::glob(inputFolder, filenames);
  38. //filenames.begin(), filenames.end()
  39. int i = 0;
  40. for(std::vector<std::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. Rect roi = Rect(0, original_image.rows*(7.5/12.0), original_image.cols, original_image.rows/12);
  50. Mat image = original_image(roi);
  51. imwrite(current_output + "\\01_roi.jpg", image);
  52. cvtColor(image, image, COLOR_BGR2GRAY);
  53. imwrite(current_output + "\\02_color_convert.jpg", image);
  54. GaussianBlur(image, image, Size(gaussKernelSize, gaussKernelSize), 0);
  55. imwrite(current_output + "\\03_gauss.jpg", image);
  56. threshold(image, image, thresholdBinary, 255, THRESH_BINARY);
  57. imwrite(current_output + "\\04_threshold.jpg", image);
  58. // Opening (reduces noise)
  59. Mat kernel(5,5, CV_8UC1,1);
  60. morphologyEx(image, image, 2, kernel);
  61. imwrite(current_output + "\\05_opening.jpg", image);
  62. //Canny(image, image, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  63. //imwrite(outputFolder + "\\06_canny.jpg", image);
  64. vector<vector<Point> > contours;
  65. vector<Vec4i> hierarchy;
  66. findContours(image,contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
  67. for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
  68. {
  69. double a = contourArea( contours[i],false); // Find the area of contour
  70. if(a > 3500)
  71. {
  72. drawContours(original_image, contours, i, Scalar(0,255,255), 1, 8, hierarchy, 0, Point(0,original_image.rows*(7.5/12.0)));
  73. }
  74. }
  75. imwrite(current_output + "\\06_contours.jpg", original_image);
  76. i++;
  77. }
  78. }
  79. int main(void)
  80. {
  81. //Disable opencv logging messages
  82. cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING);
  83. const int thresholdBinary = 140;
  84. const int videoHeight = 720;
  85. const int videoWidth = 960;
  86. const int gaussKernelSize = 11;
  87. const int thresholdCanny1 = 50;
  88. const int thresholdCanny2 = 100;
  89. const int apertureSizeCanny = 3;
  90. //sweep_em_all(thresholdBinary, videoHeight, videoWidth, gaussKernelSize, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  91. in_depth_processing_chain(thresholdBinary, videoHeight, videoWidth, gaussKernelSize, thresholdCanny1, thresholdCanny2, apertureSizeCanny);
  92. }