c++ - OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code -


i trying run simple opencv program, got 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's cause of error?

this error means trying show empty image. when load image imshow, caused by:

  1. the path of image wrong (in windows escape twice directory delimiters, e.g. imread("c:\path\to\image.png") should be: imread("c:\\path\\to\\image.png"), or imread("c:/path/to/image.png"));
  2. the image extension wrong. (e.g. ".jpg" different ".jpeg");
  3. you don't have rights access folder.

a simple workaround exclude other problems put image in project dir, , pass imread filename (imread("image.png")).

remember add waitkey();, otherwise won't see anything.

you can check if image has been loaded correctly like:

#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; } 

Comments