C++ 断言失败(size.width>0 && size.height>0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20821269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assertion failed (size.width>0 && size.height>0)
提问by jytoronto
I'm using Visual Studio Express 2013 with OpenCV 2.4.7
, following this tutorial.
2.4.7
按照本教程,我将 Visual Studio Express 2013 与 OpenCV 一起使用。
I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:
我花了几个小时在网上搜索解决方案,包括所有相关的 SO 问题。我试过了:
the return value of
VideoCapture::open
is 1extending the waitKey() delay to 50ms and later 500ms
setting the dimensions of the window
creating another project on Visual C++
opening an existing image instead of reading from camera (same error)
的返回值为
VideoCapture::open
1将 waitKey() 延迟延长至 50 毫秒,然后延长至 500 毫秒
设置窗口的尺寸
在 Visual C++ 上创建另一个项目
打开现有图像而不是从相机读取(相同错误)
but no luck, please help!
但没有运气,请帮忙!
Here's my code:
这是我的代码:
#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 Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ........\opencv\modules\highgui\src\window.cpp, line 261
OpenCV 错误:断言失败 (size.width>0 && size.height>0) in cv::imshow, file ........\opencv\modules\highgui\src\window.cpp, line 261
Error occurs at the line imshow("window", image);
. When I commented it out, there are no complaints.
错误发生在线路上 imshow("window", image);
。当我评论出来时,没有任何抱怨。
UPDATES:
更新:
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.
出现此错误的合理解释是我的网络摄像头需要时间启动,这就是为什么 image.empty() 最初为 true,因此调用 abort() 函数退出程序。
With the code
随着代码
if (!image.empty()) {
imshow("window", image);
}
we can wait for the camera to start
我们可以等待相机启动
采纳答案by Dennis
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:
我试过你的代码,对我来说它有效(它可视化当前的网络摄像头输入)!
我在带有 OpenCV 2.4.7 的 Visual Studio 2012 Ultimate 上运行它。
...
发生错误是因为图像为空,所以试试这个:
while (true) {
cap >> image;
if(!image.empty()){
imshow("window", image);
}
// delay 33ms
waitKey(33);
}
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.
也许您从网络摄像头收到的第一张图像是空的。在这种情况下 imshow 不会抛出错误。所以希望下一个输入图像不是空的。
回答by ravindra singh rathor
int i=0;
while(i<4)
{
VideoCapture cap(0); // force camera to open 4 tiMEs
i++;
}
waitKey(5000);
VideoCapture cap(0);
int camOpen = cap.open(CV_CAP_ANY);
namedWindow("window", CV_WINDOW_AUTOSIZE);
while (true) {
cap >> image;
imshow("window", image);
waitKey(33);
}
Do this it will work for you for sure.
这样做它肯定对你有用。
回答by 72DFBF5B A0DF5BE9
Do this:
做这个:
VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int camOpen = cap.open(CV_CAP_ANY);
Or you can try changing this:
或者你可以尝试改变这个:
while (true) {
cap >> image;
imshow("window", image);
// delay 33ms
waitKey(33);
}
to
到
try
{
cap >> image;
imshow("window", image);
waitKey(33);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}