[英]Synchronizing screencasting (ffmpeg) and capturing from the webcam (OpenCV)
[英]Function for capturing images from a webcam using openCV works only the first time called
嗨,我是OpenCV的新手,实际上是一般的编程人员。 我正在尝试编写一个函数,每次调用它时都会从相机捕获单个图像。 我编写了此函数,但仅在第一次调用时才起作用。 我有点跳了起来,但是现在我迷上了,必须解决这个问题。
int main(int argc, char * argv[])
{
int faceCnt = 1;
faceCnt = captureFace(faceCnt);
cout << faceCnt << endl;
if(faceCnt == -1)
cout << "A problem has occured << endl;
faceCnt = captureFace(faceCnt);
cout << faceCnt << endl;
if(faceCnt == -1)
cout << "A problem has occured << endl;
return 0;
}
int captureFace(int cnt){
Mat temp;
VideoCapture cap(0);
if(!cap.isOpened()) //Check to see if capture was successful
return -1;
cap >> temp;
cap.release();
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", cnt);
string location = "faces/sub";
string fileType = ".jpg";
string result = location + numstr + fileType;
imwrite(result, temp);
cout << result << endl;
cnt++;
if(cap.isOpened())
return -1;
return cnt;
}
从控制台输出:
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
Corrupt JPEG data: 1 extraneous bytes before marker 0xd2
faces/sub1.jpg
2HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
VIDIOC_STREAMON: Bad file descriptor
Unable to stop the stream.: Bad file descriptor
faces/sub2.jpg
3
查看我得到的错误的其他答案,我发现它们实际上并不重要(尽管我不知道我是否相信)。 我正在为相机使用Logitech C270,并且正在运行xubuntu 13.10。
非常感谢你的帮助!
我注意到关闭和打开同一摄像头的VideoCaptures有时会失败。 最好一次打开网络摄像头并重新使用它。
另外,您不一定需要调用release(),该释放是在VideoCapture的析构函数中自动完成的,或者在再次调用open()时自动完成的。
int main(int argc, char * argv[])
{
int faceCnt = 1;
VideoCapture cap(0);
faceCnt = captureFace(faceCnt, cap);
cout << faceCnt << endl;
if(faceCnt == -1)
cout << "A problem has occured << endl;
faceCnt = captureFace(faceCnt, cap);
cout << faceCnt << endl;
if(faceCnt == -1)
cout << "A problem has occured << endl;
return 0;
}
int captureFace(int cnt, VideoCapture& cap){
Mat temp;
if(!cap.isOpened()) //Check to see if capture was successful
return -1;
cap >> temp;
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", cnt);
string location = "faces/sub";
string fileType = ".jpg";
string result = location + numstr + fileType;
imwrite(result, temp);
cout << result << endl;
cnt++;
return cnt;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.