我构建了带有 ffmpeg 支持的 opencv,这是我的代码片段,用于读取视频并遍历帧。
if (argc != 3) {
std::cout<<"Usage: exe input_video_filename skip_frames \n"<<std::endl;
return 0;
}
char* video_fn = argv[1];
int skip_frames = atoi(argv[2]);
VideoCapture cap;
if(!cap.open(video_fn))
{
std::cout<<"error reading video"<<video_fn<<std::endl;
return -1;
}
Mat frame,gray;
int i = 0;
double score = 0;
vector<double> scores;
while(true)
{
if(!**cap.retrieve(frame)**)
{
std::cout<<"error retrieve frame"<<std::endl;
break;
}
if(i%skip_frames)
{
cvtColor(frame,gray,CV_BGR2GRAY);
double sc = compute_cpbdm(gray);
std::cout<<sc<<std::endl;
scores.push_back(sc);
}
}
程序成功读取 cap.open(video_fn) 行的视频,但无法在 if(!cap.retrieve(frame)) 行从 VideoCapture 检索帧。我尝试了mp4和avi,但总是失败。有人遇到过类似的问题吗?如何解决?非常感谢!