1

I'm working right out of a book, and copied the following code (essentially the openCV equivalent of "hello world"):

//helloCV.cpp
#include <opencv2/opencv.hpp>

int main(int argc, char** argv){

        cv::Mat img = cv::imread(argv[1], -1);
        if (img.empty()) return -1;

        cv::namedWindow("Example1", cv::WINDOW_AUTOSIZE);
        cv::imshow("Example1", img);
        cv::waitKey(0);
        cv::destroyWindow("Example1");

        return 0;

}//main 

Unfortunately, when I run this code, I get a window with the header and nothing in it:

I suspect that I've messed up the environment or some such when installing OpenCV, but cmake is throwing no errors, and the code runs as expected, exiting correctly on a keystroke and all of that, with the glaring exception of a lack of a displayed photo.

Any tips?

Thanks!

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
Taylor Nelms
  • 384
  • 2
  • 16
  • 1
    There have been a few similar questions posted recently, e.g. https://stackoverflow.com/questions/44639948/image-not-displaying-with-imshow-opencv – Dan Mašek Jun 22 '17 at 01:26
  • 1
    Thanks, @DanMašek! Glad to see that it's not just me; I'll mess around with this tonight and see if other functions work better and can get around the issue that imshow seems to have. – Taylor Nelms Jun 22 '17 at 15:39
  • 1
    Related bug report: https://github.com/opencv/opencv/issues/8885 – Dan Mašek Jun 22 '17 at 17:18

1 Answers1

4

Thanks to @DanMašek for the lead on this one, and to all the people on this page: http://answers.opencv.org/question/160201/video-window-not-loading-frame/

To repeat what they said, what worked for me was the following:

To resolve this, locate the file window_cocoa.mm; if built from source it'll be in opencv/modules/highgui/src.

Change the following:

@implementation CVView
#if defined(__LP64__)
@synthesize image;
#else // 32-bit Obj-C does not have automatic synthesize
@synthesize image = _image;
#endif

To this:

@implementation CVView
@synthesize image = _image;

Do the same thing for the CVWindow and CVSlider implementations to accommodate videos as well.

Recompile OpenCV and test out your code.

Hope this helps other people struggling with this issue!

Taylor Nelms
  • 384
  • 2
  • 16