OpenCV错误:断言失败(size.width> 0&& amp; size.height> 0)简单代码 [英] OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code

查看:706
本文介绍了OpenCV错误:断言失败(size.width> 0&& amp; size.height> 0)简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行这个简单的OpenCV程序,但我得到了这个错误:

  OpenCV错误:Assertion失败文件... / opencv / modules / highgui / src / window.cpp,第276行
.width> 0&& p $ p>

代码:

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

using namespace std;

int main()
{
cout< 你好,世界! << endl;

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

return 0;
}

这个错误的原因是什么?

解决方案

此错误表示您尝试显示一个空白图像。当您使用 imshow 加载图片时,通常是由以下原因造成的:


  1. 你的图像的路径是错误的(在Windows中,转义两次目录分隔符,例如 imread(C:\path\to\image.png)应该是: code> imread(C:\\path\\\\\\image.png)或 imread(C: path / to / image.png));

  2. 图片扩展名错误。 (例如.jpg与.jpeg不同);

  3. 您没有访问该文件夹的权限。

一个简单的解决办法,排除其他问题是把图像放在你的项目dir,并简单地传递到 imread 文件名code> imread(image.png))。



记住添加 waitKey



您可以检查图片是否已正确加载,如:

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

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

if(!img.data)
{
std :: cout< 图片未加载;
return -1;
}

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


I am trying to run this simple OpenCV program, but i got this error:

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:

  1. The path of your image is wrong (in Windows escape twice directory delimiters, e.g. imread("C:\path\to\image.png") should be: imread("C:\\path\\to\\image.png"), or imread("C:/path/to/image.png"));
  2. The image extension is wrong. (e.g. ".jpg" is different from ".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")).

Remember to add 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;
}

这篇关于OpenCV错误:断言失败(size.width&gt; 0&amp;&amp; amp; size.height&gt; 0)简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