[英]how to get a single frame from a video clip using opencv and eclipse
#include <iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char* argv[]) {
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"can't open video file"<<endl;
return -1;
}
namedWindow("Myvideo",CV_WINDOW_AUTOSIZE);
while(1)
{
Mat frame;
bool bsuccess=cap.read(frame);
if(!bsuccess)
{
cout<<"can't read a frame"<<endl;
break;
}
imshow("Myvideo",frame);
if(waitKey(30)==27)
{
cout<<"Esc key is pressed by the user"<<endl;
break;
}
}
return 0;
}
上面的代碼只是從攝像機捕獲視頻。 但是我想從該視頻中獲取一張圖像(即只有一幀)。 有人可以告訴我該怎么做。 我實際上試圖刪除while循環,以便我只能得到一個循環,而不是一個接一個(例如視頻)。 並且還刪除了“ break”語句。
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>`
using namespace std;
using namespace cv;
int main(int argc, const char* argv[]) {
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"can't open video file"<<endl;
return -1;
}
namedWindow("Myvideo",CV_WINDOW_AUTOSIZE);
Mat frame;
cap.read(frame);
imshow("Myvideo",frame);
if(waitKey(30)==27)
{
cout<<"Esc key is pressed by the user"<<endl;
}
return 0;
}
但不幸的是,它只是顯示一個空白窗口。 有人可以幫我嗎...預先感謝
初讀的幀通常是(總是?)黑/灰色,嘗試顯示下一幀-替換此代碼
cap.read(frame);
imshow("Myvideo",frame);
有了這個:
cap.read(frame);
cap.read(frame);
imshow("Myvideo",frame);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.