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
imshow( " ORIGINAL ", src );

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’

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s