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

2023-01-21C/C++开发问题
4

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6