Hello world !

wow finally hello world , you have been waiting for this right ;p. if you have setup all the things correct and added libraries to the IDE , then It’s time to do some nice simple coding.

fire up eclipse , now create a project and add libraries and library paths.

here is the code that I have used

/*
* hello.cc
*
* Created on: Feb 23, 2013
* Author: ludkiller
*/

//This file is for educational purpose

#include<iostream>
#include<cv.h>
#include<highgui.h>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {

Mat image;
//check if filename is provided through command line.
if(argc != 2)
return 0;
//imread function takes file name(if file exists in same directory as executable) or path to file
//and returns a Mat object.
image = imread( argv[1] );
//We can use if(!image.data) to check if image is loaded or not.
//namedWindow to create a window (highgui)
namedWindow(“Display Window”);
//imshow takes a window name and Mat object as parameters and shows a window.
imshow(“Display Window”,image);
//waitKey(x) waits till x ms . 0 is for infinity.
waitKey(0);
return 0;

}

ok now compile/build and run it using command line like this

./Hello_world image.jpg

this will open a nice image in a windows ;D, that’s what it does displays and image as you can see in below image.

Screenshot from 2013-02-23 22:02:34

 

now let us see what the code exactly does. I have commented the code so it will be easy if you just look at the code (you should know basics of C/C++)

 

#include<iostream>
#include<cv.h>
#include<highgui.h>

the above files are included files which are needed for various function and classes.

like Mat class , namedWindow function and other functions.

 

using namespace std;
using namespace cv;

above namespaces are included to make program user friendly, if you don’t use these then you need to use scope resolution to resolve things present in other name spaces.

for example if you skip using namespace std;

std::cout<<“Hello world”;

and if you skip using namespace cv;

then you need to do cv::Mat to declare object of Mat class.

then comes our main() function the entry point of program.

 Mat image;

declare a object of Mat class.

if(argc!=2)

return 0;

check if image path is provided or not if not then exit program.

image = imread(argv[1]);

argv[1] contains image path, so it’s passed on to imread(path to image);

namedWindow(“Display Window”);

create a window named “DIsplay Window”.

imshow(“Display Window”,image);

imshow is used to display image stored in a Mat Object.

first argument is window name and second object is object of class image with loaded image.

waitKey(0);

return 0;

waitKey(x) takes time as argument time is in milli second so waitKey(40); will wait for 40 ms.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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