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

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

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

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)