21
OpenCV Tutorial I: Image Processing Xuan Mo iPAL Group Meeting February 4, 2011

OpenCV Tutorial I: Image Processing - Information Processing and

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OpenCV Tutorial I: Image Processing - Information Processing and

OpenCV Tutorial I: Image Processing

Xuan Mo

iPAL Group Meeting

February 4, 2011

Page 2: OpenCV Tutorial I: Image Processing - Information Processing and

Outline

Introduction

Data structure

Important functions

Examples

Demos

2/4/2011 iPAL Group Meeting 2

Page 3: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 4: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 5: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 6: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 7: OpenCV Tutorial I: Image Processing - Information Processing and

Image data structure

Figure: Image data structure

2/4/2011 iPAL Group Meeting 7

Page 8: OpenCV Tutorial I: Image Processing - Information Processing and

Image data structure

Figure: Matrices and vectors data structure

2/4/2011 iPAL Group Meeting 8

Page 9: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 10: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 11: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 12: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 13: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 14: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 15: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 16: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 17: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 18: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 19: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 20: OpenCV Tutorial I: Image Processing - Information Processing and

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

Page 21: OpenCV Tutorial I: Image Processing - Information Processing and

Demos

edge detection

histogram

ellipse fitting

delaunay

convexhull

2/4/2011 iPAL Group Meeting 21