You are on page 1of 19

stanford hci group / cs377s

Designing Applications that See Lecture 8: OpenCV


Dan Maynes-Aminzade 31 January 2008

Designing Applications that See

http://cs377s.stanford.edu

Reminders R i d
Pick Pi k up A i Assignment # if you h t #1 havent already t l d Assignment #2 due next Tuesday g y Fill out the interim course evaluation form

31 January 2008

Lecture 8: OpenCV

Todays Goals T d G l
Explore the capabilities of the O E l th biliti f th OpenCV CV library Learn the basics of programming with OpenCV Build a few working OpenCV examples Study other examples to learn more

31 January 2008

Lecture 8: OpenCV

Outline O tli
General i t d ti G l introduction Basic video capture and display p p y Image processing examples Optical flow example l fl l Video writing Look at more examples (time permitting)

31 January 2008

Lecture 8: OpenCV

What i OpenCV? Wh t is O CV?


Open source C/C++ computer vision library O C/C t i i lib Created and maintained by Intel Optimized for real-time applications Composed of four separate sub-libraries: p p
CXCORE: Linear algebra, matrix operations CV: Computer v s o a go t s Co pute vision algorithms HIGHGUI: Media, window, and UI handling
Capture, read, and write videos p Display video windows Handle mouse and keyboard events

CVAUX: Experimental functionality (beta)


31 January 2008 Lecture 8: OpenCV 5

OpenCV F t O CV Features
Image data manipulation Image and video I/O Matrix and vector manipulation Dynamic data structures Image Processing Structural analysis Camera calibration Motion analysis Object recognition j g Basic GUI g Basic drawing
31 January 2008 Lecture 8: OpenCV 6

Help H l on O OpenCV CV
Reference Manuals
OpenCV\docs

Image Processing Samples g g p


Edge detection: edge Segmentation: pyramid_segmentation Morphology: Morphology morphology Histogram: demhist Distance transform: distrans Ellipse fitting: fitellipse

Video Processing Samples


Color tracking: camshiftdemo tracking Optical flow: lkdemo Motion segmentation: motempl Edge detection: laplace
31 January 2008 Lecture 8: OpenCV 7

Installing OpenCV I t lli O CV


Download f D l d from S SourceForge F
http://sourceforge.net/projects/opencvlibrary

Current version: 1.0.0 Windows version includes installer Installing in Linux (requires ffMPEG):
g gunzip opencv-1.0.0.tar.gz p p g tar xvf opencv-1.0.0.tar cd opencv-1.0.0 ./configure / fi make make install
31 January 2008 Lecture 8: OpenCV 8

Compilation Instructions C il ti I t ti
Download the sample code here: D l d th l d h
http://cs377s.stanford.edu/code/opencv-tutorial.zip

Compiling in Linux:
g++ basic cpp o basic \ basic.cpp o -I /usr/local/include/opencv -L /usr/local/lib \ g g -lm -lcv -lhighgui lcvaux

Compiling in Windows:
Start Vi l St di St t Visual Studio and follow along now! d f ll l !

31 January 2008

Lecture 8: OpenCV

Check Wi d Ch k Windows P th Path


Should include OpenCV\bin Sh ld i l d O CV\bi

31 January 2008

Lecture 8: OpenCV

10

Update Visual Studio Directories


Include directories I l d di t i
OpenCV\cv\include OpenCV\cxcore\include OpenCV\otherlibs\highgui OpenCV\cvaux\include

Library directories
OpenCV\lib

31 January 2008

Lecture 8: OpenCV

11

Update P j t Li k Settings U d t Project Link S tti


Link Li k against i t the OpenCV libraries
cv.lib cxcore.lib lib highgui.lib (for HIGHGUI support) cvaux.lib cvaux lib (for CVAUX support)
31 January 2008 Lecture 8: OpenCV 12

Basic E B i Example l
#include <cv h> <cv.h> #include <highgui.h> int main(int argc, char *argv[]) { IplImage* img = cvLoadImage("tennis.jpg"); cvFlip(img, img); cvSaveImage("tennis_flipped.jpg", img); cvReleaseImage(&img); }

31 January 2008

Lecture 8: OpenCV

13

Pixel Processing Pi l P i
IplImage* img = cvLoadImage("tennis jpg"); cvLoadImage( tennis.jpg ); for (int x=0; x<img->width; x++) { y y<img->height; y g g y++) { for (int y=0; y for (int c=0; c<img->nChannels; c++) { int idx=x*img->nChannels+y*img->widthStep+c; int val=img->imageData[idx]; img->imageData[idx]=255val; } } } cvSaveImage("tennis_inverted.jpg", img); cvReleaseImage(&img);

31 January 2008

Lecture 8: OpenCV

14

Using Wi d U i Windows
cvNamedWindow("ImageWindow"); N dWi d ("I Wi d ") cvShowImage("ImageWindow", img); cvWaitKey(0); W itK (0)

31 January 2008

Lecture 8: OpenCV

15

Loading Video L di Vid


// Open the input video CvCapture *video = cvCaptureFromFile("stanley.avi"); // Query first frame cvQueryFrame(video); // Read video properties int width = cvGetCaptureProperty(video,CV_CAP_PROP_FRAME_WIDTH); cvGetCaptureProperty(video CV CAP PROP FRAME WIDTH); int height = cvGetCaptureProperty(video,CV_CAP_PROP_FRAME_HEIGHT); int nFrames = cvGetCaptureProperty(video,CV_CAP_PROP_FRAME_COUNT);

31 January 2008

Lecture 8: OpenCV

16

Optical Flow O ti l Fl
cvCalcOpticalFlowPyrLK(grlastframe, C l O ti lFl P LK( l tf grcurrframe, pyramid1, pyramid2, lastframe_features, currframe_features, lastframe features currframe features nFeatures, cvSize(3,3), 5, found_features, feature_error, , , cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ), 0);

31 January 2008

Lecture 8: OpenCV

17

Saving Video S i Vid


// Open a video writer to save the output CvVideoWriter *writer = cvCreateVideoWriter( output.avi cvCreateVideoWriter("output avi", -1, 30, cvSize(width, height)); // Write some frames cvWriteFrame(writer, cvWriteFrame(writer lastframe); // R l Release when fi i h d h finished cvReleaseVideoWriter(&writer);

31 January 2008

Lecture 8: OpenCV

18

Functions t Ch k Out F ti to Check O t


Image Processing
cvSobel, cvLaplace, cvCanny, cvCornerHarris, cvGoodFeaturesToTrack, cvHoughLines2, cvHoughCircles

Optical Flow
cvCalcOpticalFlowPyrLK, cvFindFundamentalMat

Template Matching
cvMatchTemplate, cvMatchShapes, cvCalcEMD2, h l h h l cvMatchContourTrees

Motion
cvKalmanPredict, cvConDensation, cvAcc, cvMeanShift, cvCamShift

Segmentation and Grouping


cvSnakeImage, cvKMeans2, cvSeqPartition, cvCalcSubdivVoronoi2D, cvCreateSubdivDelaunay2D

Machine Learning g
cvHaarDetectObjects
31 January 2008 Lecture 8: OpenCV 19

You might also like