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

时间:2023-01-21
本文介绍了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) 简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:imread 在 Opencv 中不起作用 下一篇:是否有公式可以确定给定 BGR 值的整体颜色?(OpenCV 和 C++)

相关文章

最新文章