opencv tutorial i: image processing - information processing and
TRANSCRIPT
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 2
What is OpenCV?
OpenCV means Intel Open Source Computer Vision Library.
It is a collection of C functions and a few C++ classes thatimplement some popular Image Processing and Computer Visionalgorithms.
The key features: Cross-Platform API of C functions FREE forcommercial and non-commercial uses.
2/4/2011 iPAL Group Meeting 3
Why to use it?
Newton says:’If I have seen a little further it is by standing on theshoulders of Giants.’.
When we have to implement something into production, we need touse C language program.
We can take advantage of high speed implementations of functionscommonly used in Computer Vision/Image Processing.
2/4/2011 iPAL Group Meeting 4
OpenCV modules
cv: Main OpenCV functions, Image processing and visionalgorithms.
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 5
Install OpenCV
Free Download:http://sourceforge.net/projects/opencvlibrary/
Follow the Installation Guidehttp://opencv.willowgarage.com/wiki/InstallGuide.It’s sort of complicated.
Configure and test Microsoft Visual Studio .net 2008/2010.
2/4/2011 iPAL Group Meeting 6
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 flag);loads image from file, converts to color or grayscle, if need.Supported image formats:BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM,SR, RAS,TIFF, TIFflag:> 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 thefile).
cvSaveImage(image path, image);saves image to file, image format is determined from extension.IplImage ∗ img = cvLoadImage(picture.jpeg,−1);if(img) cvSaveImage(picture.png, img);
2/4/2011 iPAL Group Meeting 9
Accessing image elements
For a multi-channel float imageIplImage ∗ img =cvCreateImage(cvSize(640, 480), IPL DEPTH 32F, 3);intheight = img− > height;intwidth = img− > width;intstep = img− > widthStep/sizeof(float);intchannels = img− > nChannels;float ∗ data = (float∗)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:CvMat ∗ cvCreateMat(introws, intcols, inttype);
Direct matrix element access:IplImage ∗ img =cvCreateImage(cvSize(640, 480), IPL DEPTH 32F, 3);CvMat ∗M = cvCreateMat(4, 4, CV 32FC1);intn = M− > cols;float ∗ data = M− > data.fl;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(Ma,Mb,Mc); Mc = Ma+MccvSub(Ma,Mb,Mc); Mc = Ma−MccvMatMul(Ma,Mb,Mc); Mc = Ma×Mc
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 ∗ imageName = ”Critters 00005.JPG”;IplImage ∗ im = cvLoadImage(imageName,−1);
Specifying a Working Region:IplImage ∗ grayImage = cvCreateImage(cvSize(im− >width, im− > height), IPL DEPTH 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) Specific 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, NULL);Calculate the Histogram
cvGetMinMaxHistV alue(hist,&min value,&max value,&min idx,&max idx);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 Calculationsfor(i = 0; i < hist size; i++){. . . }
Display Results
Figure: Result of Histograms
2/4/2011 iPAL Group Meeting 17
Example 2: Image CorrelationExtract Template RegionCvRectrect = cvRect(xV al, yV al, neighLength, neighLength);CvMat ∗ tplate =cvCreateMat(neighLength, neighLength, CV8UC1);cvGetSubRect(imG, tplate, rect);
(a) Original image (b) Template Region
Figure: Two Subfigures
2/4/2011 iPAL Group Meeting 18
Example 2: Image Correlation
Template MatchcvMatchTemplate(imG, tplate, result0, CV TM SQDIFF );cvMatchTemplate(imG, tplate, result2, CV TM 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 CorrelationUse normalized resultcvMatchTemplate(imG, tplate, result1, CV TM SQDIFF NORMED);cvMatchTemplate(imG, tplate, result2, CV TM CCORR NORMED);
(a) SQDIFF NORMED (b) CCORR NORMED
Figure: Result of normalized correlation
The result is good. It showed that the normalized techniquesexhibited better results.
2/4/2011 iPAL Group Meeting 20
Demos
edge detection
histogram
ellipse fitting
delaunay
convexhull
2/4/2011 iPAL Group Meeting 21