- 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 namespaceCvusing namespace STD;intMain () {Videocapture capture ("D:\\road.avi");//Detect if the video is read successfully if(!capture.isopened ()) {cout<<"No input image";return 1; }//Get image frame rate DoubleRate = Capture.get (cv_cap_prop_fps);BOOLStop =false; Mat frame; Namedwindow ("Example");intDelay = +/rate; while(!stop) {if(!capture.read (frame)) 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.
- Processing Video frames
In order 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 ); //canny operator edge detection on each frame of the video void Canny (mat& img, mat& out ) {//first to convert each frame image to grayscale image Cvtcolor (img,out , Cv_bgr2gray); //call canny function Canny (out , out , , 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 an instance Videoprocessor processor;Open Video File processor. SetInput("E:\\road.avi");Input and output video processor, respectively. Displayinput("Input Video");Processor. Displayoutput("Output Video");Play video processor at original frame rate. Setdelay(./processor. Getframerate());Set processing callback function processor. Setframeprocessor(canny);Start the frame processing process processor. Run();Waitkey ();
OPENCV reading and processing of video streams