stanford hci group / cs377s Designing Applications that See Designing Applications that See Lecture 8: OpenCV Dan Maynes-Aminzade 31 January 2008 31 January 2008 Designing Applications that See http://cs377s.stanford.edu
R Reminders i d � Pick up Assignment #1 if you haven’t already Pi k A i t # if h ’t l d � Assignment #2 due next Tuesday g y � Fill out the interim course evaluation form Lecture 8: OpenCV 31 January 2008 2
T d Today’s Goals ’ G l � Explore the capabilities of the OpenCV E l th biliti f th O CV library � Learn the basics of programming with OpenCV OpenCV � Build a few working OpenCV examples � Study other examples to learn more Lecture 8: OpenCV 31 January 2008 3
O tli Outline � General introduction G l i t d ti � Basic video capture and display p p y � Image processing examples � Optical flow example l fl l � Video writing Video writing � Look at more examples (time permitting) Lecture 8: OpenCV 31 January 2008 4
Wh t i O What is OpenCV? CV? � O � Open source C/C++ computer vision library 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 vision algorithms CV: Co pute v s o a go t s � HIGHGUI: Media, window, and UI handling � Capture, read, and write videos p � Display video windows � Handle mouse and keyboard events � CVAUX: Experimental functionality (beta) Lecture 8: OpenCV 31 January 2008 5
O OpenCV Features CV F t � Image data manipulation � Image data manipulation � Image and video I/O � Matrix and vector manipulation Matrix and vector manipulation � Dynamic data structures � Image Processing Image Processing � Structural analysis � Camera calibration Camera calibration � Motion analysis � Object recognition j g � Basic GUI � Basic drawing Lecture 8: OpenCV 31 January 2008 6
H l Help on OpenCV O CV � Reference Manuals � Reference Manuals � OpenCV\docs � Image Processing Samples g g p � Edge detection: edge � Segmentation: pyramid_segmentation � Morphology morphology � Morphology: morphology � Histogram: demhist � Distance transform: distrans � Ellipse fitting: fitellipse � Video Processing Samples � Color tracking camshiftdemo � Color tracking: camshiftdemo � Optical flow: lkdemo � Motion segmentation: motempl � Edge detection: laplace Lecture 8: OpenCV 31 January 2008 7
I Installing OpenCV t lli O CV � D � Download from SourceForge l d f S F http://sourceforge.net/projects/opencvlibrary � Current version: 1.0.0 � Windows version includes installer � Installing in Linux (requires ffMPEG): gunzip opencv-1.0.0.tar.gz g p p g tar –xvf opencv-1.0.0.tar cd opencv-1.0.0 ./configure / fi make make install Lecture 8: OpenCV 31 January 2008 8
C Compilation Instructions 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 \ g++ basic cpp o basic \ -I /usr/local/include/opencv -L /usr/local/lib \ -lm -lcv -lhighgui –lcvaux g g � Compiling in Windows: � Start Visual Studio and follow along now! St t Vi l St di d f ll l ! Lecture 8: OpenCV 31 January 2008 9
Ch Check Windows Path k Wi d P th � Should include OpenCV\bin l d O CV\bi Sh ld i Lecture 8: OpenCV 31 January 2008 10
Update Visual Studio Directories 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 Library directories OpenCV\lib Lecture 8: OpenCV 31 January 2008 11
U d t P Update Project Link Settings j t Li k S tti � Li k � Link against i t the OpenCV libraries libraries � cv.lib � cxcore.lib lib � highgui.lib (for HIGHGUI (for HIGHGUI support) � cvaux lib cvaux.lib (for CVAUX support) Lecture 8: OpenCV 31 January 2008 12
B Basic Example i E l #include <cv h> #include <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); cvReleaseImage(&img); } Lecture 8: OpenCV 31 January 2008 13
Pi Pixel Processing l P i IplImage* img = cvLoadImage("tennis jpg"); IplImage* img = cvLoadImage( tennis.jpg ); for (int x=0; x<img->width; x++) { for (int y=0; y<img->height; y++) { y y g g 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]=255–val; } } } cvSaveImage("tennis_inverted.jpg", img); cvReleaseImage(&img); Lecture 8: OpenCV 31 January 2008 14
U i Using Windows Wi d cvNamedWindow("ImageWindow"); N dWi d ("I Wi d ") cvShowImage("ImageWindow", img); cvWaitKey(0); W itK (0) Lecture 8: OpenCV 31 January 2008 15
L Loading Video di Vid // Open the input video // 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); Lecture 8: OpenCV 31 January 2008 16
O ti Optical Flow 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); Lecture 8: OpenCV 31 January 2008 17
S Saving Video i Vid // Open a video writer to save the output // 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, lastframe); cvWriteFrame(writer lastframe); // R l // Release when finished h fi i h d cvReleaseVideoWriter(&writer); Lecture 8: OpenCV 31 January 2008 18
F Functions to Check Out ti t Ch k O t � Image Processing Image Processing � cvSobel, cvLaplace, cvCanny, cvCornerHarris, cvGoodFeaturesToTrack, cvHoughLines2, cvHoughCircles � Optical Flow � 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 � cvHaarDetectObjects Lecture 8: OpenCV 31 January 2008 19
Recommend
More recommend