Using OpenCV with gcc and CMake on Slackware64 14.0

Home / Using OpenCV with gcc and CMake on Slackware64 14.0

On previous article, we have discuss about how to install OpenCV using git and cmake to out Slackware64 14.0 machine. Now on this article we will see how we can use OpenCV with gcc and CMake to build a computer vision application.

On this article we will use:

  1. Slackware64 14.0 (Any Linux is fine)
  2. gcc
  3. CMake
  4. OpenCV

Of course I assume you have successfully install OpenCV. If not, please refer to this. Using CMake gives us advantages such as:

  1. No need to change anything when porting between Linux and Windows
  2. Can be easily combined with other tools by CMake such as Qt, ITK, and VTK.

If you are new to CMake you can visit their website and read their tutorial.

Create An OpenCV Program

Let’s create a simple program. Write a new C++ source file and name it as 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;
}

 

Create a CMake file

Next create the CMakeLists.txt file which has following text:

project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Generate the executable

Then execute following:

cd <DisplayImage_directory>
cmake .
make

Result

At this point you will have an executeable file called DisplayImage. Try run it and give it an image location as an argument, such as:

./DisplayImage <some image here>

Now, have you see the image? 😀

, ,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial