Upload
hassan-shafique
View
37
Download
9
Tags:
Embed Size (px)
DESCRIPTION
OpenCv Tutorial chapter 1 and chapter 2
Citation preview
6/1/2015 OpenCVTutorial1
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html 1/5
OpenCVTutorial1Chapters1and2Author:NoahKuntz(2008)Contact:[email protected]
Keywords:OpenCV,computervision,imagemanipulaton,gaussianblur,avi
MyVisionTutorialsIndex
Thistutorialassumesthereader:(1)HasabasicknowledgeofVisualC++(2)Hassomefamiliaritywithcomputervisionconcepts
Therestofthetutorialispresentedasfollows:
Step1:InstallingOpenCVStep2:DisplayingImagesandVideoStep3:SimpleTransformationsFinalWords
ImportantNote!
Moreinformationonthetopicsofthesetutorialscanbefoundinthisbook:LearningOpenCV:ComputerVisionwiththeOpenCVLibrary
Step1:InstallingOpenCV
InstallMSVC++onwindowsandGCCorGLIBConLinux.
DownloadOpenCVfromsourceforge
Useopencvwinoropencvlindependingonyourplatformofchoice.
OptionallyyoumaypurchaseIntelIntegratedPerformancePrimitives(IPP)whichoptimizestheperformanceofOpenCV.IntelIPP
OptionallyupdateOpenCVusingthe"ConcurrentVersionsSystem,"withsoftwarelikeTortoiseCVS.
NowyoumustcreateaC++projectandlinkthecorrectfiles.Theseheadersshouldbelinked:
#include"C:\ProgramFiles\OpenCV\cv\include\cv.h"#include"C:\ProgramFiles\OpenCV\ml\include\ml.h"#include"C:\ProgramFiles\OpenCV\cxcore\include\cxcore.h"#include"C:\ProgramFiles\OpenCV\cxcore\include\cxtypes.h"#include"C:\ProgramFiles\OpenCV\otherlibs\highgui\highgui.h"
Linktocv.lib,ml.lib,cxcore.lib,andhighgui.libMakesuretochangetheenvironmentvariabletoPATH=C:\ProgramFiles\OpenCV\bin
Step2:DisplayingImagesandVideo
6/1/2015 OpenCVTutorial1
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html 2/5
AnimagedisplayedwithOpenCV
ThefirstthingwecandowithOpenCVissimplyaccessanimage.Thiscodewilldisplayatheimage"MGC.jpg"inawindow.cvLoadImageloadstheimageintoanIplImage*variable.cvNamedWindowcreatesawindowandcvShowImagepaintstheimageonthewindow.ThencvWaitKey(0)waitsforanykeytobepressedatwhichpointthememoryisreleasedandthewindowisclearedwithcvReleaseImageandcvDestroyWindow.
int_tmain(intargc,_TCHAR*argv[]){ IplImage*img=cvLoadImage("MGC.jpg"); cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE); cvShowImage("Example1",img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("Example1"); return0;}
Thenextstepistodisplayavideo.NowwehavetocreateaCvCapture*objectwithcvCreateFileCapture.WeloopthroughtheimagesusingcvQueryFrametogeteachframeandcvWaitKey(33)towait33msbetweeneachframe.TheniftheuserpressesEsctheloopbreaksandthecaptureisreleasedandthewindowclosed.Itshouldbenotedthatinordertocapturefromacamerainsteadofavideofile,allthatisneededistoreplacecvCreateFileCapturewithcvCreateCameraCapture(0)whichwillpickthefirstavailablecamerainterface.Hereisthecodeforplayingavideo:
int_tmain(intargc,_TCHAR*argv[]){ cvNamedWindow("Example2",CV_WINDOW_AUTOSIZE); CvCapture*capture=cvCreateFileCapture("MGC_RC_ATV.avi"); IplImage*frame; while(1){ frame=cvQueryFrame(capture); if(!frame)break; cvShowImage("Example2",frame); charc=cvWaitKey(33); if(c==27)break; } cvReleaseCapture(&capture); cvDestroyWindow("Example2"); return0;}
Step3:SimpleTransformations
6/1/2015 OpenCVTutorial1
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html 3/5
Gaussianblurtransformation
Nextwewouldliketobeabletodosomeactualprocessingonanimage.Onesimpletransformationisagaussianblur.Firstweloadanimageasbefore,andmaketwowindows.Anewfunctioninthisexampleiscreatinganemptyimagetoholdtheoutput,withcvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3)whichissayingmakeanewimagethesamesizeas"img",with3channelsof8bitseach.ThenthegaussianblurisaccomplishedwithcvSmooth(img,out,CV_GAUSSIAN,11,11)whichputstheresultin"out"andusesawindowof11x11fortheblur.Hereisthecode:
int_tmain(intargc,_TCHAR*argv[]){ IplImage*img=cvLoadImage("MGC.jpg"); cvNamedWindow("Example3in"); cvNamedWindow("Example3out");
//Showtheoriginalimage cvShowImage("Example3in",img);
//Createanimagefortheoutput IplImage*out=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
//PerformaGaussianblur cvSmooth(img,out,CV_GAUSSIAN,11,11);
//Showtheprocessedimage cvShowImage("Example3out",out);
cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&out); cvDestroyWindow("Example3in"); cvDestroyWindow("Example3out"); return0;}
6/1/2015 OpenCVTutorial1
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html 4/5
DownsampleandCannytransformation
Hereisanexampleofmoretransformations,doingapyramidaldownsampleofanimage,andperformingCannyedgedetection.Thereareafewnewthingsinthisexample.Byaddinganinputof0tocvLoadImagetheimageisforcedtobegrayscale.ThisisrequiredforCannyedgedetection.Alsoweusepropertiesoftheimageloaded,forexample"img>width."Using">"youcanseeallthepropertiesfortheimagethatareavailable.AndthentheimageisprocessedusingcvPyrDown(img,out)forthedownsample,andthencvCanny(out,out,10,100,3)fortheedgedetection.Forthecannyfunctionthesyntaxis"input,""output,""lowerthreshold,""upperthreshold,"and"aperature."
int_tmain(intargc,_TCHAR*argv[]){ IplImage*img=cvLoadImage("MGC.jpg",0); cvNamedWindow("Example4in"); cvNamedWindow("Example4out");
//Showtheoriginalimage cvShowImage("Example4in",img);
//Makesureimageisdivisibleby2 assert(img>width%2==0&&img>height%2==0);
//Createanimagefortheoutput IplImage*out=cvCreateImage(cvSize(img>width/2,img>height/2),img>depth,img>nChannels);
//Reducetheimageby2 cvPyrDown(img,out); //Performcannyedgedetection cvCanny(out,out,10,100,3);
//Showtheprocessedimage cvShowImage("Example4out",out);
cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&out); cvDestroyWindow("Example4in"); cvDestroyWindow("Example4out");
return0;}
FinalWords
Thistutorial'sobjectivewastoshowhowtodobasicimageloadingandprocessinginOpenCV.Latertutorialsinthisserieswillexpandonthefunctionalityshownhere.
Clickheretoemailme.ClickheretoreturntomyTutorialspage.
hitcounter
6/1/2015 OpenCVTutorial1
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html 5/5
htmlhitcountercode