Powered by Blogger.

Tuesday, September 14, 2010

Tag: , , , ,

OpenCV and CMake

From OpenCV 2.1 version we can use CMake to create and manage our OpenCV projects.

CMake is a cross-platform and open-source build system, and it's used to control our compilation process, using a simple text files for define compilation process with independent platform and compiler.

Thanks to CMake we can create our project in our operating system as linux compile and work in it, and then use it to compile a new version in other os as windows, osx, ..., even create a visual studio project, xcode project or eclipse.

To create a basic project with 1 main.c file with cmake and opencv we must create a new file called CMakeList.txt.

In this file we go to define the compiler process and his requeriments.

First we need create a project name:

[c language="++"]
PROJECT( project_name )
[/c]

We need find OpenCV libraries. OpenCV is required by our project and it's defined with REQUIRED statement:

[c language="++"]
FIND_PACKAGE( OpenCV REQUIRED )
[/c]

Then we must defined our executable, his  name and the dependent files, in our example only main.c, and we go to use the project name for the executable name. This name is stored in ${PROJECT_NAME} variable.

[c language="++"]
ADD_EXECUTABLE( ${PROJECT_NAME} main.c )
[/c]

To finish, we must link OpenCV to our executable, to do this only need call TARGET_LINK_LIBRARIES statement, with the project name and OpenCV library.

[c language="++"]
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
[/c]

The CMakeList.txt was defined, then we can compile our project. To do this only need create a directory where we want get our executable. Then  we call cmake with the directory where  CMakeList.txt and main.c files are saved.

When cmake finish whe can call make to compile our project under unix.

If we want create a VisualStudio project under Windows, only need open CMake Gui and select the source code and build directories and cmake detect our visualstudio version and with generate button create our VisualStudio project.

Sample Code:

[c language="++"]
PROJECT( Nombre_del_proyecto )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( ${PROJECT_NAME} main.c )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
[/c]

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.

12 comments:

  1. And if you need to check the OpenCV version you can use this ;)


    IF (${OpenCV_VERSION} VERSION_LESS 2.0.0)
    MESSAGE(FATAL_ERROR "OpenCV version is not compatible : ${OpenCV_VERSION}")
    ENDIF()

    ReplyDelete
  2. TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
    should be:
    TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )

    and CMakeList.txt needs to be CMakeLists.txt

    Mark

    ReplyDelete
  3. You use the cvMat for train boosting, then when you have this mat created, save it in xml or yaml with opencv functions http://opencv.willowgarage.com/documentation/cpp/core_xml_yaml_persistence.html

    ReplyDelete
  4. On linux in a GTK environment I use Anjuta. It is a young IDE, but has potential and integrates well with Gnome. It is written in C/C++ so its not a slow Java IDE.

    ReplyDelete
  5. Is this can run on ios

    ReplyDelete
  6. [...] you through the process of using CMake to compile and build OpenCV projects. It is based on the tutorial made by Damilesbut has some minor [...]

    ReplyDelete
  7. I would say this is the better way for doing it:

    PROJECT( helloworld_proj )
    FILE( MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/src)
    CONFIGURE_FILE( main.cpp ${PROJECT_BINARY_DIR} )
    FIND_PACKAGE( OpenCV REQUIRED )
    add_library(helloworld ${PROJECT_BINARY_DIR}/main.cpp)
    INCLUDE_DIRECTORIES(.)#visual studio path for included files
    TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} )

    that way you can build many different projects with opencv

    ReplyDelete
  8. I'm very happy to find this website. I wanted to thank you for ones time for this fantastic read!! I definitely enjoyed every part of it and I have you saved to fav to see new stuff in your site.

    ReplyDelete
  9. [...] you through the process of using CMake to compile and build OpenCV projects. It is based on the tutorial made by Damiles but has some minor [...]

    ReplyDelete
  10. [...] you through the process of using CMake to compile and build OpenCV projects. It is based on the tutorial made by Damiles but has some minor changes.1. Create a file named CMakeLists.txt and open it for edition in your [...]

    ReplyDelete
  11. [...] o CMake para compilar projectos que usem a biblioteca OpenCV. Este tutorial é baseado num outro tutorial feito por Damiles mas tem algumas pequenas mudanças.1. Criar um ficheiro chamado CMakeLists.txt e abri-lo para [...]

    ReplyDelete
  12. whoah this weblog is great i really like reading your
    posts. Keep up the good work! You realize, a lot of individuals are looking round for this information, you can aid them greatly.

    ReplyDelete