Now I have tried Section 1.2's using OpenCV with gcc and CMake and found everything OK, except that I blindly copied the program and cmake files without understanding what they are doing.
OpneCV Tutorial 1.2 Using OpenCV with gcc and CMake
...
Steps
Create a program using OpenCV
Let’s use a simple program such as DisplayImage.cpp shown below.
#include <cv.h>
#include <highgui.h>
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;
}
Create a CMake file
Now you have to create your CMakeLists.txt file. It should look like this:
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Generate the executable
This part is easy, just proceed as with any other project using CMake:
cd <DisplayImage_directory>
cmake .
make
Result
By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:
./DisplayImage lena.jpg
You should get a nice window as the one shown below.
.END
pi@raspberrypi ~/fongvision/DisplayImage $ ls
CMakeCache.txt cmake_install.cmake DisplayImage Makefile
CMakeFiles CMakeLists.txt DisplayImage.cpp TestImage.jpg
pi@raspberrypi ~/fongvision/DisplayImage $ cat DisplayImage.cpp
#include <cv.h>
#include <highgui.h>
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;
}
pi@raspberrypi ~/fongvision/DisplayImage $ cat CMakeLists.txt
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
.END
No comments:
Post a Comment