Ask Your Question

I am trying to run this simple OpenCV program, but i got this error:

asked 2017-06-16 19:34:28 -0500

dab gravatar image

updated 2017-06-16 19:52:38 -0500

berak gravatar image

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp, line 276

and the image shown was gray.

The code is:

#include <opencv\cv.h>
#include <opencv\highgui.h>

using namespace cv;

int main(int argc, char** argv)
    IplImage* img = cvLoadImage( "C:\\Users\\elala mobile\\Documents\\Visual Studio 2010\\Projects\\opencv\\opencv\\mypic.png" ); //change the name (image.jpg) according to your Image filename.
    cvNamedWindow( "Example1", CV_WINDOW_NORMAL );
    cvShowImage("Example1", img);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
    return 0;

What's the cause of this error?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-06-16 20:02:43 -0500

berak gravatar image

updated 2017-06-16 20:07:09 -0500

please burn that, immediately. whereever you found that, don't go back there again.

this is opencv1.0 c-based code from before 2010, you MUST NOT use it.

#include "opencv2/opencv.hpp"
using namespace cv;
int main() {
    Mat img = imread("/some/path/img.png");
    if (img.empty()) {  // most likely, here's your problem.
          // not found, wrong path, adjust, and try again !
          return -1;
    imshow("Example", img);
    return 0;

please also take a tour of the tutorials here

edit flag offensive delete link more


what about this code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main()
    Mat image;// new blank image
    image = cv::imread("C://Users//elala mobile//Documents//Visual Studio 2010//Projects//opencv//opencv//mypic.png", 0);// read the file
    namedWindow( "Display window", WINDOW_NORMAL );// create a window for display.
    imshow( "Display window", image );// show our image inside it.
    waitKey(0);// wait for a keystroke in the window
    return 0;

I have tried many codes but the result was the same(gray image).

dab gravatar imagedab ( 2017-06-16 20:52:44 -0500 )edit

if something as simple as:

Mat img(400,400,CV_8UC3, Scalar(0,0,255)); 
imshow("lalala", img);

does not show a red image, something is broken in your project, or even your libs.

did you build the opencv libs locally, usingcmake ? which opencv version is it, again ?

berak gravatar imageberak ( 2017-06-17 04:08:29 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-06-16 19:34:28 -0500

Seen: 111 times

Last updated: Jun 16 '17