C++ 使用OpenCV逐帧读取视频文件
我编写了以下代码:C++ 使用OpenCV逐帧读取视频文件,c++,opencv,video,C++,Opencv,Video,我编写了以下代码: #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace std; int main() { cv::namedWindow( "Example 2-3", cv::WINDOW_AUTOSIZE ); cv::VideoCapture cap; cap.open("/
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
int main()
{
cv::namedWindow( "Example 2-3", cv::WINDOW_AUTOSIZE );
cv::VideoCapture cap;
cap.open("/Testing/test.avi");
if(cap.isOpened()==0)
{
cout<<"The video file cannot be opened."<<endl;
return -1;
}
cv::Mat frame;
cap >> frame;
std::cout<<frame.empty()<<std::endl; // I have a problem here: Why the result is 1?
for(;;)
{
cap >> frame;
if( frame.empty() ) break; // Ran out of film
cv::imshow( "Example 2-3", frame );
if( (char)cv::waitKey(33) >= 0 ) break;
}
return 0;
}
我正在尝试逐帧显示视频,当我通过cap.open打开视频时,它已成功打开,但当程序执行代码std::cout时,据我所知,在这种情况下,您应该检查您的视频文件。此外,尝试在打开文件后查看cap包含的内容,例如帧数或fps等 成功打开视频文件并不意味着您的文件是好的。有时,您的视频文件在下载或解码时损坏,等会导致某些帧可能为空 尝试其他视频文件以检查是否得到不同的结果