수색…


이미지로드

#include <highgui.h>

//...

cv::Mat img = cv::imread("img.jpg");

...

비디오로드 중

cv::VideoCapture 사용법을 보여줍니다. 다음은 파일에서 비디오를로드하는 예제입니다.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>

using namespace cv;
VideoCapture videoSource;
Mat frame;
#define VIDEO_PATH "video.avi"

int main() 
{
    //Open video
    if (!videoSource.open(VIDEO_PATH))
    {            
        std::cout<<"Video not found at "<<VIDEO_PATH<<std::endl;
        return 1;     // Exit if fail
    }
    videoSource.set(CV_CAP_PROP_CONVERT_RGB, 1);
    
    int cameraWidth = videoSource.get(CV_CAP_PROP_FRAME_WIDTH);
    int cameraHeight = videoSource.get(CV_CAP_PROP_FRAME_HEIGHT);
    float cameraAspectRatio = cameraWidth / cameraHeight;

    std::cout <<"Camera resolution: " << cameraWidth<<", "<<cameraHeight<<" aspect ratio: "<<cameraAspectRatio<< std::endl;
    
    while(true)
    {
        videoSource >> frame;
        if(frame.empty())
            break;  
        //Resize frame
        cv::resize(frame, frame, cv::Size(320, 320 / cameraAspectRatio));
        imshow("frame", frame);
        waitKey(20);
    }
   waitKey(0);
   return 0;
}

라이브 캡처

웹캠과 함께 cv::VideoCapture 를 사용하는 방법을 보여줍니다. 웹캠에서 프레임 캡처 및 표시. 다음은 예제 코드입니다.

#include <iostream> 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;
VideoCapture videoSource;
Mat frame;

int main() 
{
    if(!videoSource.open(0)) //if more cameras available use 1,2,...
        return 1;
 
    while(true)
    {            
        videoSource >> frame;
        if(frame.empty())
            break;
        imshow("Webcam", frame); //or any kinf of precessing
        if(waitKey(1)==27)
            break;//stop capturing is ESC pressed
    }

   return 0;
}

동영상 저장하기

cv :: VideoWriter 사용법을 보여줍니다.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "ERROR: Cannot open the video file" << endl;
        return -1;
    }

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
 
    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

    Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

    VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object 

    if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
   {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
   }

   while (1)
   {

    Mat frame;

    bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) //if not success, break loop
    {
         cout << "ERROR: Cannot read a frame from video file" << endl;
         break;
    }

    oVideoWriter.write(frame); //writer the frame into the file

    imshow("MyVideo", frame); //show the frame in "MyVideo" window

    if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
   {
        cout << "esc key is pressed by user" << endl;
        break; 
   }
}

return 0;

}

이미지 저장

실제로 라이브 캡처 예제는 이미지를 캡처하는 데 적합하므로 이미지를 캡처하여 폴더에 저장하는 데 사용하고 있습니다.

#include <fstream>
#include <string>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>


int main() 
{
    std::stringstream file; // to write the file name 
    
    cv::VideoCapture cap(0); // create a capture object

    int counter = 0; // Create counter
    
    while(true) // infinite loop
    { 
        cv::Mat frame; // Create a object 
       
        cap.read(frame); // read the frame

        file << "/home/user/path_to_your_folder/image" << counter << ".jpg"; // file name

        cv::imwrite(file.str(), frame);
        
        counter++; // increment the counter
    }

   return 0;
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow