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.

input.cpp 2.0KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "input.h"
  2. // TODO: Wenn ihr in die Zeile den Pfad zum Testvideo statt der 0 packt, benmutzt er das Testvideo.
  3. Input::Input(int videoHeight, int videoWidth) : cap("C:\\Line-Following-Robot\\AutonomousMode\\Test_data\\video1.h264"), videoHeight(videoHeight), videoWidth(videoWidth)//Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
  4. //Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
  5. {
  6. std::unique_lock<std::mutex> lock(mtx);
  7. this->cap.set(CAP_PROP_FRAME_HEIGHT, videoHeight);
  8. this->cap.set(CAP_PROP_FRAME_WIDTH, videoWidth);
  9. }
  10. Input::~Input()
  11. {
  12. this->freeWebcam();
  13. }
  14. Mat Input::readFile(String filePath)
  15. {
  16. std::unique_lock<std::mutex> lock(mtx);
  17. std::srand(static_cast<unsigned int>(std::time(0)));
  18. // Read all .jpg files from the specified folder
  19. cv::String folder = filePath;
  20. std::vector<cv::String> filenames;
  21. cv::glob(folder, filenames);
  22. // Random shuffle
  23. std::random_device rd;
  24. std::mt19937 g(rd());
  25. std::shuffle(filenames.begin(), filenames.end(), g);
  26. Mat image = imread(filePath, IMREAD_COLOR);
  27. if(image.empty())
  28. {
  29. stringstream sstream;
  30. sstream << "Could not read the image: " << filePath << std::endl;
  31. throw std::runtime_error(sstream.str());
  32. }
  33. resize(image, image, Size(this->videoWidth, this->videoHeight));
  34. return image;
  35. }
  36. Mat Input::readWebcam()
  37. {
  38. std::unique_lock<std::mutex> lock(mtx);
  39. Mat image;
  40. if(!cap.isOpened()) {
  41. stringstream sstream;
  42. sstream << "Video capture not opened" << std::endl;
  43. throw std::runtime_error(sstream.str());
  44. }
  45. if(!cap.grab()) {
  46. stringstream sstream;
  47. sstream << "Could not grab frame from camera" << std::endl;
  48. throw std::runtime_error(sstream.str());
  49. }
  50. cap.retrieve(image);
  51. return image;
  52. }
  53. void Input::freeWebcam()
  54. {
  55. std::unique_lock<std::mutex> lock(mtx);
  56. this->cap.release();
  57. }