0% found this document useful (0 votes)
478 views29 pages

Tutorials On OpenCV and Matlab

This document provides tutorials on OpenCV and Matlab. OpenCV is an open source library for image processing and computer vision. It has C/C++ and Python APIs and supports Linux, Mac and Windows. Matlab integrates computation, visualization and programming and is widely used for math, modeling and graphics. Both OpenCV and Matlab provide functions for reading, displaying and manipulating images and videos.

Uploaded by

Krishna Vinay
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
478 views29 pages

Tutorials On OpenCV and Matlab

This document provides tutorials on OpenCV and Matlab. OpenCV is an open source library for image processing and computer vision. It has C/C++ and Python APIs and supports Linux, Mac and Windows. Matlab integrates computation, visualization and programming and is widely used for math, modeling and graphics. Both OpenCV and Matlab provide functions for reading, displaying and manipulating images and videos.

Uploaded by

Krishna Vinay
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

Tutorials on OpenCV and Matlab

Jiawei Huang
Jan. 18, 2010

1
2
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/
3
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

4
OpenCV Modules
● cv
● Main OpenCV functions
● cvaux
● Auxiliary (experimental) OpenCV functions
● cxcore
● Data structures and linear algebra support
● highgui
● GUI functions
5
OpenCV Naming Conventions
● Function naming conventions
cvActionTargetMod(...)
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 types
CV_<bit_depth>(S|U|F)C<number_of_channels>
S = Signed integer
U = Unsigned integer
F = Float 

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

6
OpenCV Naming Conventions (cont'd)
● Image data types
IPL_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

7
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);
}
8
Some Explanations
● Load an image
IplImage *image = cvLoadImage("SFU.bmp");

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

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

● Release the resources
cvReleaseImage(&image);

9
Basic Image operations
● Allocate an image
IplImage* 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 image
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); 

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

10
Basic Image operations (cont'd)
● Releasing an image
cvReleaseImage(&img);

● Clone an image
IplImage*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);

11
Color Image Manipulation
● Get pixel values from an image
CvScalar 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.
x     X (col)
● Set pixel values in an image
CvScalar s; y
s.val[0] = v; // for grayscale image
cvSet2D(img, row, col, s);
Y (row)

12
Color Image Manipulation (cont'd)
● Convert color space
cvCvtColor(color_img, gray_img, CV_BGR2GRAY);

● Convert between color spaces
cvSplit(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

13
Video Capturing
● OpenCV supports capturing images from a 
camera or a video file (AVI)
● Initializing capture from a camera
// capture from video device #0
CvCapture* capture = cvCaptureFromCAM(0); 

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

14
Video Capturing (cont'd)
● Capturing a frame
IplImage* 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 source
cvReleaseCapture(&capture);

15
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.

16
Questions?

17
Matlab

18
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

19
Matrices in Matlab
● Matrix is a universal data type in Matlab
● For instance, we have a matrix
A = [1, 2; 3, 4], then
A's elements A(2, 1) is 3
A(:, 1) = [1; 3]'
A(:, 2) = [2; 4]'
A(1, :) = [1, 2]
A(2, :) = [3, 4]
A(:) = [1; 2; 3; 4];

20
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

21
Image Processing using Matlab
● Matlab has an image processing toolbox that 
supports different image operations
● To read an image
I = 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)
22
Image Processing using Matlab (cont'd)

● A color image can be converted to a grayscale 
one using
gray = rgb2gray(I);
● To display an image
imshow(I);
● To save an image
imwrite(I, 'out.bmp');

23
Flow Control in Matlab
● The if conditional block
if condition

end
● The for loop block
for n = list

end

24
Flow Control in Matlab (cont'd)
● The while block
while condition

end
● The do­while block
do

while condition

25
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 list
Function 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
26
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

27
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

28
Questions?

29

You might also like