Zoeken…


Afbeeldingen laden

#include <highgui.h>

//...

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

...

Video's laden

Laat zien hoe u cv::VideoCapture . Hier is het voorbeeld van het laden van video uit een bestand:

#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;
}

Live vastleggen

Laat zien hoe cv::VideoCapture met bijvoorbeeld een webcam. Frames van webcam vastleggen en weergeven. Hier is de voorbeeldcode:

#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;
}

Video's opslaan

Laat zien hoe u cv :: VideoWriter gebruikt.

#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;

}

Afbeeldingen opslaan

Live Capture-voorbeeld is eigenlijk goed voor het maken van afbeeldingen, dus ik gebruik het om afbeeldingen vast te leggen en op te slaan in een map.

#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
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow