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

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

<시간/>

여기에서는 기본 카메라에 액세스하고 해당 카메라의 비디오 스트림을 표시하는 방법을 이해합니다. 랩톱에서는 고정 웹캠이 기본 카메라입니다. 데스크탑에서 기본 카메라는 카메라가 연결된 직렬 포트의 순서에 따라 다릅니다. 기본 웹캠에서 비디오 스트림을 캡처하려는 경우 카메라에 대해 알 필요가 없고 카메라가 연결되어 있는지 확인할 필요가 없습니다.

다음 프로그램은 기본 카메라에서 비디오 스트림을 가져와 실시간으로 화면에 보여줍니다.

예시

#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(0);//Declaring an object to capture stream of frames from default 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;
      }
   }
   cap.release();//Releasing the buffer memory//
   return 0;
}

이 프로그램은 모니터에 실시간 기본 카메라의 비디오 스트림을 보여줍니다.

출력

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