How to code OpenCV program using CMake.
Why CMake? - Because there is no need to change anything while porting between windows and linux. Steps:-
1. Create a file ShowImage.cpp.
2. Create CMakeLists.txt
3. Save both files (ShowImage.cpp, CMakeLists.txt) into same folder named Show(say).
4. Change your directory to /Show.
5. Create Executables.
6. Run Executable.
Download the code from
Download the code from
Step 1. ShowImage.cpp
#include <stdio.h>#include <opencv2/opencv.hpp>usingnamespacecv;intmain(intargc,char**argv){Matimage;image=imread(argv[1],1);if(argc!=2||!image.data){printf("Image data not found\n");return-1;}namedWindow("Show Image",CV_WINDOW_AUTOSIZE);imshow("Show Image",image);waitKey(0);return0;}
cmake_minimum_required(VERSION2.8)
project(ShowImage)
find_package(OpenCVREQUIRED)
add_executable(ShowImageShowImage.cpp)
target_link_libraries(ShowImage${OpenCV_LIBS})
Step 3. Save both files and image into folder "Show" .
Step 4. Change directory to Show
Step 5.cd <Show_Directory>
cmake <Show_Directory> makeStep 6.
./ShowImage loki.jpegHope you enjoy this post. Don't Forget to share this post.
