#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;
}
上記のコードは、カメラからビデオをキャプチャするだけです。しかし、私はこのビデオから画像 (つまり、1 フレームのみ) を取得したいと考えています。誰かがこれを行う方法を教えてください。私は実際にwhileループを削除しようとしました。これにより、ループを次々に取得するのではなく、ループを1つだけ取得できます(つまり、ビデオ)。また、「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;
}
残念ながら、空白のウィンドウが表示されているだけです。誰か助けてくれませんか...よろしくお願いします