Tuesday, July 15, 2014

OpenCV: Trouble in First OpenCV Code

Understanding Arguments in OpenCV

One long problem figuring out for the first time in OpenCV that I was not aware of in the visual studios tutorial is in the following code from the same link:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// 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;
}
On line 8, notice that the main takes in parameters, meaning this int main needs arguments, which I suspect mean argc = argumentCount, argv = argumentValue, and therefore need the image file name input. This can be done in command line or going to the properties of your project → Configuration Properties → Debugging → Command Arguments and inputting the name of the image file.

Useful Debugging Tool: Image Watch

For reference sake, even though the extension is mentioned in the opencv tutorials, Image Watch is the name of the extension that allows viewing the images analyzed.

Running Multiple Projects in Visual Studio

Just to make my life easier (or not), I'm running all my OpenCV projects under one solution in Microsoft Visual Studios 12 2013. The way to choose which projects you are going to run with or without debugger is to right click the solution → Set StartUp Projects and choose which project(s) you want to run.

No comments:

Post a Comment