tutorials on opencv and matlab

29
1 Tutorials on OpenCV and Matlab Jiawei Huang Jan. 18, 2010

Upload: krishna-vinay

Post on 08-Apr-2015

445 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Tutorials on OpenCV and Matlab

1

Tutorials on OpenCV and Matlab

Jiawei HuangJan. 18, 2010

Page 2: Tutorials on OpenCV and Matlab

2

Page 3: Tutorials on OpenCV and Matlab

3

OpenCV

● OpenCV is an open source C/C++ library for image processing and computer vision.

● In 2006, Intel released the first stable version of OpenCV

● In 2008, OpenCV obtained corporate support from Willow Garage

● October 2009, the second major release of the OpenCV – OpenCV v2!● http://opencv.willowgarage.com/wiki/

Page 4: Tutorials on OpenCV and Matlab

4

OpenCV

● OpenCV has Linux, Mac, and Windows versions● OpenCV has basic data structures for matrix 

operation and image processing● OpenCV has a bunch of functions that can be 

used to process visual data and extract information for images/videos

● OpenCV also has some functions for image capturing and display

Page 5: Tutorials on OpenCV and Matlab

5

OpenCV Modules

● cv● Main OpenCV functions

● cvaux● Auxiliary (experimental) OpenCV functions

● cxcore● Data structures and linear algebra support

● highgui● GUI functions

Page 6: Tutorials on OpenCV and Matlab

6

OpenCV Naming Conventions

● Function naming conventionscvActionTargetMod(...)

Action = the core functionality (e.g. set, create)Target = the target image area (e.g. contour, polygon)Mod    = optional modifiers (e.g. argument type)

● Matrix data typesCV_<bit_depth>(S|U|F)C<number_of_channels>

S = Signed integerU = Unsigned integerF = Float 

E.g.: CV_8UC1: an 8­bit unsigned single­channel matrix,       CV_32FC2: a 32­bit float matrix with two channels.

Page 7: Tutorials on OpenCV and Matlab

7

