a basic introduction to opencv for image processing

Upload: kelvin-goh

Post on 15-Oct-2015

75 views

Category:

Documents


0 download

DESCRIPTION

A Basic Introduction to OpenCV for Image Processing

TRANSCRIPT

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    1/17

    A Basic Introductionto OpenCV for Image

    ProcessingQing Chen (David)

    E-mail: [email protected]

    DiscoverLabSchool of Information Technology & Engineering

    University of Ottawa

    January 15, 2007

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    2/17

    2

    Outline

    1. Introduction 2. Image data structure in OpenCV

    3. Basic operations for images 4. Working with videos

    5. References and resources

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    3/17

    3

    1. Introduction

    General description

    Open source computer vision library in C/C++.

    Optimized and intended for real-time applications.

    OS/hardware/window-manager independent.

    Generic image/video loading, saving, and acquisition.

    Both low and high level API.

    Provides interface to Intel's Integrated Performance

    Primitives (IPP) with processor specific optimization(Intel processors).

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    4/17

    4

    1. Introduction Features:

    Image data manipulation (allocation, release, copying, setting, conversion).

    Image and video I/O (file and camera based input, image/video file output). Matrix and vector manipulation and linear algebra routines.

    Various dynamic data structures (lists, queues, sets, trees, graphs).

    Basic image processing (filtering, edge detection, corner detection, samplingand interpolation, color conversion, morphological operations, histograms,image pyramids).

    Structural analysis (connected components, contour processing, distancetransform, various moments, template matching, Hough transform, polygonalapproximation, line fitting, ellipse fitting, Delaunay triangulation).

    Camera calibration (finding and tracking calibration patterns, calibration,fundamental matrix estimation, homography estimation, stereo correspondence).

    Motion analysis (optical flow, motion segmentation, tracking).

    Object recognition (eigen-methods, HMM). Basic GUI (display image/video, keyboard and mouse handling, scroll-bars).

    Image labeling (line, conic, polygon, text drawing).

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    5/17

    5

    1. Introduction

    OpenCV modules:

    cv - Main OpenCV functions.

    cvaux - Auxiliary (experimental) OpenCV

    functions.cxcore - Data structures and linear algebra

    support.

    highgui - GUI functions.

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    6/17

    6

    2. Image data structure in OpenCV

    Load and display an image in OpenCV:#i ncl ude "cv. h" / / mai n OpenCV f unct i ons#i ncl ude "hi ghgui . h" / / OpenCV GUI f unct i onsi ncl ude

    i nt mai n( ){

    / * decl ar e a new I pl I mage poi nt er , t he basi ci mage dat a st r uct ur e i n OpenCV */

    IplImage* newImg;/ * l oad an i mage named "appl e. bmp", 1 means

    t hi s i s a col or i mage */

    newImg = cvLoadImage("apple.bmp",1);/ / cr eat e a new wi ndowcvNamedWi ndow( "Wi ndow", 1) ;/ / di spl ay t he i mage i n t he wi ndow

    cvShowI mage( "Wi ndow", newI mg) ;/ / wai t f or key t o cl ose t he wi ndowcvWai t Key( 0) ;cvDest r oyWi ndow( "Wi ndow" ) ; / / dest r oy t he wi ndowcvRel easeI mage( &newI mg ) ; / / r el ease t he memory f or t he i mager et ur n 0;

    }

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    7/17

    7

    2. Image data structure in OpenCV IplImage is the basic image data structure in OpenCV

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    8/17

    8

    3. Basic operations for images Threshold

    #i ncl ude "cv. h"#i ncl ude "hi ghgui . h"#i ncl ude "mat h. h"

    i nt mai n( )

    { I pl I mage* src;I pl I mage* col orThr esh;I pl I mage* gray;I pl I mage* gr ayThresh;i nt t hr eshol d = 120, maxVal ue = 255;i nt t hr eshol dType = CV_THRESH_BI NARY;

    sr c = cvLoadI mage( "appl e. bmp", 1) ;col orThresh = cvCl oneI mage( src ) ;gr ay = cvCr eat eI mage( cvSi ze( sr c- >wi dt h, sr c- >hei ght ) , I PL_DEPTH_8U, 1 ) ;cvCvt Col or ( sr c, gray, CV_BGR2GRAY ) ;gr ayThr esh = cvCl oneI mage( gr ay ) ;

    cvNamedWi ndow( " sr c", 1 ) ; cvShowI mage( "s r c", sr c ) ;cvNamedWi ndow( "gray", 1 ) ; cvShowI mage( "gray", gr ay ) ;

    cvThreshold(src, colorThresh, threshold, maxValue, thresholdType);

    cvThreshold(gray, grayThresh, threshold, maxValue, thresholdType);

    cvNamedWi ndow( "col orThr esh", 1 ) ; cvShowI mage( "col orThr esh", col orThr esh ) ;

    cvNamedWi ndow( "grayThresh", 1 ) ; cvShowI mage( "grayThr esh" , grayThr esh ) ;

    cvWai t Key(0) ;

    cvDest r oyWi ndow( "sr c" ) ;cvDest r oyWi ndow( "col orThr esh" ) ;cvDest r oyWi ndow( "gray" ) ;cvDest r oyWi ndow( "grayThr esh" ) ;cvRel easeI mage( &sr c ) ;cvRel easeI mage( &col orThr esh ) ;cvRel easeI mage( &gray ) ;

    cvRel easeI mage( &gr ayThr esh ) ;

    r etur n 0;}

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    9/17

    9

    3. Basic operations for images Canny edge detection

    #i ncl ude "cv. h"#i ncl ude "hi ghgui . h"

    i nt mai n( ){I pl I mage* newI mg; / / or i gi nal i mageI pl I mage* grayI mg; / / gr ay i mage f or t he conver si on of t he or i gi nal i mageI pl I mage* cannyI mg; / / gr ay i mage f or t he canny edge detect i on

    / / l oad or i gi nal i magenewI mg = cvLoadI mage( "appl e. bmp", 1) ;/ / creat e a si ngl e channel 1 byte i mage ( i . e. gr ay- l evel i mage)grayI mg = cvCr eat eI mage( cvSi ze( newI mg- >wi dt h, newI mg->hei ght) , I PL_DEPTH_8U, 1 ) ;

    / / conver t or i gi nal col or i mage (3 channel r gb col or i mage) t o gr ay-l evel i magecvCvt Col or ( newI mg, grayI mg, CV_BGR2GRAY ) ;cannyI mg = cvCr eat eI mage(cvGetSi ze( newI mg) , I PL_DEPTH_8U, 1) ;

    / / canny edge detect i on

    cvCanny(grayImg, cannyImg, 50, 150, 3);

    cvNamedWi ndow( "sr c" , 1) ;cvNamedWi ndow( "canny", 1) ;cvShowI mage( " sr c" , newI mg ) ;

    cvShowI mage( "canny", cannyI mg ) ;

    cvWai t Key( 0) ;

    cvDest r oyWi ndow( "sr c" ) ;cvDest r oyWi ndow( "canny" ) ;cvRel easeI mage( &newI mg ) ;cvRel easeI mage( &gr ayI mg ) ;cvRel easeI mage( &cannyI mg ) ;

    r etur n 0;}

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    10/17

    10

    3. Basic operations for images Contour detection

    #i ncl ude "cv. h"#i ncl ude "cxcore. h"#i ncl ude "hi ghgui . h"

    i nt mai n( ){I pl I mage* newI mg = NULL;I pl I mage* grayI mg = NULL;I pl I mage* cont our I mg = NULL;

    / / paramet ers f or t he cont our detecti onCvMemStorage * st or age = cvCr eateMemStorage( 0) ;CvSeq * cont our = 0;i nt mode = CV_RETR_EXTERNAL;mode = CV_RETR_CCOMP; / / detect both out si de and i nsi de cont our

    cvNamedWi ndow( "sr c" , 1) ;cvNamedWi ndow( "cont our" , 1) ;/ / l oad or i gi nal i magenewI mg = cvLoadI mage("appl ebw. bmp", 1) ;/ / create a si ngl e channel 1 byte i mage (i . e. gr ay- l evel i mage)grayI mg = cvCr eat eI mage( cvSi ze( newI mg- >wi dth, newI mg- >hei ght ) , I PL_DEPTH_8U, 1 );/ / conver t ori gi nal col or i mage (3 channel r gb col or i mage) t o gr ay- l evel i magecvCvtCol or ( newI mg, grayI mg, CV_BGR2GRAY ) ;cvShowI mage( "s r c" , newI mg ) ;/ / make a copy of t he or i gi nal i mage t o dr aw t he det ect ed cont ourcont our I mg = cvCr eateI mage(cvGetSi ze( newI mg), I PL_DEPTH_8U, 3);

    cont ourI mg=cvCl oneI mage( newI mg ) ;

    / / f i nd the cont our

    cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));/ / draw t he cont our

    cvDrawContours(contourImg, contour, CV_RGB(0, 255, 0), CV_RGB(255, 0, 0), 2, 2, 8);cvShowI mage( " cont our ", cont our I mg ) ;cvWai t Key( 0) ;cvDest r oyWi ndow( " sr c" ) ; cvDestr oyWi ndow( "contour " ) ;cvRel easeI mage( &newI mg ) ; cvRel easeI mage( &grayI mg ) ; cvRel easeI mage( &cont our I mg ) ;

    cvRel easeMemStorage( &st or age) ;r et ur n 0;}

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    11/17

    11

    3. Basic operations for images Dilate/Erode

    #i ncl ude "cv. h"#i ncl ude "cxcor e. h"#i ncl ude "hi ghgui . h"

    i nt mai n( ){

    I pl I mage* newI mg = NULL;I pl I mage* di l at eI mg = NULL;I pl I mage* erodeI mg = NULL;

    cvNamedWi ndow( "sr c" , 1) ;cvNamedWi ndow( "di l ate", 1);cvNamedWi ndow( "er ode", 1) ;

    / / l oad or i gi nal i magenewI mg = cvLoadI mage( "appl e. bmp", 1);cvShowI mage( " sr c" , newI mg ) ;

    / / make a copy of t he or i gi nal i magedi l ateI mg=cvCl oneI mage( newI mg ) ;erodeI mg=cvCl oneI mage( newI mg ) ;

    / / di l ate i mage

    cvDilate(newImg,dilateImg,NULL,4);/ / erode i mage

    cvErode(newImg,erodeImg,NULL,4);

    cvShowI mage( "di l at e", di l at eI mg ) ;cvShowI mage( " erode", erodeI mg ) ;

    cvWai t Key(0) ;

    cvDest r oyWi ndow( " sr c" ) ; cvDest r oyWi ndow( " di l ate" ) ; cvDest r oyWi ndow( " erode" );cvRel easeI mage( &newI mg ) ; cvRel easeI mage( &di l ateI mg ) ; cvRel easeI mage( &erodeI mg ) ;ret ur n 0;

    }

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    12/17

    12

    3. Basic operations for images Flood and Fill

    #i ncl ude "cv. h"#i ncl ude "cxcor e. h"#i ncl ude "hi ghgui . h"

    i nt mai n( ){I pl I mage* newI mg = NULL;I pl I mage* ff I mg = NULL;

    / / f l ood and f i l l par amet er si nt l o_di f f , up_di f f ; / / t he l ow and up f l ood randge whi ch can be adj ust edCvConnect edComp comp;CvPoi nt f l oodSeed; / / t he or i gi nal pi xel wher e the f l ood begi nsCvScal ar f l oodCol or;l o_di f f =8;up_di f f =8;f l oodCol or = CV_RGB( 255, 0, 0 ) ; / / set t he f l ood col or t o r ed

    cvNamedWi ndow( "sr c" , 1) ;cvNamedWi ndow( "f l ood&f i l l ", 1) ;

    / / l oad or i gi nal i magenewI mg = cvLoadI mage( "appl e. bmp", 1);cvShowI mage( "s r c", newI mg ) ;/ / make a copy of t he or i gi nal i magef f I mg=cvCl oneI mage( newI mg ) ;f l oodSeed=cvPoi nt ( 60, 60) ; / / f l oodi ng start f rompi xel ( 60, 60)

    / / Fl ood and Fi l l f r om pi xel ( 60, 60) wi t h col or r ed and t he f l ood r ange of ( - 8, +8)

    cvFloodFill( ffImg, floodSeed, floodColor, CV_RGB( lo_diff, lo_diff, lo_diff ),

    CV_RGB( up_diff, up_diff, up_diff ), &comp, 8, NULL);

    cvShowI mage( "f l ood&f i l l ", f f I mg ) ;

    cvWai t Key( 0) ;cvDest r oyWi ndow( "sr c" ) ; cvDest r oyWi ndow( "f l ood&f i l l " ) ;cvRel easeI mage( &newI mg ) ; cvRel easeI mage( &f f I mg ) ;

    ret ur n 0;}

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    13/17

    13

    3. Basic operations for images Rotate and Scale

    #i ncl ude "cv. h"#i ncl ude "hi ghgui . h"#i ncl ude "mat h. h"

    i nt mai n( )

    { I pl I mage* src;I pl I mage* dst ;i nt del t a;i nt angl e;

    sr c = cvLoadI mage(" appl e. bmp", 1) ;dst = cvCl oneI mage( src );del t a = 1; angl e = 0;cvNamedWi ndow( "sr c" , 1 );cvShowI mage( "sr c", sr c ) ;f or ( ; ; ){

    f l oat m[ 6] ;doubl e f actor = ( cos( angl e*CV_PI / 180. ) + 1. 1) *3;CvMat M = cvMat( 2, 3, CV_32F, m ) ;i nt w = src->wi dth;i nt h = src->hei ght;

    m[ 0] = ( f l oat ) ( f actor*cos( - angl e*2*CV_PI / 180. ) ) ;m[ 1] = ( f l oat ) (f actor*si n( - angl e*2*CV_PI / 180. ) ) ;m[ 2] = w*0. 5f ;m[ 3] = - m[ 1] ;

    m[ 4] = m[ 0];m[ 5] = h*0. 5f ;

    cvGetQuadrangleSubPix( src, dst, &M, 1, cvScalarAll(0));

    cvNamedWi ndow( "dst " , 1 ) ; cvShowI mage( " dst " , dst ) ;

    i f ( cvWai t Key( 5) == 27 )break;

    angl e = ( angl e + del t a) % 360;}r etur n 0;

    }

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    14/17

    14

    4. Working with videos

    Video capture from a file: CvCapture* cvCaptureFromFile( const char* filename );

    Video capture from a camera: CvCapture* cvCaptureFromCAM( int index );

    example:

    / / capt ur e f r om vi deo devi ce #0CvCapt ur e* capt ur e = cvCapt ur eFr omCAM( 0) ;

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    15/17

    15

    4. Working with videos Grab a frame:

    cvGrabFrame( CvCapture* capture );

    Get the image grabbed with cvGrabFrame: cvRetrieveFrame( CvCapture* capture );

    example:I pl I mage* i mg = 0;

    i f ( ! cvGr abFr ame( capt ur e) ) { / / capt ur e a f r amepr i nt f ( "Coul d not gr ab a f r ame\ n\ 7") ;exi t ( 0) ; }

    / / r et r i eve t he capt ur ed f r ame

    i mg=cvRet r i eveFr ame( capt ur e) ; Release the capture source:

    cvReleaseCapture(&capture);

    For a better understanding of video processing with OpenCV, refer tothe face detection example under the dir: C:\Program

    Files\OpenCV\samples\c\facedetect.c

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    16/17

    16

    5. References and resources

    http://www.intel.com/technology/computing/opencv/index.htmOpenCV official webpage.

    http://opencvlibrary.sourceforge.net/OpenCV documentation and FAQs.

    http://tech.groups.yahoo.com/group/OpenCV/OpenCV forum at Yahoo Groups.

    http://www.site.uottawa.ca/~laganier/tutorial/opencv+directshow/cvision.htmThis is a good walkthrough for OpenCV and the MicrosoftDirectShow technology by Prof. Robert Laganire of university ofOttawa. The configuration of OpenCV for MS .Net is also included.

    http://ai.stanford.edu/~dstavens/cs223b/stavens_opencv_optical_flow.pdfThis is another OpenCV introduction focused on Optical Flow, theinstallation of OpenCV is also included.

  • 5/25/2018 A Basic Introduction to OpenCV for Image Processing

    17/17

    17

    About myself

    I am a Ph.D. candidate at the DiscoverLab, School of InformationTechnology and Engineering, University of Ottawa. My generalresearch interests include image processing and computer vision.My current research topic is focused on real-time vision-based handgesture recognition for Human Computer Interface (HCI).For more information about me, please refer to my webpage:http://www.discover.uottawa.ca/~qchen