c++ - 未知函数行 261 中的 opencv 错误 : assertion failed (size. width>0 && size.height>0)

标签 c++ opencv width line assert

这是我的代码:

#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;

int main(){
    //create matrix to store image
    Mat image;

    //initialize capture    
    VideoCapture cap(0);
    cap.open(0);

    //create window to show image   
    namedWindow("window",1);

    while(1){   
        //copy webcam stream to image
        cap>>image;

        //print image to screen     
        imshow("window",image); //Error line

        //delay 33ms
        waitKey(33);
    }
}

我得到的错误:

opencv error: assertion failed (size.width>0 && size.height>0) in unknown function
file...\opencv\modules\highgui\src\window.cpp line 261

在window.cpp中,第261行是:

CV_Assert(size.width>0 && size.height>0);

我解决了我的问题。 首先,您可以添加此代码

VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 

然后在 while 循环中添加这段代码:

cap.read(image);

最佳答案

(size.width>0 && size.height>0) 表示那边有一个空的Mat。在这种情况下,肯定是 image 是空的,因为无法打开相机。检查 VideoCapture::open 返回值。

关于c++ - 未知函数行 261 中的 opencv 错误 : assertion failed (size. width>0 && size.height>0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766025/

相关文章:

c++ - C++ 中的二维数组

python - 我们如何从输入图像中去除棋盘状噪声

c++ - 在新线程中调用 OpenCV 函数 Canny() 会导致段错误

opencv - 在SURF中更改描述符大小

html - 2012年布局的首选宽度

c++ - 在构造函数中为另一个相同类型的对象构造一个对象是个好主意吗?

c++ - 递归设置的 n 个元素的 k 元组组合

c++ - 将构造函数指定为 noexcept 是否会隐式导致在 C++ 中使用 new 运算符的 nothrow 版本?

html - 具有剩余宽度、省略号和新行上的一些内容的 Flexbox 列表

html Canvas 文本大小不准确