断言失败(size.width>0 && size.height>0)
我正在使用带有 OpenCV 2.4.7
的 Visual Studio Express 2013,遵循此 教程.
I'm using Visual Studio Express 2013 with OpenCV 2.4.7
, following this tutorial.
我花了几个小时在网上搜索解决方案,包括所有相关的 SO 问题.我试过了:
I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:
VideoCapture::open
的返回值为 1
将 waitKey() 延迟延长至 50 毫秒,然后延长至 500 毫秒
extending the waitKey() delay to 50ms and later 500ms
设置窗口的尺寸
在 Visual C++ 上创建另一个项目
creating another project on Visual C++
打开现有图像而不是从相机读取(同样的错误)
opening an existing image instead of reading from camera (same error)
但没有运气,请帮忙!
这是我的代码:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat image;
VideoCapture cap;
int camOpen = cap.open(CV_CAP_ANY);
namedWindow("window", CV_WINDOW_AUTOSIZE);
while (true) {
cap >> image;
imshow("window", image);
// delay 33ms
waitKey(33);
}
}
当我编译并运行它时,出现以下错误:
As I compiled and ran it, I got the following error:
OpenCV 错误:断言失败 (size.width>0 && size.height>0) in cv::imshow, file ........opencvmoduleshighguisrcwindow.cpp,第 261 行
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ........opencvmoduleshighguisrcwindow.cpp, line 261
错误发生在 imshow("window", image);
行.当我评论出来时,没有人抱怨.
Error occurs at the line imshow("window", image);
. When I commented it out, there are no complaints.
更新:
为什么会发生此错误的合理解释是我的网络摄像头需要时间才能启动,这就是为什么 image.empty() 最初为 true,因此调用 abort() 函数退出程序.
A plausible explanation of why this error occured was that my webcam takes time to start, which is why image.empty() is true initially, hence the abort() function was called to exit the program.
用代码
if (!image.empty()) {
imshow("window", image);
}
我们可以等待相机启动
推荐答案
我试过你的代码,对我来说它有效(它可视化当前的网络摄像头输入)!
我在带有 OpenCV 2.4.7 的 Visual Studio 2012 Ultimate 上运行它.
...
出现错误是因为图片为空,所以试试这个:
I tried your code and for me it works (it visualizes the current webcam input)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7.
...
The error occurs because the image is empty, so try this:
while (true) {
cap >> image;
if(!image.empty()){
imshow("window", image);
}
// delay 33ms
waitKey(33);
}
也许您从网络摄像头收到的第一张图片是空的.在这种情况下 imshow 不会抛出错误.所以希望接下来的输入图像不是空的.
Maybe the first image you receive from your webcam is empty. In this case imshow will not throw an error. So hopefully the next input images are not empty.
相关文章