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.

processing.cpp 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "processing.h"
  2. Processing::Processing(/* args */)
  3. {
  4. }
  5. Processing::~Processing()
  6. {
  7. }
  8. static double angle( Point pt1, Point pt2, Point pt0 )
  9. {
  10. double dx1 = pt1.x - pt0.x;
  11. double dy1 = pt1.y - pt0.y;
  12. double dx2 = pt2.x - pt0.x;
  13. double dy2 = pt2.y - pt0.y;
  14. return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
  15. }
  16. void Processing::processImage(Mat& inputPicture, int thresholdValue, int gaussKernelSize)
  17. {
  18. //Idea here is: Processing module consists of two methods:
  19. // One (this) to do all kinds of stuff to the picture (grayscale conversion, threshold, gauss etc etc)
  20. // And one (the other one) to segment the lines.
  21. // No return value here as the input is passed by reference -> directly modified.
  22. vector<Vec4i> lines;
  23. cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY);
  24. threshold(inputPicture, inputPicture, thresholdValue, 255, THRESH_BINARY);
  25. GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0);
  26. Canny(inputPicture, inputPicture, 50, 200, 3);
  27. HoughLinesP(inputPicture, lines, 1, CV_PI/180, 150, 0, 0);
  28. //Draw lines
  29. for( size_t i = 0; i < lines.size(); i++ )
  30. {
  31. line( inputPicture, Point(lines[i][0], lines[i][1]),
  32. Point( lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
  33. }
  34. vector<VectorOfLines> vectors;
  35. Point point11;
  36. Point point12;
  37. Point point21;
  38. Point point22;
  39. for( size_t i = 0; i < lines.size(); i++ )
  40. {
  41. point11 = Point(lines[i][0], lines[i][1]);
  42. point12 = Point( lines[i][2], lines[i][3]);
  43. float gradient1 = VectorOfLines::calcGradient(point11, point12);
  44. for( size_t j = 0; j < lines.size(); j++ )
  45. {
  46. if(j != i){
  47. point21 = Point(lines[j][0], lines[j][1]);
  48. point22 = Point(lines[j][2], lines[j][3]);
  49. float gradient2 = VectorOfLines::calcGradient(point21, point22);
  50. float gradient12 = VectorOfLines::calcGradient(point12, point21);
  51. if((norm(gradient1 - gradient2) < 0.05) & (norm(gradient1 - gradient12) < 0.05))
  52. {
  53. //To Do: add line between 2 lines
  54. }
  55. }
  56. }
  57. }
  58. imshow("Result", inputPicture);
  59. waitKey(0);
  60. destroyWindow("Result");
  61. for( size_t i = 0; i < lines.size(); i++ )
  62. {
  63. line( inputPicture, Point(lines[i][0], lines[i][1]),
  64. Point( lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
  65. }
  66. }
  67. std::vector<LFRLine> Processing::calculateLineSegments(const Mat& inputPicture)
  68. {
  69. //https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv
  70. return std::vector<LFRLine>();
  71. }