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

C++를 사용하여 OpenCV의 컴퓨터에서 비디오를 로드하는 방법은 무엇입니까?

<시간/>

이번 토픽에서는 OpenCV를 이용하여 동영상 파일을 불러와 재생하는 방법을 알게 되며, 이전 토픽에서 배운 것과 유사한 방법을 사용해야 합니다. 유일한 차이점은 숫자를 'VideoCapture' 클래스의 객체 인수로 넣는 것이 아니라 동영상의 경로를 넣어야 한다는 점입니다.

다음 프로그램은 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("video.mp4");//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의 컴퓨터에서 비디오를 로드하는 방법은 무엇입니까?