OpenCV Error: Assertion failed (size.width>0 &&

OpenCV Error: Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0) simple code(OpenCV Error: Assertion failed (size.width0 amp;amp;size.height0) 简单代码)
本文介绍了OpenCV Error: Assertion failed (size.width>0 &&size.height>0) 简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行这个简单的 OpenCV 程序,但出现此错误:

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

代码:

#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?

推荐答案

此错误表示您正在尝试显示空图像.当你用imshow加载图片时,这通常是由于:

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. 你的图片路径错误(在 Windows 中转义两次目录分隔符,例如 imread("C:path oimage.png") 应该是:imread("C:\path\to\image.png")imread("C:/path/to/image.png"));
  2. 图片扩展名错误.(例如.jpg"不同于.jpeg");
  3. 您无权访问该文件夹.

排除其他问题的一个简单解决方法是将图像放在您的项目目录中,然后简单地将文件名 (imread("image.png") 传递给 imread).

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")).

记得加上waitKey();,否则什么都看不到.

Remember to add waitKey();, otherwise you won't see anything.

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

You can check if an image has been loaded correctly like:

#include <opencv2opencv.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 Error: Assertion failed (size.width>0 &amp;&amp;size.height>0) 简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)