2

I am new to to OpenCV and C++ programming. I recently installed and configure OpenCV 2.4.8 with Visual Studio 2010 through this link: http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

Then I test if i did make it right by testing this code:

#include "opencv2/highgui/highgui.hpp"
#include "StdAfx.h"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread("C:\Users\jay\Google Drive\profilepic", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

    if (img.empty()) //check whether the image is loaded or not
    {
        cout << "Error : Image cannot be loaded..!!" << endl;
        //system("pause"); //wait for a key press
        return -1;
    }

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
    imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

    waitKey(0); //wait infinite time for a keypress

    destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

    return 0;
}

Unfortunately, it gave errors:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  ConsoleApplication1.cpp
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(1): warning C4627: '#include "opencv2/highgui/highgui.hpp"': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(5): error C2871: 'cv' : a namespace with this name does not exist
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'Mat' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2146: syntax error : missing ';' before identifier 'img'
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'j' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'G' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'p' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'CV_LOAD_IMAGE_UNCHANGED' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C3861: 'imread': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(12): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(12): error C2228: left of '.empty' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(19): error C2065: 'CV_WINDOW_AUTOSIZE' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(19): error C3861: 'namedWindow': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(20): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(20): error C3861: 'imshow': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(22): error C3861: 'waitKey': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(24): error C3861: 'destroyWindow': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Hope you can tell me what is wrong with my code. I am new to this OpenCV thing and C++.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
user3388464
  • 27
  • 1
  • 3

1 Answers1

4

The line

#include "StdAfx.h"

should be first, before any other includes.
That's why the compiler warns you that it skipped the highgui header.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • it still produce errors like "Cannot find or open PDB file,,," does my way of installing openCV has something to do with these errors? i always saw on tutorials that they use CMake to install openCV but i opt in installing openCV alone without using CMake because it takes long and its complicated. – user3388464 Mar 19 '14 at 10:59
  • @user3388464 When do you get those errors, and what are they specifically? It's normal to see loads of those in VS's output window as you run the program, when it loads DLLs that don't have debugging info. – molbdnilo Mar 19 '14 at 11:03
  • when i build it again.. here is the link of my question about this errors http://stackoverflow.com/questions/22277668/error-cannot-find-or-open-the-pdb-file-error-in-in-vs-c-2010/22277941?noredirect=1#22277941 – user3388464 Mar 19 '14 at 11:35
  • 1
    As far as I can see, those are nothing to worry about but perfectly normal. The exit code "-1" is what you're returning when you can't open the file, and you can't do that because you forgot to escape the backslash with another backslash. Use "\\" or "/" as path separators in C++ literals. – molbdnilo Mar 19 '14 at 11:48