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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_shuffle(filenames.begin(), filenames.end());
  22. Mat image = imread(filePath, IMREAD_COLOR);
  23. if(image.empty())
  24. {
  25. std::cout << "Could not read the image: " << filePath << std::endl;
  26. return Mat();
  27. //To do:Exception handeling
  28. }
  29. resize(image, image, Size(this->videoWidth, this->videoHeight));
  30. return image;
  31. }
  32. Mat Input::readWebcam()
  33. {
  34. Mat image;
  35. if(!cap.isOpened()) {
  36. cout << "Video capture not opened" << std::endl;
  37. return Mat();
  38. }
  39. if(!cap.grab()) {
  40. cout << "Could not grab frame from camera" << std::endl;
  41. return Mat();
  42. }
  43. cap.retrieve(image);
  44. return image;
  45. }
  46. void Input::freeWebcam()
  47. {
  48. this->cap.release();
  49. }