0

I am trying to set up opencv for c++ in visual studio. I have download OpenCV and set it up and can successfully include opencv without any errors. 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.

#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. Any ideas?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    Make certain that `imread` succeeded by inspecting `img`. If the matrix is empty, find out why and correct the problem before trying to display it. – user4581301 Jul 15 '20 at 21:48
  • so how do i find out why its empty? –  Jul 15 '20 at 22:05
  • `img.data == NULL` is the easiest way if I remember correctly. – user4581301 Jul 15 '20 at 22:08
  • ok, img.data did equal null, so how do i figure out why? –  Jul 15 '20 at 22:12
  • 1
    @user4581301 I think [`cv::Mat::empty()`](https://docs.opencv.org/4.3.0/d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4) is a good way to check if a matrix is empty. – MikeCAT Jul 15 '20 at 22:15
  • 2
    @Whityj Check if the image path is correct. Check if the image file is accessble and not broken. Check if other software can read the image. – MikeCAT Jul 15 '20 at 22:17
  • @MikeCAT Seems reasonable. Checking data was the first thing to pop into my head, though. – user4581301 Jul 15 '20 at 22:17
  • ok, the path is correct, and other applications / programs (such as ms-paint) can display the image. –  Jul 15 '20 at 22:30
  • 1
    Does this answer your question? [imread not working in Opencv](https://stackoverflow.com/questions/7417637/imread-not-working-in-opencv) – Mast Jul 16 '20 at 04:40
  • @Whityj Try running your application with admin privilege. As mentioned by the previous comments, the issue adheres to the fact that you are trying to show an empty image. – hecate Jul 16 '20 at 05:20
  • Yeah, I tried running the code as admin and it still failed –  Jul 16 '20 at 12:47
  • also @Mast, I looked over the thread and tried each answer, but all failed –  Jul 16 '20 at 12:55
  • I don't think we have enough information to properly answer this then. – Mast Jul 16 '20 at 12:56
  • @Whityj I suspect it is 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. I will update my answer. – rayryeng Jul 16 '20 at 15:34
  • ok, i tried moving the project to the "repos" folder (where all visual studio projects default to), and opened visual studio as an admin, and it still failed. :( –  Jul 16 '20 at 19:45

1 Answers1

1

Since you're on Windows, you must use \\ to denote the directory separator:

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. I will update my answer. So for the former, exit Visual Studio, then right click on the icon and click on 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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193