Thread: OpenCV and Visual Studio 2010: Infamous namespace error.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    OpenCV and Visual Studio 2010: Infamous namespace error.

    I have tried and compile the following code with out any success, I am using Visual Studio 2010.

    Code:
    #include <cxcore.h>
    #include <highgui.h>
    #include <cv.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", CV_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 get the following errors when I try to compile the code.

    \documents\visual studio 2010\projects\test_open_cv.cpp(6): error C2871: 'cv' : a namespace with this name does not exist
    \documents\visual studio 2010\projects\test_open_cv.cpp(17): error C2065: 'Mat' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(17): error C2146: syntax error : missing ';' before identifier 'image'
    \documents\visual studio 2010\projects\test_open_cv.cpp(17): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(18): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(18): error C3861: 'imread': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(20): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(20): error C2228: left of '.data' must have class/struct/union
    1> type is ''unknown-type''
    \documents\visual studio 2010\projects\test_open_cv.cpp(26): error C3861: 'namedWindow': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(27): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(27): error C3861: 'imshow': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(29): error C3861: 'waitKey': identifier not found


    I don't know why I'm getting this. Why am I getting errors for stuff already included in header files. Why am I getting the namespace not defined even though header for it is included for CV.

    Thanks,

    James

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I don't have a Windows machine handy to test with, but where did you ever read any mention of a "namespace cv"? And where did you find "cv.hpp"?

    Try
    Code:
    #include <cv.h>
    #include <cxcore.h>
    #include <highgui.h>
    #include <iostream>
    using namespace std; 
    
    
    int main( int argc, char** argv )
    { 
    //...
    Maybe you'll find this opencv wiki helpful.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Okay I have tried that and now these are the errors I'm getting:


    \documents\visual studio 2010\projects\test_open_cv.cpp(15): error C2065: 'Mat' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(15): error C2146: syntax error : missing ';' before identifier 'image'
    \documents\visual studio 2010\projects\test_open_cv.cpp(15): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(16): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(16): error C3861: 'imread': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(18): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(18): error C2228: left of '.data' must have class/struct/union
    1> type is ''unknown-type''
    \documents\visual studio 2010\projects\test_open_cv.cpp(24): error C3861: 'namedWindow': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(25): error C2065: 'image' : undeclared identifier
    \documents\visual studio 2010\projects\test_open_cv.cpp(25): error C3861: 'imshow': identifier not found
    \documents\visual studio 2010\projects\test_open_cv.cpp(27): error C3861: 'waitKey': identifier not found
    1>
    1>Build FAILED.
    1>

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to learn how to solve problems by yourself:

    Why is Mat undeclared? Google says it's in the namespace cv.
    Why didn't you "use" the namespace cv? Because you were told so and did without questioning.
    Why didn't it compile with the namespace? Google says you included the wrong header file.

    Try to come up with the same answers using a search engine. Then test them.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Actually, I have tried using it without namespace, prior to not being told to do so. However, I wanted to show what would happend if I didn't use namespace cv. And I have been using the google search engine for any problems I have ran into, including this issue.
    However, I had a hard time finding the solution. It seems to me that, I have been inquiring the wrong questions in the search engine and probably why I wasn't able to find the answer I was seeking.
    Thank you, I'll look into using correct header file.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    According to this page, you should be including cv.h and using namespace cv.
    Attached Images Attached Images OpenCV and Visual Studio 2010: Infamous namespace error.-untitled-jpg 
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio 2010 - DirectX compilation error.
    By PieMonster in forum Game Programming
    Replies: 2
    Last Post: 09-04-2011, 01:44 PM
  2. Visual Studio 2010 .exe help
    By omGeeK in forum Game Programming
    Replies: 4
    Last Post: 03-20-2011, 01:36 PM
  3. Replies: 2
    Last Post: 08-28-2010, 12:58 AM
  4. Visual Studio 2010 Help
    By dnelsalty in forum C++ Programming
    Replies: 15
    Last Post: 08-30-2009, 06:07 PM
  5. Windows 7 RC, Visual Studio 2010 Beta, Office 2010 beta, anyone in?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2009, 01:57 PM