Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++를 사용하여 OpenCV의 다른 카메라에서 비디오를 캡처하는 방법은 무엇입니까?

<시간/>

이 주제에서는 OpenCV를 사용하여 다른 카메라에서 비디오를 캡처하는 방법을 결정할 것입니다. 기본 카메라가 아닌 다른 카메라에 접근하는 것은 기본 카메라에 접근하는 것과 유사합니다. 'VideoCapture cap(0)'을 사용하는 대신 카메라 번호를 할당해야 한다는 차이점이 있습니다. 카메라 번호는 USB 포트의 순서에 따릅니다. 카메라가 세 번째 USB 포트에 연결되어 있으면 카메라 번호는 3입니다.

다음 프로그램은 세 번째 카메라에 액세스하여 카메라에서 가져온 실시간 비디오 스트림을 보여줍니다.

#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
   Mat myImage;//Declaring a matrix to load the frames//
   namedWindow("Video Player");//Declaring the video to show the video//
   VideoCapture cap(3);//Declaring an object to capture stream of frames from third camera//
   if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
      cout << "No video stream detected" << endl;
      system("pause");
      return-1;
   }
   while (true){ //Taking an everlasting loop to show the video//
      cap >> myImage;
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
   }
   cap.release();//Releasing the buffer memory//
   return 0;
}

출력

C++를 사용하여 OpenCV의 다른 카메라에서 비디오를 캡처하는 방법은 무엇입니까?