OpenCV Naming Conventions (cont'd)

● Image data typesIPL_DEPTH_<bit_depth>(S|U|F)

E.g.: IPL_DEPTH_8U: an 8­bit unsigned image.      IPL_DEPTH_32F: a 32­bit float image.

● Header files#include <cv.h>#include <cvaux.h>#include <highgui.h>  #include <cxcore.h>   // unnecessary ­ included in cv.h

Page 8: Tutorials on OpenCV and Matlab

8

A VERY Simple OpenCV Program#include "cv.h"

#include "highgui.h"

void main()

{

    IplImage *image = cvLoadImage("SFU.bmp");

    cvNamedWindow("SFU Image", CV_WINDOW_AUTOSIZE);

    cvShowImage("SFU Image", image);

    cvWaitKey(0);

    cvReleaseImage(&image);

}

Page 9: Tutorials on OpenCV and Matlab

9

Some Explanations

● Load an imageIplImage *image = cvLoadImage("SFU.bmp");

● Create and position a windowcvNamedWindow("SFU Image", CV_WINDOW_AUTOSIZE);cvMoveWindow("SFU Image", 100, 100) // from UL corner

● Display an imagecvShowImage("SFU Image", image);

● Release the resourcescvReleaseImage(&image);

Page 10: Tutorials on OpenCV and Matlab

10

Basic Image operations

● Allocate an imageIplImage* cvCreateImage(CvSize size, int depth, int channels);

size:  cvSize(width,height);

depth: pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_32F 

channels: Number of channels per pixel. Can be 1, 2, 3 or 4. The channels are interleaved. The usual data layout of a color image is b0 g0 r0 b1 g1 r1 …

Examples:// Allocate a 1­channel byte imageIplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); 

// Allocate a 3­channel float imageIplImage* img2=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);

Page 11: Tutorials on OpenCV and Matlab

11

Basic Image operations (cont'd)

● Releasing an imagecvReleaseImage(&img);

● Clone an imageIplImage*img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); IplImage* img2;img2=cvCloneImage(img1);

● Set/get region of interest (ROI)void  cvSetImageROI(IplImage* image, CvRect rect);vRect cvGetImageROI(const IplImage* image);

Page 12: Tutorials on OpenCV and Matlab

12

Color Image Manipulation

● Get pixel values from an imageCvScalar s = cvGet2D(img, row, col)

If the image is grayscale, s.val[0] is the pixel value

If the image is color, s.val[0], s.val[1], and s.val[2] are R, G, and B.

● Set pixel values in an imageCvScalar s;s.val[0] = v; // for grayscale imagecvSet2D(img, row, col, s);

x     X (col)

Y (row)

y

Page 13: Tutorials on OpenCV and Matlab

13

Color Image Manipulation (cont'd)

● Convert color spacecvCvtColor(color_img, gray_img, CV_BGR2GRAY);

● Convert between color spacescvSplit(src_img, dst_img, code);

code = CV_<X>2<Y><X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS

e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab

Page 14: Tutorials on OpenCV and Matlab

14

Video Capturing

● OpenCV supports capturing images from a camera or a video file (AVI)

● Initializing capture from a camera// capture from video device #0CvCapture* capture = cvCaptureFromCAM(0); 

● Initializing capture from a fileCvCapture* capture = cvCaptureFromAVI("infile.avi");

Page 15: Tutorials on OpenCV and Matlab

15

Video Capturing (cont'd)

● Capturing a frameIplImage* img = 0; if(!cvGrabFrame(capture)){  // capture a frame 

printf("Could not grab a frame\n");exit(0);

}img=cvRetrieveFrame(capture);  // retrieve the frame

● Releasing the capture sourcecvReleaseCapture(&capture);

Page 16: Tutorials on OpenCV and Matlab

16

About OpenCV

● OpenCV's code is highly optimized. It is very fast and efficient,

● The documentation is bad!!!● There is no protection for errors. Thus, prepare 

to crash your program a lot when you debug.● Overall OpenCV is a good library. It is also a 

good tool if you want to implement realtime systems.

Page 17: Tutorials on OpenCV and Matlab

17

Questions?

Page 18: Tutorials on OpenCV and Matlab

18

Matlab

Page 19: Tutorials on OpenCV and Matlab

19

Matlab

● Matlab integrates computation, visualization, and programming in a easy­to­use environment

● Matlab has been widely used in universities and research institutes for● Math and computation● Algorithm development● Modeling, simulation, and prototyping● Scientific and engineering graphics

Page 20: Tutorials on OpenCV and Matlab

20

Matrices in Matlab

● Matrix is a universal data type in Matlab● For instance, we have a matrix

A = [1, 2; 3, 4], thenA's elements A(2, 1) is 3A(:, 1) = [1; 3]'A(:, 2) = [2; 4]'A(1, :) = [1, 2]A(2, :) = [3, 4]A(:) = [1; 2; 3; 4];

Page 21: Tutorials on OpenCV and Matlab

21

Matrix Operations are Built in

● A * B, (see the difference to A .* B)● The transpose of A is A'● The inverse of A is inv(A), Assuming A is 

invertible. ● Eig(A) returns A's eigenvectors and 

eigenvalues

Page 22: Tutorials on OpenCV and Matlab

22

Image Processing using Matlab

● Matlab has an image processing toolbox that supports different image operations

● To read an imageI = imread('SFU.bmp');% convert uint8 to float in [0, 1]I = im2double(I);  

● If I is a color image, it will have 3 channels● The red channel I(:, :, 1), green channel I(:, :, 2), and blue channel I(:, :, 3)

Page 23: Tutorials on OpenCV and Matlab

23

Image Processing using Matlab (cont'd)

● A color image can be converted to a grayscale one usinggray = rgb2gray(I);

● To display an imageimshow(I);

● To save an imageimwrite(I, 'out.bmp');

Page 24: Tutorials on OpenCV and Matlab

24

Flow Control in Matlab

● The if conditional blockif condition…end

● The for loop blockfor n = list…end

Page 25: Tutorials on OpenCV and Matlab

25

Flow Control in Matlab (cont'd)

● The while blockwhile condition…end

● The do­while blockdo…while condition

Page 26: Tutorials on OpenCV and Matlab

26

Script and Function

● A Script does not have arguments and return values. ● It is just a group of commands.

● Functions has arguments and usually with a return listFunction rt_val_list = f(param_list)

… (function body)

● Usually function is stored as a m file named as f.m● Functions can have no return list

Page 27: Tutorials on OpenCV and Matlab

27

Performance Optimization

● Matlab is slow while doing iterations. Try to vectorize the process instead of using iterations.

● Pre­allocate memory for a matrix● Rewrite bottleneck procedures using c

● System('c_binary');

● Compile the function using mex

Page 28: Tutorials on OpenCV and Matlab

28

Other Tools

● FLTK/QT/Wxwidgets● http://www.fltk.org/● http://qt.nokia.com/products● http://www.wxwidgets.org/

● DirectX, DirectShow● JMF (Java Media Framework API)

● link to the website

Page 29: Tutorials on OpenCV and Matlab

29

Questions?