1Using OpenCV with gcc and CMake {#tutorial_linux_gcc_cmake} 2=============================== 3 4@prev_tutorial{tutorial_linux_install} 5@next_tutorial{tutorial_linux_eclipse} 6 7| | | 8| -: | :- | 9| Original author | Ana Huamán | 10| Compatibility | OpenCV >= 3.0 | 11 12@warning 13This tutorial can contain obsolete information. 14 15@note We assume that you have successfully installed OpenCV in your workstation. 16 17- The easiest way of using OpenCV in your code is to use [CMake](http://www.cmake.org/). A few 18 advantages (taken from the Wiki): 19 -# No need to change anything when porting between Linux and Windows 20 -# Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK ) 21- If you are not familiar with CMake, checkout the 22 [tutorial](http://www.cmake.org/cmake/help/cmake_tutorial.html) on its website. 23 24Steps 25----- 26 27### Create a program using OpenCV 28 29Let's use a simple program such as DisplayImage.cpp shown below. 30@code{.cpp} 31#include <stdio.h> 32#include <opencv2/opencv.hpp> 33 34using namespace cv; 35 36int main(int argc, char** argv ) 37{ 38 if ( argc != 2 ) 39 { 40 printf("usage: DisplayImage.out <Image_Path>\n"); 41 return -1; 42 } 43 44 Mat image; 45 image = imread( argv[1], 1 ); 46 47 if ( !image.data ) 48 { 49 printf("No image data \n"); 50 return -1; 51 } 52 namedWindow("Display Image", WINDOW_AUTOSIZE ); 53 imshow("Display Image", image); 54 55 waitKey(0); 56 57 return 0; 58} 59@endcode 60### Create a CMake file 61 62Now you have to create your CMakeLists.txt file. It should look like this: 63@code{.cmake} 64cmake_minimum_required(VERSION 2.8) 65project( DisplayImage ) 66find_package( OpenCV REQUIRED ) 67include_directories( ${OpenCV_INCLUDE_DIRS} ) 68add_executable( DisplayImage DisplayImage.cpp ) 69target_link_libraries( DisplayImage ${OpenCV_LIBS} ) 70@endcode 71### Generate the executable 72 73This part is easy, just proceed as with any other project using CMake: 74@code{.bash} 75cd <DisplayImage_directory> 76cmake . 77make 78@endcode 79### Result 80 81By now you should have an executable (called DisplayImage in this case). You just have to run it 82giving an image location as an argument, i.e.: 83@code{.bash} 84./DisplayImage lena.jpg 85@endcode 86You should get a nice window as the one shown below: 87 88![](images/GCC_CMake_Example_Tutorial.jpg) 89