OPENCV READ IMAGE AND VIDEO

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets look into a way of opening image/video/camera with opencv

Reading an image:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.


#include <highgui.h>     //main header file

int main( int argc,char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
//IplImage is an old way of reading,Mat can also be used

cvNamedWindow("example" ,CV_WINDOW_AUTOSIZE);
//naming a window to show image

cvShowImage("example,img);
//Tranferring the image read to the window

cvWaitKey(0);
//creating a waitkey to put up a stable window

cvReleaseImage( &img);
cvDestroyWindow("example");
}


3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp

Note:FILENAME is the name given to the file,use the one you used

b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`
c)./FILENAME

Reading a video/camera input:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

#include <highgui.h>

CvCapture* capture = NULL;
int main( int argc, char** argv )
{
cvNamedWindow( "example5", CV_WINDOW_AUTOSIZE );

if(argc == 1)
{
capture = cvCreateCameraCapture(0);
}
else
{
capture = cvCreateFileCapture(argv[1]);
}
assert(capture != NULL);
IplImage* frame;
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "example5", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "example5" );
}

3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp
Note:FILENAME is the name given to the file,use the one you used
b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`

c)./FILENAME (for video)

d)./FILENAME _____________ (for video,the video file name goes in the blank,eg:- ./FILENAME video.mkv and the video file should be in same folder of the cpp file otherwise whole path should me mentioned while execution eg:- ./FILENAME /home/USER/Desktop/video.mkv)

Thank you 🙂

See also :- http://blindperception.wordpress.com/

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