OpenCV2 and g++: running your first program

Here’s a sample ‘helloworld’ program which takes an image and displays it:

#include "opencv2/opencv.hpp"

using namespace cv;

int main( int argc, char** argv )
{
// Read the source file
Mat src;
src = imread("fruits.jpg");

// Show the results
namedWindow( " ORIGINAL ", WINDOW_AUTOSIZE );
imshow( " ORIGINAL ", src );

waitKey();
return 0;
}

Save it is ‘helloworld.cpp’ using your favorite editor. Download a ‘fruits.jpg’ file from internet and save it in the same folder.

Comping the program using g++:

1. Use following command to compile:

g++ -Wall helloworld.cpp `pkg-config --libs --cflags opencv` -o helloworld

‘pkg-config –libs –cflags opencv’ adds all the necessary header file path and libraries.

2. run using ‘./helloworld’

OpenCV Installation Issue on Ubuntu: How to fix the ‘include : No such file or directory’ error

To install the latest OpenCV from git we follow the instructions on :

http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation

But if you stumble on the ‘include<Eigen/Eigenvalues>: No such file or directory’ error during make here’s how to fix that:

1. Install package

sudo apt-get install libeigen3-dev

2. Then change the cmake command to:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 ..

3. do ‘make’ and ‘sudo make install’ as instructed.