OpenCV error: approval failed (size.width> 0 && size.height> 0) simple code

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 

code:

 #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 is the cause of this error?

+12
source share
4 answers

This error means that you are trying to show a blank image. When you upload an image using imshow , this is usually caused by:

  • The path of your image is incorrect (directory separators are duplicated twice in Windows, for example imread("C:\path\to\image.png") should be: imread("C:\\path\\to\\image.png") or imread("C:/path/to/image.png") );
  • Incorrect image extension. (for example, ".jpg" is different from ".jpeg");
  • You do not have permission to access the folder.

A simple workaround to eliminate other problems is to place the image in the project directory and simply go to imread the file name ( imread("image.png") ).

Remember to add waitKey(); otherwise you will not see anything.

You can check if the image is loaded correctly, for example:

 #include <opencv2\opencv.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; } 
+13
source

Usually this means that your image is not there, this is the main statement for checking whether the content is displayed in the window before it is actually displayed, and by the way, you need to create a window to show the namedWindow ("name") image, then imshow ("name ", image);

+1
source

I had the same problem, only in Raspbian. After several hours of trying, the solution was quite simple, I had to leave the file extension.

 #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main() { Mat inputImage = imread("beniz1"); imshow("Display Image", inputImage); waitKey(5000); return 0; } 
+1
source

double check your image path

0
source

All Articles