用opencv读取USB摄像头图像,实时显示例程:
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> int main() { // Open the video file // cv::VideoCapture capture("E:\\UAV Detection\\video\\video.mp4"); cv::VideoCapture capture(0); // check if video successfully opened if (!capture.isOpened()) return 1; // Get the frame rate double rate= capture.get(CV_CAP_PROP_FPS); bool stop(false); cv::Mat frame; // current video frame cv::namedWindow("Extracted Frame"); // Delay between each frame // corresponds to video frame rate int delay= 1000/rate; // for all frames in video while (!stop) { // read next frame if any if (!capture.read(frame)) break; cv::imshow("Extracted Frame",frame); // introduce a delay // or press key to stop if (cv::waitKey(delay)>=0) //if (cv::waitKey(30) >= 0) stop= true; } // Close the video file capture.release(); }
原程序的问题:图像显示不连续,显示的过程中,要不断的关闭窗口才可以不断的刷新图像显示。
修改:
将while()循环中的 waitKey(delay)修改为 waitKey(30)。即可能的原因是图像窗口刷新的太快,内存跟不上。