0

I found that this question has been asked many times here, but I haven't found any solution or work-around to this problem. Here's my code (copied from here: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;


int _tmain(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;
}

I compiled this using Visual Studio 2008 and 2010, and got different results (both don't work). The program compiled using VS 2008 has run-time error at imread(), and the other displays the message "Could not open or find the image".

Anybody can help me with this?

ArthurN
  • 179
  • 5
  • 15
  • How are you providing image path to the program? Also make sure you are linking the correct versions of OpenCV `.lib` files for `Debug` and `Release` modes. – sgarizvi Dec 11 '14 at 05:39
  • see http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails-in-debug-mode-when-using-release-libraries especially if using the pre-built libs. Better to build it from source if you can (at least on windows) – Martin Beckett Dec 11 '14 at 05:40
  • The image is in the same directory as the program, it's the argument (`argv[1]`). I tried (from the program's directory) both the relative name (only the file name) and the full path, but it doesn't work. I already linked the correct version of the OpenCV `.lib`. – ArthurN Dec 11 '14 at 05:46
  • I already checked this question, but didn't find any solution or work-around for C++ there. Haven't tried using C interface (`cvLoadImage()`). – ArthurN Dec 11 '14 at 05:49
  • 1
    Can you please add a std::cout << argv[1] << std::endl; before your imread an post the output? – Micka Dec 11 '14 at 05:57
  • Thanks for this hint. Found something that solve this (in VS 2010). If I use `int _tmain()` as the main function (as in the code above) and add `cout << argv[1] << endl` before `imread()`, only the first character of the image file name is displayed. Then I changed the main function to `int main()`, and this code works (the whole file name is displayed in `cout << argv[1] << endl`). What is the difference between `int _tmain()` and `int main()` actually? – ArthurN Dec 11 '14 at 06:26
  • However, in VS 2008 it's already `int main()` (because I began writing the code as an empty project). The code still doesn't work. – ArthurN Dec 11 '14 at 06:29
  • Is your path relative or absolute? – Micka Dec 11 '14 at 07:25
  • You mean in VS2010? It's relative. In VS2008, I tried both relative & absolute, but it doesn't work. – ArthurN Dec 11 '14 at 07:27
  • So your 2010 version works perfectly now (reads and displays the image), or does it not work? – Micka Dec 11 '14 at 09:44

4 Answers4

1

The problem here is your main() function._tmain does not exist in C++. main does.

_tmain is a Microsoft extension. Here is a nice explanation of these two methods. Further more if you want to add default argument in Visual studio please follow these steps.

  1. Right click your project in Solution Explorer and select Properties from the menu

  2. Go to Configuration Properties -> Debugging

  3. Set the Command Arguments in the property list.

Hope this solves your problem!


#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
    if( argc != 2)
    {
        cout <<"No Commandline Aurgument Found!: 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;
}
Community
  • 1
  • 1
Balaji R
  • 1,805
  • 22
  • 41
0

set argv[1] to be a known image page "C:\test.jpg"

sdn342323
  • 399
  • 3
  • 5
0

Ok, Ive read all the comments and I'm going to answer the main question along with the sub questions.

Why Doesn't my Code Work in VS2008?

The reason your code doesn't work in VS2008 is because you are using the compiled libraries for 2010, at least I think this is a pretty accurate assumption. If you want to be completely accurate then build the libraries, for the compiler you are using.

What is tmain & what is main

This stack overflow question answers the subject a lot better than I ever could, but effectively it is a windows specific main and does not actually exist in C++. It get removes by the compiler on compile time and converter to main.

Community
  • 1
  • 1
GPPK
  • 6,546
  • 4
  • 32
  • 57
  • About the first comment, I don't think I use the compiled OpenCV libraries for VS2010. Previously I wrote several codes in VS2008 using the same OpenCV libraries, and they all work. Anyway, I'm going to work with VS2010, so this doesn't really matter to me. – ArthurN Dec 11 '14 at 07:30
  • About the second comment, I already found the answer in stackoverflow. Thanks. – ArthurN Dec 11 '14 at 07:30
  • Please remember that StackOverflow is for everybody, if one of these answers solves your problem please accept/upvote so the next person who searches google can find which one worked for you without reading through all the comments. – GPPK Dec 11 '14 at 09:51
0

Can you please try this: Use known-to-work image and absolute path until it works so you can be sure that there is no problem with image or relative path.

download http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png to C:\Lenna.png

rename your main function to something else and try this: if it does not work, please tell me the displayed name of the output window.

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
    namedWindow( "Display window", WINDOW_AUTOSIZE );   // Create a window for display.

    Mat image;
    image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR);   // Read the file
    if(! image.cols )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image. Press any key to exit." << std::endl ;
        cv::waitKey(0)
        return -1;
    }
    imshow( "Display window", image );                   // Show our image inside it.
    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

Please try this and if it does not work, please tell me the displayed name of the output window.

Micka
  • 19,585
  • 4
  • 56
  • 74