Download - Tutorials on OpenCV and Matlab
1
Tutorials on OpenCV and Matlab
Jiawei HuangJan. 18, 2010
2
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/
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
5
OpenCV Modules
● cv● Main OpenCV functions
● cvaux● Auxiliary (experimental) OpenCV functions
● cxcore● Data structures and linear algebra support
● highgui● GUI functions
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 8bit unsigned singlechannel matrix, CV_32FC2: a 32bit float matrix with two channels.
7
OpenCV Naming Conventions (cont'd)
● Image data typesIPL_DEPTH_<bit_depth>(S|U|F)
E.g.: IPL_DEPTH_8U: an 8bit unsigned image. IPL_DEPTH_32F: a 32bit float image.
● Header files#include <cv.h>#include <cvaux.h>#include <highgui.h> #include <cxcore.h> // unnecessary included in cv.h
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);
}
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);
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 1channel byte imageIplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
// Allocate a 3channel float imageIplImage* img2=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
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);
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
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
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");
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);
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.
17
Questions?
18
Matlab
19
Matlab
● Matlab integrates computation, visualization, and programming in a easytouse 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
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];
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
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)
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');
24
Flow Control in Matlab
● The if conditional blockif condition…end
● The for loop blockfor n = list…end
25
Flow Control in Matlab (cont'd)
● The while blockwhile condition…end
● The dowhile blockdo…while condition
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
27
Performance Optimization
● Matlab is slow while doing iterations. Try to vectorize the process instead of using iterations.
● Preallocate memory for a matrix● Rewrite bottleneck procedures using c
● System('c_binary');
● Compile the function using mex
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
29
Questions?