Você está na página 1de 5

6/1/2015

OpenCVTutorial1

OpenCVTutorial1Chapters1and2
Author:NoahKuntz(2008)
Contact:nk752@drexel.edu
Keywords:OpenCV,computervision,imagemanipulaton,gaussianblur,avi

MyVisionTutorialsIndex
Thistutorialassumesthereader:
(1)HasabasicknowledgeofVisualC++
(2)Hassomefamiliaritywithcomputervisionconcepts
Therestofthetutorialispresentedasfollows:
Step1:InstallingOpenCV
Step2:DisplayingImagesandVideo
Step3:SimpleTransformations
FinalWords

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

http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html

1/5

6/1/2015

OpenCVTutorial1

AnimagedisplayedwithOpenCV
ThefirstthingwecandowithOpenCVissimplyaccessanimage.Thiscodewilldisplayatheimage"MGC.jpg"inawindow.cvLoadImage
loadstheimageintoanIplImage*variable.cvNamedWindowcreatesawindowandcvShowImagepaintstheimageonthewindow.Then
cvWaitKey(0)waitsforanykeytobepressedatwhichpointthememoryisreleasedandthewindowisclearedwithcvReleaseImageand
cvDestroyWindow.

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.Weloopthroughtheimagesusing
cvQueryFrametogeteachframeandcvWaitKey(33)towait33msbetweeneachframe.TheniftheuserpressesEsctheloopbreaksandthe
captureisreleasedandthewindowclosed.Itshouldbenotedthatinordertocapturefromacamerainsteadofavideofile,allthatisneededis
toreplacecvCreateFileCapturewithcvCreateCameraCapture(0)whichwillpickthefirstavailablecamerainterface.Hereisthecodefor
playingavideo:

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

http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html

2/5

6/1/2015

OpenCVTutorial1

Gaussianblurtransformation
Nextwewouldliketobeabletodosomeactualprocessingonanimage.Onesimpletransformationisagaussianblur.Firstweloadanimage
asbefore,andmaketwowindows.Anewfunctioninthisexampleiscreatinganemptyimagetoholdtheoutput,withcvCreateImage(
cvGetSize(img),IPL_DEPTH_8U,3)whichissayingmakeanewimagethesamesizeas"img",with3channelsof8bitseach.Thenthe
gaussianblurisaccomplishedwithcvSmooth(img,out,CV_GAUSSIAN,11,11)whichputstheresultin"out"andusesawindowof11x11
fortheblur.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;

http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html

3/5

6/1/2015

OpenCVTutorial1

DownsampleandCannytransformation
Hereisanexampleofmoretransformations,doingapyramidaldownsampleofanimage,andperformingCannyedgedetection.Therearea
fewnewthingsinthisexample.Byaddinganinputof0tocvLoadImagetheimageisforcedtobegrayscale.ThisisrequiredforCannyedge
detection.Alsoweusepropertiesoftheimageloaded,forexample"img>width."Using">"youcanseeallthepropertiesfortheimagethat
areavailable.AndthentheimageisprocessedusingcvPyrDown(img,out)forthedownsample,andthencvCanny(out,out,10,100,3)for
theedgedetection.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);

//Showtheprocessedimage
cvShowImage("Example4out",out);

cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&out);
cvDestroyWindow("Example4in");
cvDestroyWindow("Example4out");

return0;

//Performcannyedgedetection
cvCanny(out,out,10,100,3);

FinalWords
Thistutorial'sobjectivewastoshowhowtodobasicimageloadingandprocessinginOpenCV.Latertutorialsinthisserieswillexpandon
thefunctionalityshownhere.
Clickheretoemailme.
ClickheretoreturntomyTutorialspage.
hitcounter
http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html

4/5

6/1/2015

OpenCVTutorial1
htmlhitcountercode

http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html

5/5

Você também pode gostar