opencv tutorial 1 chap no.1 and 2

5
OpenCV Tutorial 1 Chapters 1 and 2 Author: Noah Kuntz (2008) Contact: [email protected] Keywords: OpenCV, computer vision, image manipulaton, gaussian blur, avi My Vision Tutorials Index This tutorial assumes the reader: (1) Has a basic knowledge of Visual C++ (2) Has some familiarity with computer vision concepts The rest of the tutorial is presented as follows: Step 1: Installing OpenCV Step 2: Displaying Images and Video Step 3: Simple Transformations Final Words Important Note! More information on the topics of these tutorials can be found in this book: Learning OpenCV: Computer Vision with the OpenCV Library Step 1: Installing OpenCV Install MSVC++ on windows and GCC or GLIBC on Linux. Download OpenCV from sourceforge Use opencvwin or opencvlin depending on your platform of choice. Optionally you may purchase Intel Integrated Performance Primitives (IPP) which optimizes the performance of OpenCV. Intel IPP Optionally update OpenCV using the "Concurrent Versions System," with software like Tortoise CVS. Now you must create a C++ project and link the correct files. These headers should be linked: #include "C:\Program Files\OpenCV\cv\include\cv.h" #include "C:\Program Files\OpenCV\ml\include\ml.h" #include "C:\Program Files\OpenCV\cxcore\include\cxcore.h" #include "C:\Program Files\OpenCV\cxcore\include\cxtypes.h" #include "C:\Program Files\OpenCV\otherlibs\highgui\highgui.h" Link to cv.lib, ml.lib, cxcore.lib, and highgui.lib Make sure to change the environment variable to PATH=C:\Program Files\OpenCV\bin Step 2: Displaying Images and Video

Upload: hassan-shafique

Post on 06-Nov-2015

49 views

Category:

Documents


9 download

DESCRIPTION

OpenCv Tutorial chapter 1 and chapter 2

TRANSCRIPT

  • 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