OPENCV reading and processing of video streams

Source: Internet
Author: User
OPENCV provides an easy-to-use framework for extracting image frames from video files and USB cameras, and if you just want to read a video, you just need to create a videocapture instance and extract each frame in the loop. The following is a simple code
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\ highgui\highgui.hpp>
#include <iostream>
using namespace CV;
using namespace std;

int main ()
{
    videocapture capture ("D:\\road.avi");
    Detects if the video is successfully read
    if (!capture.isopened ())
    {
        cout << "No input image";
        return 1;
    }

    Get image framerate
    double rate = capture.get (cv_cap_prop_fps);
    bool stop = false;
    Mat frame;
    Namedwindow ("Example");

    int delay = 1000/rate;

    while (!stop)
    {
        if (!capture.read (frame)) is break
            ;
        Imshow ("Example", frame);
        if (Waitkey (delay) >= 0)
            stop = true;
    }
    return 0;
}

To properly open the video file, you must make sure that the computer has the appropriate decoder. It should also be noted that the path of the file is not known correctly, and the path error often prompts the error warning:error opening. /.. /modules/highgui/src/cap_ffmpeg_impl.hpp:545). This error is usually caused by a file path error. Working with video frames
To process each frame in the video, we can create our own class Videoprocessor, which encapsulates the OOPENCV video capture framework, which allows us to make a handler function for each frame call.
First, we want to make a callback handler, (about the callback function, another post http://blog.csdn.net/neal1991/article/details/45009377). This shout out can accept a mat object and then output the Mat object after processing.

void Processframe (mat& img, mat& out);


    Do canny operator edge detection for each frame of the video
void Canny (mat& img, mat& out) 
{
    ///////per frame image to be converted to grayscale
   Cvtcolor (img,out,cv_ Bgr2gray);
    Call the Canny function
   Canny (out,out,100,200);
    Flips the pixel
    threshold (OUT,OUT,128,255,THRESH_BINARY_INV);
}

Defines the good one video processing class, which is associated with a callback function. Using this class, you can create an instance, make an input video file, bind a callback function, and then start processing each frame, to invoke the video processing class, just add it in the main function:

//First create instance Videoprocessor processor;

    Open the video file Processor.setinput ("E:\\road.avi");
    Input and output video processor.displayinput ("Input video"), respectively;
    Processor.displayoutput ("Output Video");
    Plays the video Processor.setdelay (1000./processor.getframerate ()) at the original frame rate;
    Set processing callback function Processor.setframeprocessor (canny);
    Start the frame processing process processor.run (); Waitkey (); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.