简体   繁体   English

C++,OpenCV,在尝试显示图像时收到此错误“OpenCV(4.3.0)错误:断言失败(size.width>0 && size.height>0)”

[英]C++, OpenCV, getting this error “OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)” when trying to display image

I am trying to set up opencv for c++ in visual studio.我正在尝试在 Visual Studio 中为 c++ 设置 opencv。 I have download OpenCV and set it up and can successfully include opencv without any errors.我已经下载了 OpenCV 并进行了设置,并且可以成功包含 opencv 而没有任何错误。 When trying the following code I get and error: "OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)" after line 11.尝试以下代码时,我得到错误:“OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)”在第 11 行之后。

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

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("C:/Users/Whity/Desktop/OpenCVRoot/OpenCV/Win10BG_Red.png", IMREAD_COLOR);

    imshow("Display Window", img);

    waitKey(10);

    return 0;
}

I have looked at numerous other threads where many much of the discussion was about an incorrect path to the image, or using the wrong file type.我查看了许多其他线程,其中许多讨论是关于图像的错误路径或使用错误的文件类型。 (png, jpg, jpeg) I have quadruple checked my path and have tried other paths and other images, all resulting in the same error. (png,jpg,jpeg)我已经四次检查了我的路径并尝试了其他路径和其他图像,所有这些都导致了相同的错误。 Any ideas?有任何想法吗?

Since you're on Windows, you must use \\ to denote the directory separator:由于您使用的是 Windows,因此您必须使用\\来表示目录分隔符:

Mat img = imread("C:\\Users\\Whity\\Desktop\\OpenCVRoot\\OpenCV\\Win10BG_Red.png", IMREAD_COLOR);

Even after the corrections to the path above, I suspect that you may still be having trouble due to a permissions error.即使在对上述路径进行了更正之后,我怀疑您仍然可能由于权限错误而遇到问题。 Your Desktop cannot be accessed by certain programs unless you give it Administrator access.除非您授予其管理员访问权限,否则某些程序无法访问您的桌面。 You either need to open up Visual Studio as an Administrator, or move the image somewhere else on your computer so that it has access.您要么需要以管理员身份打开 Visual Studio,要么将图像移动到计算机上的其他位置以使其具有访问权限。 I will update my answer.我会更新我的答案。 So for the former, exit Visual Studio, then right click on the icon and click on Run As Administrator .所以对于前者,退出 Visual Studio,然后右键单击图标并单击Run As Administrator Once you open it up, try loading up the image again.打开它后,尝试再次加载图像。 If you're still having trouble, move the image somewhere else like in your Documents directory on your computer and try reading it from there.如果您仍然遇到问题,请将图像移动到其他位置,例如您计算机上的Documents目录中,然后尝试从那里读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV 错误:断言失败 (size.width&gt;0 &amp;&amp; size.height&gt;0) 简单代码 - OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code opencv错误:未知函数行261中的断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - opencv error: assertion failed (size.width>0 && size.height>0) in unknown function line 261 使用OpenCV断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0)Qt - Assertion failed (size.width>0 && size.height>0) Qt with OpenCV Visual Studio 2015 OpenCV 断言在 cv::imshow windows.cpp 中失败(size.width&gt;0 &amp;&amp; size.height&gt;0) - Visual Studio 2015 OpenCV Assertion failed (size.width>0 && size.height>0) in cv::imshow windows.cpp 尝试使用 VideoCapture 和 imshow(),引发 Assertion failed (size.width&gt;0 &amp;&amp; size.height&gt;0) in cv::imshow - trying to use VideoCapture and imshow(), raises Assertion failed (size.width>0 && size.height>0) in cv::imshow 复制图像后在imshow中断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - Assertion failed (size.width>0 && size.height>0) in imshow after copying image OpenCV错误:断言失败(a_size.width == len) - OpenCV Error: Assertion failed (a_size.width == len) OPENCV / C ++:roximateddp断言失败错误 - OPENCV / C++ : approxpolydp assertion failed error Opencv错误:断言失败 - Opencv Error:Assertion failed OpenCv图像块,大小错误? - OpenCv image blocks, size error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM