Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Image and Video Processing

with MATLAB

Dr Thang Ka Fei
Senior Lecturer
School of Engineering
ka.fei@apu.edu.my
Agenda

Introduction
Working with Images/Videos
Image Enhancement
Image Morphology and Segmentation
Case Studies
Image Processing for Engineering

Lane Deviation Warning


Security Surveillance

Smart Entertainment Robotic Vision


What Can You Do with
Image Processing

Isolate region of interest (ROI) in an image to process.


Measure the properties of objects located in an image.
Preprocess images by enhancing image characteristics,
and reducing the effects of noise and motion.
Identify and extract image features using different methods
Working with Images/Videos
Image Types: Binary Images
Image Types: Grayscale Images
Image Types: Indexed Images
Image Types: RGB Images
Importing and Exporting Images
Command line
imfinfo - Returns info about graphics file.
imread - Read image from graphics file.
imwrite - Write image to graphics file.
Displaying Images
imtool Display image in Image Tool.
imshow Display image.
colorbar Display colorbar
colormap Sets the color map of the image
montage Display multiple image frames.
subimage Display multiple images with
different colormaps in a single figure.
Displaying Multiple Images

Note The montage function requires the image to be m-by-


n-by-1-by-k for binary, intensity, and indexed images, and
m-by-n-by-3-by-k for RGB images.
Reading/Writing of Video

% Read video frames from movie file


>> mov = aviread(skull.avi);

% Display multiple video frames


>> for cnt = 1:length(mov)
movFrames(:,:,:,cnt) = mov(cnt).cdata;
end
>> montage(movFrames)

skull.avi % Write video frames into movie file


[click on image to play video] >> movNew = movFrames(:,:,:,1:50);
>> mov2 = immovie(movNew);
>> movie2avi(mov2, skull2.avi, fps,
30, quality, 100); % Make AVI file
>> aviinfo(skull2.avi); % Get file info
Finding Image Characteristics
Pixel Values:
impixel Data values for selected pixels
improfile Data values along a path in an
image
Statistics:
min/max Minimum and maximum value of
an image
mean2 Mean value of the image data
values
std2 Standard deviation of the image
data values
corr2 Correlation between two images
Converting Image Formats

ind2gray Indexed image to grayscale image.


ind2rgb Indexed image to RGB image (MATLAB).
gray2ind Grayscale image to indexed image.
rgb2gray RGB image or colormap to grayscale.
rgb2ind RGB image to indexed image.
im2bw Image to binary image by thresholding.
Image Enhancement
Image Enhancement
One of the most basic ways to enhance an image is to
change its brightness and its contrast. This can be done
by

Stretching the color distribution


Equalizing the distribution of colors to use the full
range
Adjusting the scaling of the colors
Histogram Stretching
I I min
J = 255
I max I min
Histogram Equalization
>> J = adapthisteq(I);
Histogram Adjustment
Image Arithmetic

The following arithmetic symbols (+,-,.*,./) can be used from


MATLAB to perform addition, subtraction, multiplication,
and division of image data.
Image Addition

+
Image Addition (Continued)

+
Image Multiplication

*
Image Subtraction
Can you see anything different about the two images?
Image Subtraction (Continued)
Spatial Transformations

imrotate Rotate an image by a certain


amount of degrees
imcrop Crop an image based on
coordinates that highlight the
area you want cropped
imresize Resize an image
Image Rotation
Syntax
B = imrotate(A,angle,method)
B = imrotate(A,angle,method,'crop')

B Output image
A Input image
angle Degrees of rotation in the
counterclockwise direction
method Type of interpolation: [{nearest},
bilinear,bicubic]
'crop' Returns only central portion of B, which
is the same size as A.
Image Cropping
Syntax
I2 = imcrop(I,rect)

I2 Output image
I Input image
rect Spatial coordinates of [xmin ymin
width height]

If rect is omitted, you specify the crop region on the


image directly using the mouse.
Image Resizing
Syntax
B = imresize(A,m,method)

B Output image
A Input image
m Magnification factor
method Type of interpolation: [{bicubic},
bilinear,nearest]

Note The second input argument for imresize could be


not only a magnification factor but it could either be
[mrows ncols] or a scale value for resizing image data.
Image Morphology and Segmentation
Morphology and Segmentation
Morphological techniques can help to identify and
segment objects in images

Morphology technique used for processing


image based on shapes.

Segmentation the process used for identifying


objects in an image.
Image Thresholding
Example Problem: Morphology
We are assigned to locate all the rectangular chips on a
black and white integrated circuit image, but how?
Structuring Element
To process an image according to a given shape, we
need to first define the shape, or structuring element.

A structuring element can be created by using the strel


function.
Syntax
SE = strel(shape,parameters)

SE Structuring element
shape Flat ['arbitrary' 'pair' 'diamond'
'periodicline ' 'disk' 'rectangle' 'line'
'square' 'octagon']
Nonflat ['arbitrary' 'ball']
parameters Associated with the selected shape
Structuring Element (Continued)
Erosion and Dilation
Erosion

Dilation
Erosion and Dilation (Continued)

BW = BW2 =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Example Solution
Blob Analysis
BW Functions
bwarea Returns areas of objects
bwareaopen Removes small objects
bwboundaries Traces region boundaries
bwdist Computes distances between
pixels
bwperim Finds perimeter of objects

bwselect Selects objects


bwtraceboundary Traces objects
bwpack Accelerates some binary
operations
bwhitmiss Performs the hit miss operation
bwmorph Performs morphological
operations
Flood-Fill Operations
The imfill function performs a flood-fill operation on binary
and grayscale images, by bringing the intensity values of
dark areas surrounded by lighter areas to the same lighter
intensity.
Example: Edge Detection
Case Studies
Finding Particles
Problem Description: Identify particles in a microscopic
image using morphology.
Key Concepts: Thresholding and morphology
Key Functions: im2bw, graythresh, strel, imopen, and
bwperim
Ball Tracking
Problem Description: Track the motion of a ping-pong
ball in a series of image frames.
Key Concepts: Working with multiimage TIFF files,
object identification (morphology), movie creation, and
object properties
Key Functions: imfinfo, montage, immovie, imerode,
bwlabel, and regionprops

You might also like