简体   繁体   English

OpenCV 错误:断言失败 (size.width>0 && size.height>0) 简单代码

[英]OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code

I am trying to run this simple OpenCV program, but i got this error:我正在尝试运行这个简单的 OpenCV 程序,但出现此错误:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp, line 276

Code:代码:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    cv::Mat inputImage = cv::imread("/home/beniz1.jpg");
    cv::imshow("Display Image", inputImage);

    return 0;
}

What's the cause of this error?这个错误的原因是什么?

This error means that you are trying to show an empty image.此错误意味着您正在尝试显示空图像。 When you load the image with imshow , this is usually caused by:当您使用imshow加载图像时,这通常是由以下原因引起的:

  1. The path of your image is wrong (in Windows escape twice directory delimiters, eg imread("C:\\path\\to\\image.png") should be: imread("C:\\\\path\\\\to\\\\image.png") , or imread("C:/path/to/image.png") );您的图像路径错误(在 Windows 中转义两次目录分隔符,例如imread("C:\\path\\to\\image.png")应该是: imread("C:\\\\path\\\\to\\\\image.png")imread("C:/path/to/image.png") );
  2. The image extension is wrong.图片扩展名错误。 (eg ".jpg" is different from ".jpeg"); (例如“.jpg”不同于“.jpeg”);
  3. You don't have the rights to access the folder.您无权访问该文件夹。

A simple workaround to exclude other problems is to put the image in your project dir, and simply pass to imread the filename ( imread("image.png") ).排除其他问题的一个简单解决方法是将图像放在项目目录中,然后简单地传递给imread文件名( imread("image.png") )。

Remember to add waitKey();记得添加waitKey(); , otherwise you won't see anything. ,否则你什么也看不到。

You can check if an image has been loaded correctly like:您可以检查图像是否已正确加载,例如:

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    if (!img.data)
    {
        std::cout << "Image not loaded";
        return -1;
    }

    imshow("img", img);
    waitKey();
    return 0;
}

通常这意味着您的图像不存在,这是在实际显示之前检查内容是否可在窗口中显示的基本断言,顺便说一句,您需要创建一个窗口以显示该图像 namedWindow("name" ) 然后 imshow ("name", image);

I had the exact same problem, only in Raspbian.我有完全相同的问题,只有在 Raspbian 中。 After hours of trying, the solution was pretty simple, I had to leave out the file extension.经过数小时的尝试,解决方案非常简单,我不得不省略文件扩展名。

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
int main()
{
    Mat inputImage = imread("beniz1");
    imshow("Display Image", inputImage);
    waitKey(5000);

    return 0;
}

I also got the same error when I was using Qt Creator in Ubuntu.当我在 Ubuntu 中使用 Qt Creator 时,我也遇到了同样的错误。 The image was in the project folder so I thought there is no need to give the complete path.图像在项目文件夹中,所以我认为没有必要提供完整路径。

img = imread("baboon.png");

The error which I got was:我得到的错误是:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-36Gs_O/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp, line 304
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-36Gs_O/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

Error got resolved by giving the complete path:通过提供完整路径解决了错误:

img = imread("home/vivek/QT_ImageProcessing/IP_HomeWork1/baboon.png");

仔细检查您的图像路径

Most probably, You have not used the correct path to your image or it's format.最有可能的是,您没有使用正确的图像路径或其格式。 If you're using windows: img =cv2.imread("C:/Users/mohin/Pictures/IMG_4514.jpg")如果您使用的是 Windows: img =cv2.imread("C:/Users/mohin/Pictures/IMG_4514.jpg")

Simply add your image to your project directory folder.只需将您的图像添加到您的项目目录文件夹中。

How:如何:

1-Right click on your project name at Search Solution Explorer which is on left by default. 1-在默认位于左侧的搜索解决方案资源管理器中右键单击您的项目名称。

2-Click on Open folder in file explorer 2-单击文件资源管理器中的打开文件夹

3-Paste your image to that folder 3-将您的图像粘贴到该文件夹​​中

then然后

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
int main()
{
    //change "beniz1" to "beniz1.jpg"

    Mat inputImage = imread("beniz1.jpg"); 
    imshow("Display Image", inputImage);
    waitKey(5000);

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用OpenCV断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0)Qt - Assertion failed (size.width>0 && size.height>0) Qt with OpenCV opencv错误:未知函数行261中的断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - opencv error: assertion failed (size.width>0 && size.height>0) in unknown function line 261 C++,OpenCV,在尝试显示图像时收到此错误“OpenCV(4.3.0)错误:断言失败(size.width&gt;0 &amp;&amp; size.height&gt;0)” - C++, OpenCV, getting this error “OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)” when trying to display image Visual Studio 2015 OpenCV 断言在 cv::imshow windows.cpp 中失败(size.width&gt;0 &amp;&amp; size.height&gt;0) - Visual Studio 2015 OpenCV Assertion failed (size.width>0 && size.height>0) in cv::imshow windows.cpp 复制图像后在imshow中断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - Assertion failed (size.width>0 && size.height>0) in imshow after copying image 尝试使用 VideoCapture 和 imshow(),引发 Assertion failed (size.width&gt;0 &amp;&amp; size.height&gt;0) in cv::imshow - trying to use VideoCapture and imshow(), raises Assertion failed (size.width>0 && size.height>0) in cv::imshow OpenCV错误:断言失败(a_size.width == len) - OpenCV Error: Assertion failed (a_size.width == len) 断言OpenCV代码失败的错误 - Assertion failed error in opencv code 如何修复“断言&#39;__pos &lt;= size&#39;失败”错误? - How to fix “Assertion '__pos <= size' failed” error? OpenCV错误:cv :: Mat :: at中的断言失败((无符号)i0 &lt;(无符号)size.p [0]) - OpenCV Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM