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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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)
  4. //Input::Input(int videoHeight, int videoWidth) : cap(0), videoHeight(videoHeight), videoWidth(videoWidth)
  5. {
  6. this->cap.set(CAP_PROP_FRAME_HEIGHT, videoHeight);
  7. this->cap.set(CAP_PROP_FRAME_WIDTH, videoWidth);
  8. }
  9. Input::~Input()
  10. {
  11. this->freeWebcam();
  12. }
  13. Mat Input::readFile(String filePath)
  14. {
  15. std::srand(std::time(0));
  16. // Read all .jpg files from the specified folder
  17. cv::String folder = filePath;
  18. std::vector<cv::String> filenames;
  19. cv::glob(folder, filenames);
  20. // Random shuffle
  21. std::random_device rd;
  22. std::mt19937 g(rd());
  23. std::shuffle(filenames.begin(), filenames.end(), g);
  24. Mat image = imread(filePath, IMREAD_COLOR);
  25. if(image.empty())
  26. {
  27. stringstream sstream;
  28. sstream << "Could not read the image: " << filePath << std::endl;
  29. throw std::runtime_error(sstream.str());
  30. }
  31. resize(image, image, Size(this->videoWidth, this->videoHeight));
  32. return image;
  33. }
  34. Mat Input::readWebcam()
  35. {
  36. Mat image;
  37. if(!cap.isOpened()) {
  38. stringstream sstream;
  39. sstream << "Video capture not opened" << std::endl;
  40. throw std::runtime_error(sstream.str());
  41. }
  42. if(!cap.grab()) {
  43. stringstream sstream;
  44. sstream << "Could not grab frame from camera" << std::endl;
  45. throw std::runtime_error(sstream.str());
  46. }
  47. cap.retrieve(image);
  48. return image;
  49. }
  50. void Input::freeWebcam()
  51. {
  52. this->cap.release();
  53. }