Now to find a device capable of shooting video is really too easy. The results all use video instead of the previous sequence of images. Video may be obtained from two types, one is the webcam image as a live video stream, or generated by other compression-encoded video file device. Fortunately, the OpenCV can use the same C ++ class, process the video information in the same way. In the following tutorial you will learn how to use the camera or video file.
- How to open and read the video stream
- Two ways to check the similarity: PSNR and SSIM
How to Read a video stream (camera or video files)?
In general, all functions are integrated video capture needs in VideoCapture C ++ class inside. Although it is dependent on another underlying open source FFmpeg library, but it has been integrated in the OpenCV in so you do not need extra attention to its specific implementation. You only need to know a video consists of a series of images, we use a little professional vocabulary to refer to these images constitute a video: "frame" (frame). In addition there is a video file parameter called the "frame rate" (frame rate) that represents a time interval between two frames, the unit is the frame rate (frames / sec). This parameter is only related to the speed of the video, there is no use for a single frame image is.
You need to define an object class VideoCapture to open and read the video stream. Specific constructor can be accomplished by or through the open function. If you use integer as argument, the object can be bound to a camera, the ID number assigned by the system can be passed as a parameter. For example, you can pass to open the first camera 0, 1 pass to open the second camera, and so on. If you use the string as parameters, it will open a specified by the string (file name) video files.
CvCapture is a structure used to hold information required for image capturing. opencv provides two ways to capture an image from the outside:
#include <opencv2 / opencv.hpp> the using namespace CV; void main () { VideoCapture CAP; cap.open ( " E: \\ \\ vs2015 VS2015Opencv Project \\ \\ \\ 01.avi Video " ); // open the video, equivalent to more than two CAP VideoCapture ( "E: // 01.avi"); // cap.open ( " http://www.laganiere.name/bike.avi "); // also be available directly from the web page pictures, the premise is a web video, and speeds fast enough IF (cap.isOpened ()!) // If the video does not open properly return return ; Mat Frame; the while ( 1 ) { CAP >> Frame ; //Equivalent to cap.read (Frame); IF (frame.empty ()) // If a frame is empty the loop is exited BREAK ; imshow ( " Video " , Frame); waitKey ( 20 is ); // every frame latency 20 ms } cap.release (); // release resources }
#include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespace std; void main() { VideoCapture cap; cap.open("E:\\VS2015Opencv\\vs2015\\project\\video\\01.avi"); if (!cap.isOpened()) return; int width = cap.get(CV_CAP_PROP_FRAME_WIDTH); //帧宽度 int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //帧高度 int frameRate = cap.get(CV_CAP_PROP_FPS); //帧率 x frames/s int totalFrames = cap.get(CV_CAP_PROP_FRAME_COUNT); //总帧数 cout << "视频宽度=" << width << endl; cout << "视频高度=" << height << endl; cout << "视频总帧数=" << totalFrames << endl; cout << "帧率=" << frameRate << endl; Mat frame; while (1) { cap >> frame;//等价于cap.read(frame); if (frame.empty()) break; imshow("video", frame); if (waitKey(33) != 255) break; } cap.release(); }
waitkey(33) 如果不按键的时候是返回 oxff,这个无符号就是255,有符号就是-1
windows vs 的环境默认了这个为非符号数 即255,而opencv的新手书中,往往作者环境会认为是-1
解决方案:把原始代码中循环读取帧的
if (waitKey(33)>=0)break;
改为
if (waitKey(33) != 255)break;
或者把waitkey的返回值用有符号数去读取。