user3010273
user3010273

Reputation: 900

Why doesn't this OpenCV code do anything?

I've been trying to use OpenCV with C++ but even though my code compiles (Visual Studio 2010 ), it doesn't ever do anything:

#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cvwimage.h"
#include "cxcore.h"
#include "cxmisc.h"
#include "ml.h"

using namespace cv;
using namespace std;



int main()

{

    namedWindow("yolo", WINDOW_AUTOSIZE );
    waitKey(1);
    cout << "Why won't this show up?" << endl;

}

It compiles OK, without errors but the program doesn't do anything - when I open it in console, it doesn't return the 'Why won't this show up?" text - there is nothing returned.

Regardless of which tutorial piece of code I am trying to use, it never works and never does anything.

What is going on?

Best regards

EDIT: When I set the wait time to 0 (forever) it still doesn't work.

Upvotes: 0

Views: 221

Answers (3)

scap3y
scap3y

Reputation: 1198

It needs to be,

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

cv::namedWindow("test_1", CV_WINDOW_AUTOSIZE );
cvWaitKey();
std::cout << "This will work because I just tested it." << endl;

I use CMake to link the libraries. The CMake code is,

FIND_PACKAGE( OpenCV REQUIRED )
TARGET_LINK_LIBRARIES( myProject ${OpenCV_LIBS} )

Upvotes: 0

berak
berak

Reputation: 39796

waitKey(1); is the culprit. the minimum time is 1 millis (that#s what you got here)

either make it :

waitKey(0); // forever

or increase the time to something sensible

waitKey(5000); // 5 secs

Upvotes: 0

David Nilosek
David Nilosek

Reputation: 1412

The window does get created, however, because you have the waitKey function set to 1 millisecond it only exists for a very short period of time. Try using:

waitKey(0);

Upvotes: 1

Related Questions