Opencv Tutorial I: Image Processing: Xuan Mo
Opencv Tutorial I: Image Processing: Xuan Mo
Xuan Mo
iPAL Group Meeting
February 4, 2011
Outline
2/4/2011
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
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
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
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
2/4/2011
2/4/2011
10
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
11
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
12
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
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
14
2/4/2011
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
16
Result of Histograms
Get Values/Perform Calculations f or(i = 0; i < hist size; i + +){. . . } Display Results
2/4/2011
17
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);
The result is good. It showed that the normalized techniques exhibited better results.
2/4/2011 iPAL Group Meeting 20
Demos
2/4/2011
21