C++ 无法在OpenCV中获取帧
我一直在尝试让openCV从我的计算机网络摄像头读取图像。以下代码成功打开网络摄像头(绿灯亮起)。但是,尝试获取帧并因此读取帧失败。我在这里不知所措。有人能帮忙吗 非常感谢,, 希拉里 另外,我正在MacBookPro上运行MacOSX10.9。我的opencv版本是2.4.6.1 代码如下:C++ 无法在OpenCV中获取帧,c++,macos,opencv,webcam,C++,Macos,Opencv,Webcam,我一直在尝试让openCV从我的计算机网络摄像头读取图像。以下代码成功打开网络摄像头(绿灯亮起)。但是,尝试获取帧并因此读取帧失败。我在这里不知所措。有人能帮忙吗 非常感谢,, 希拉里 另外,我正在MacBookPro上运行MacOSX10.9。我的opencv版本是2.4.6.1 代码如下: #include "opencv.hpp" using namespace cv; int main(int, char**) { VideoCapture cap = VideoCaptur
#include "opencv.hpp"
using namespace cv;
int main(int, char**) {
VideoCapture cap = VideoCapture(0);
if(!cap.isOpened()){
printf("failed to open camera\n");
return -1;
}
namedWindow("edges",1);
for(;;){
if(waitKey(50) >= 0 ) break;
if(!cap.grab()){
printf("failed to grab from camera\n");
}
}
return 0;
}
你忘了在循环中读取新帧并显示它们!在那里:
for(;;){
if(waitKey(50) >= 0 ) break;
Mat frame;
if(!cap.grab()){
printf("failed to grab from camera\n");
break;
}
cap >> frame;
if(frame.empty()){
printf("failed to grab from camera\n");
break;
}
imshow("edges", frame);
}
谢谢你的回复。不
cap>>frame
调用grab()
然后retrieve()
?引用中说“这些方法/函数在一次调用中结合了VideoCapture::grab()和VideoCapture::retrieve()。”因此,如果grab()
本身失败,那么read()
不会因为它首先调用grab而失败吗?是的,你说得对,它会,但是操作符>
忽略grab()返回的结果
所以您没有访问权限。问题是,在大多数情况下,如果网络摄像头出现问题,您的程序会因为空的帧而在imshow()
上崩溃。我修复了我的代码,所以它现在可以处理这个案例。非常感谢您的回复。我能让我的代码正常工作。