OpenCV Tutorial I: Image Processing
Xuan Mo
iPAL Group Meeting
February 4, 2011
Outline
Introduction Data structure Important functions Examples Demos
2/4/2011
iPAL Group Meeting
What is OpenCV?
OpenCV means Intel Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms. The key features: Cross-Platform API of C functions FREE for commercial and non-commercial uses.
2/4/2011
iPAL Group Meeting
Why to use it?
Newton says:If I have seen a little further it is by standing on the shoulders of Giants.. When we have to implement something into production, we need to use C language program. We can take advantage of high speed implementations of functions commonly used in Computer Vision/Image Processing.
2/4/2011
iPAL Group Meeting
OpenCV modules
cv: Main OpenCV functions, Image processing and vision algorithms. cvaux: Auxiliary (experimental) OpenCV functions. cxcore: Data structures, linear algebra support, XML support, drawing functions and other algorithms. highgui: GUI functions, Image and Video I/O.
2/4/2011
iPAL Group Meeting
Install OpenCV
Free Download: http://sourceforge.net/projects/opencvlibrary/ Follow the Installation Guide http://opencv.willowgarage.com/wiki/InstallGuide. Its sort of complicated. Congure and test Microsoft Visual Studio .net 2008/2010.
2/4/2011
iPAL Group Meeting
Image data structure
Figure: Image data structure
2/4/2011 iPAL Group Meeting 7
Image data structure
Figure: Matrices and vectors data structure
2/4/2011 iPAL Group Meeting 8
Reading and writing images
IplImage cvLoadImage(image path, colorness f lag ); loads image from le, converts to color or grayscle, if need. Supported image formats: BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM,SR, RAS, TIFF, TIF ag: > 0 the loaded image is forced to be a 3-channel color image = 0 the loaded image is forced to be a 1 channel grayscale image < 0 the loaded image is loaded as is (with number of channels in the le). cvSaveImage(image path, image); saves image to le, image format is determined from extension. IplImage img = cvLoadImage(picture.jpeg, 1); if (img ) cvSaveImage(picture.png, img );
2/4/2011
iPAL Group Meeting
Accessing image elements
For a multi-channel oat image IplImage img = cvCreateImage(cvSize(640, 480), IP L DEP T H 32F, 3); intheight = img > height; intwidth = img > width; intstep = img > widthStep/sizeof (f loat); intchannels = img > nChannels; f loat data = (f loat)img > imageData; data[i step + j channels + k ] = 111; IplImage cvCreateImage(CvSizesize, intdepth, intchannels); depth: pixel depth in bits: IPL DEPTH 8U, IPL DEPTH 8S, IPL DEPTH 16U, IPL DEPTH 16S, IPL DEPTH 32S, IPL DEPTH 32F, IPL DEPTH 64F
2/4/2011
iPAL Group Meeting
10
Some important functions in image processing
Convert between color spaces: cvCvtColor(src, dst, code); from src to dst
Example: cvCvtColor(cimg, gimg, CV BGR2GRAY ); Convert the color image cimg into gray image gimg
2/4/2011
iPAL Group Meeting
11
Working with matrices
Allocate a matrix: CvM at cvCreateM at(introws, intcols, inttype); Direct matrix element access: IplImage img = cvCreateImage(cvSize(640, 480), IP L DEP T H 32F, 3); CvM at M = cvCreateM at(4, 4, CV 32F C 1); intn = M > cols; f loat data = M > data.f l; data[i n + j ] = 3.0; Set the element Mij = 3.0
2/4/2011
iPAL Group Meeting
12
Some important functions in Matrix/vector operations
Matrix-matrix operations: cvAdd(M a, M b, M c); M c = M a + M c cvSub(M a, M b, M c); M c = M a M c cvM atM ul(M a, M b, M c); M c = M a M c Inhomogeneous linear system solver: cvSolve(&A, &b, &x); solve (Ax = b) for x Eigen analysis (of a symmetric matrix): cvEigenV V (&A, &E, &l); l: eigenvalues of A; E: corresponding eigenvectors
2/4/2011
iPAL Group Meeting
13
Example 1: Histograms
Loading the Image: char imageN ame = Critters 00005.JP G; IplImage im = cvLoadImage(imageN ame, 1); Specifying a Working Region: IplImage grayImage = cvCreateImage(cvSize(im > width, im > height), IP L DEP T H 8U, 1); CvRectrect = cvRect(0, 0, 500, 600); cvSetImageROI (grayImage, rect); apply the rectangle to the image and establish a region of interest
2/4/2011
iPAL Group Meeting
14
Result of Specifying a Working Region
(a) Original image
(b) Specic Working Region
Figure: Result of Specifying a Working Region
2/4/2011
iPAL Group Meeting
15
Example 1: Histograms
Perform Initial Histogram Calculations: IplImage histImage = cvCreateImage(cvSize(320, 200), 8, 1); CvHistogram hist = cvCreateHist(1, &hist size, CV HIST ARRAY, ranges, 1); cvCalcHist(&grayImage, hist, 0, N U LL); Calculate the Histogram
cvGetM inM axHistV alue(hist, &min value, &max value, &min idx, &m cvScale(hist > bins, hist > bins, ((double)histImage > height)/max value, 0); cvSet(histImage, cvScalarAll(255), 0); bin w = cvRound((double)histImage > width/hist size); Grab Min/Max Values and Set Up Factors For Visualization
2/4/2011
iPAL Group Meeting
16
Result of Histograms
Get Values/Perform Calculations f or(i = 0; i < hist size; i + +){. . . } Display Results
Figure: Result of Histograms
2/4/2011
iPAL Group Meeting
17
Example 2: Image Correlation
Extract Template Region CvRectrect = cvRect(xV al, yV al, neighLength, neighLength); CvM at tplate = cvCreateM at(neighLength, neighLength, CV8 U C 1); cvGetSubRect(imG, tplate, rect);
(a) Original image
(b) Template Region
Figure: Two Subgures
2/4/2011 iPAL Group Meeting 18
Example 2: Image Correlation
Template Match cvM atchT emplate(imG, tplate, result0, CV T M SQDIF F ); cvM atchT emplate(imG, tplate, result2, CV T M CCORR);
(a) Result of SQDIFF
(b) Result of CCORR
Figure: Result of general correlation
The result is poor
2/4/2011 iPAL Group Meeting 19
Example 2: Image Correlation
Use normalized result cvM atchT emplate(imG, tplate, result1, CV T M SQDIF F N ORM ED cvM atchT emplate(imG, tplate, result2, CV T M CCORR N ORM ED);
(a) SQDIFF NORMED
(b) CCORR NORMED
Figure: Result of normalized correlation
The result is good. It showed that the normalized techniques exhibited better results.
2/4/2011 iPAL Group Meeting 20
Demos
edge detection histogram ellipse tting delaunay convexhull
2/4/2011
iPAL Group Meeting
21