Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where/how to place build files in OpenCV

Tags:

c++

opencv

cmake

I'm just getting started with OpenCV, and I have the following sample .cpp file (from opencv.org):

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
    Mat image;
    image = imread( argv[1], 1 );

    if( argc != 2 || !image.data )
    {
        printf( "No image data \n" );
        return -1;
    }

    namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
    imshow( "Display Image", image );

    waitKey(0);

    return 0;
}

and I have the following CMakeList.cmake file:

project(opencvTEST)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (test test.cpp)
target_link_libraries(test ${OpenCV_LIBS})

I have a Mac (OS 10.6.8), and I've installed OpenCV 2.4.3 with CMake, and I've searched high and low and tried numerous different things to get this test program to compile (I'm using the command line--no IDE), but I get the following compilation error (clearly, due to the include statement's not working properly):

test.cpp:3:30: error: opencv2/opencv.hpp: No such file or directory
test.cpp:5: error: ‘cv’ is not a namespace-name
test.cpp:5: error: expected namespace-name before ‘;’ token
test.cpp: In function ‘int main(int, char**)’:
test.cpp:9: error: ‘Mat’ was not declared in this scope
test.cpp:9: error: expected `;' before ‘image’
test.cpp:10: error: ‘image’ was not declared in this scope
test.cpp:10: error: ‘imread’ was not declared in this scope
test.cpp:18: error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
test.cpp:18: error: ‘namedWindow’ was not declared in this scope
test.cpp:19: error: ‘imshow’ was not declared in this scope
test.cpp:21: error: ‘waitKey’ was not declared in this scope

I have a folder named opencv2 in the same directory as test.cpp, and opencv.hpp is in opencv2, so I don't understand why it's not finding it. Any ideas?

Also, in general, where does OpenCV expect you to put your source (.cpp, etc.) files?

like image 438
MuffinTheMan Avatar asked Apr 02 '13 03:04

MuffinTheMan


3 Answers

I had exactly the same issue. I ran the same example from opencv tutorial and got the same error

error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope

I solved this by adding a header:

#include <opencv/highgui.h>
like image 108
user2684645 Avatar answered Oct 21 '22 04:10

user2684645


You forget to add in your CMakeLists.txt

include_directories(${OpenCV_INCLUDE_DIRS})

after find_package(OpenCV REQUIRED)

like image 27
Sergei Nikulov Avatar answered Oct 21 '22 05:10

Sergei Nikulov


I wish I knew exactly what the issue was above, but I can only guess that it had something to do with the fact that I was trying to work with OpenCV 2.4.3 while already having OpenCV 2.4.4 installed (I'm guessing this caused some conflicts).

I got OpenCV 2.4.4 working on my machine by removing OpenCV 2.4.3 (just ran rm -rf on the directory that I had placed it in) and uninstalled 2.4.4 (using homebrew: brew uninstall opencv). After taking those steps to clean things up, I ran the following commands (you must have homebrew):

$ brew update #just in case you're missing updates
$ brew tap homebrew/science #skip this if you already have the science formulae
$ brew install opencv #this took about 15 minutes

Finally, I followed this tutorial, and voila, it worked! I chalk this up to working with new software that wasn't intuitive, getting frustrated, and trying 273 different ways to run OpenCV. I guess that all added up to having an install that I didn't know about, which (I think) caused serious issues. Am I the only one who had such a hard time getting OpenCV to work?

like image 21
MuffinTheMan Avatar answered Oct 21 '22 04:10

MuffinTheMan