Image Processing Using MATLAB
Image Processing Using MATLAB
www.techsource.com.my
www.techsource.com.my
Course Outline:
1. Working with Images in MATLAB
a) Image types and classes
b) Read/write images
c) Display images
Section Outline:
1. Image types
• Index images
• Intensity images
• Binary images
• RGB images
Image Types
• Four basic types of images are supported in
MATLAB
• Index images: m-by-3 colormap matrix
• Intensity images: [0 1] or uint8
• Binary images: {0, 1}
• RGB images: m-by-n-by-3 matrix
>> imshow(intenImg)
>> imshow(bwImg)
>> imshow(rgbImg)
©2005 Systems Sdn. Bhd.
>> imfinfo(‘canoe.tif’)
>> [X, map] = imread(‘canoe.tif’);
>> imshow(X, map);
>> imwrite(X, map, ‘canoe2.bmp’);
Colormap?
imfinfo('file.fmt')
file.fmt x=imread('file.fmt');
[x,map]=imread('file.fmt');
Im age Intensity Binary RGB
Image Indexed Type
Type Double
1 0 1 0.9 0.4
1 0.5 0.2
Double Data 0 0.1 0.9 0 1 0
0.1 0.7
0.5 0.3
0.2
1 0.5 0.3
Data [MXN] + 0 0.7 0.8 0 1 1
0.9 0.2
Uint8 90 140
Uint8 Data 1 45 220
100 78 110
1 0 1 225 30
100 70
0 1 0
Data [MXN] + 200 7 98 0 1 1
22 230
10 50
109 220
Displaying Images
• imshow - Display image.
• image - Create and display image object (MATLAB).
• imagesc - Scale data and display as image (MATLAB).
Montage
• An example of displaying multiple image frames as a
rectangular montage.
Warping
• The warp function allows you to display an image as
a texture-mapped surface.
Extra credit:
1. Use subplots to display all three images.
2. Did you find the "Easter egg" in the "trees"?
% Easter egg
>> figure
>> [I2,map] = imread('trees.tif',2);
>> imshow(I2,map)
Section Summary:
1. Image types
• Index images
• Intensity images
• Binary images
• RGB images
Section Outline:
1. Image enhancement
• Image histogram
• Image contrast adjustment
• Image brightness adjustment
2. Image thresholding
3. Image arithmetic
Image Enhancement
• One of the most basic ways to enhance an image is
to change its brightness and its contrast, and this
can be done is by working with the image's
histogram.
Histogram
• A histogram of an image shows the current level of
contrast (the distribution of gray levels).
>> I = imread('pout.tif');
>> imshow(I)
>> figure, imhist(I)
Histogram Stretching
• One way to increase the contrast of an image is to
stretch the pixel values (min == 0 and max == 255).
I − I min
J = 255 ⋅
I max − I min
Histogram Equalization
• The histeq function can be used to equally
distribute the histogram and enhance the contrast.
>> J = histeq(I);
Histogram Adjustment
• Intensity adjustment is a technique for mapping an
image's intensity values to a new range (imadjust).
>> I = imread('cameraman.tif');
>> J = imadjust(I,[0 0.2],[0.5 1]);
>> imshow(I)
>> figure, imshow(J)
>> imadjdemo
Image Thresholding
• The adjusted cameraman image can be thresholded to
create a black and white image of the cameraman and
the camera.
Image Arithmetic
• With just simple addition, subtraction, multiplication and
division a number of different image processing techniques can
be implemented.
– With addition and multiplication an image contrast can be
increased that facilitates edge detection process.
– With subtraction and division changes can be detected from one
image to another.
Image Addition
• Image addition makes it possible to superimpose an
image on top of another or control the brightness of an
image.
• Each resulting pixel is the sum of the respective pixels
of the two images, of the same size and of the same
class.
>> I1 = imread(‘peppers.png’);
>> I2 = imadd(I1, 50);
>> subplot(2,1,1), imshow(I1)
>> subplot(2,1,2), imshow(I2)
>> I = imread(‘rice.png’);
>> J = imread(‘cameraman.tif’);
>> K = imadd(I, J);
>> imshow(K)
Image Multiplication
>> I = imread(‘moon.tif’);
>> J = immultiply(I, 1.2);
>> subplot(1,2,1),imshow(I)
>> subplot(1,2,2),imshow(J)
Image Subtraction
• Can you see anything different about the two images?
Section Summary:
1. Image enhancement
• Image histogram
• Image contrast adjustment
• Image brightness adjustment
2. Image thresholding
3. Image arithmetic
Section Outline:
1. What is block processing?
2. Distinct block operations
3. Sliding neighbourhood operations
4. Example of block processing
• Convolution
• Correlation
5. Column processing
Single
block n Zero-padding
B = blkproc(A,[m n],fun)
©2005 Systems Sdn. Bhd.
Center pixel
n
Neighborhood
m
>> A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
>> h = [8 1 6
3 5 7
4 9 2]
>> A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
>> h = [8 1 6
3 5 7
4 9 2]
>> filter2(h,A)
Column Processing
Arranging each sliding neighborhood or distinct block as
separate columns, and process each column as a block.
• Faster, since most of MATLAB is column-based.
• But uses more memory.
Syntax
B = colfilt(A,[m n],block_type,fun)
A – image matrix
[m n] – size of block
block_type – Either 'distinct' or 'sliding'
fun – function to apply
>> I1 = imread('tire.tif');
>> f = @(x) uint8(round(std(double(x))));
>> I2 = colfilt(I1,[3 3],'sliding',f);
>> subplot(1,2,1), imshow(I1)
>> subplot(1,2,2), imshow(I2)
Section Summary:
1. What is block processing?
2. Distinct block operations
3. Sliding neighbourhood operations
4. Example of block processing
• Convolution
• Correlation
5. Column processing
Section Outline:
1. Reducing noise
• Filters
• Region-based processing
2. Image alignment
• Rotation
• Cropping
• Resizing
Reducing Noise
Where does noise come from?
• Scanner resolution
• Film grain (granularity)
• Hardware (interference patterns)
• Other
Common types of noise
• Whitenoise (Gaussian)
• Local variance (Gaussian with intensity-dependent
variance)
• Salt and pepper
• Speckle
Syntax:
J = imnoise(I,type,parameters)
I – Image
type – gaussian, localvar, poisson,
salt & pepper, speckle
parameters – additional parameters needed
given the type of noise
Linear Filtering
A linear filter computes each output pixel value according
to a linear combination of the input pixel's neighborhood.
• The basics of linear filtering are done through
correlation and convolution.
A – Input image
H – The filter, also known as the
correlation/convolution kernel
options – Boundary options, output size option,
correlation and convolution option
Averaging Filter
A very basic example of a linear filter is an averaging filter.
>> I = imread('cameraman.tif');
>> % addition of graininess (i.e. noise)
>> I_noise = imnoise(I, 'speckle', 0.01);
>> % the average of 3^2, or 9 values
>> h = ones(3,3) / 3^2;
>> I2 = imfilter(I_noise,h);
>> subplot(1,2,1), imshow(I_noise), title('Original image')
>> subplot(1,2,2), imshow(I2), title('Filtered image')
Syntax
h = fspecial(type, parameter)
Median Filtering
When noise causes the pixels to vary greatly from
the original value (salt and pepper), a median filter
is more effective in reducing the noise.
Syntax
B = medfilt2(A,[m n])
A – Input image
B – Output image
[m n] – Neighborhood block size to be used to
calculate the median.
Adaptive Filter
The wiener2 adaptive filter tailors itself to the local image
variance adaptively. If the variance is large, it minimally
smoothes the image. If the variance is small, it performs more
smoothing.
This type of filter is effective in reducing the effects of
Gaussian whitenoise.
Syntax
I – Input image
[m n] – Size neighborhood block used to calculate the
median.
J – Output image
noise – Noise variance
Filter Demonstration
Explore noise reduction in images using linear and non-
linear filtering techniques by running the following demo:
>> nrfiltdemo
Region-Based Processing
Region-based processing allows you to select a region of
interest (ROI), and process only upon the selected area.
Filtering a Region
Once a region has been specified, a filter can be created
and implemented on the region.
>> I = imread('cameraman.tif');
>> imshow(I)
>> BW = roipoly;
>> figure, imshow(BW)
>> h = fspecial('unsharp');
>> I2 = roifilt2(h,I,BW);
>> imshow(I2)
Image Alignment
When the alignment is off in an image, you can apply
some basic spatial transformation techniques.
Rotate the image (imrotate)
Crop the image (imcrop)
Resize the image (imresize)
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
counter-clockwise 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]
Image Resizing
Syntax
B = imresize(A,m,method)
B – Output image
A – Input image
m – Magnification factor
method – Type of interpolation:
[{nearest}, bilinear, bicubic]
Section Summary:
1. Reducing noise
• Filters
• Region-based processing
2. Image alignment
• Rotation
• Cropping
• Resizing
Section Outline:
1. Morphology and segmentation
2. Structuring element
3. Dilation and erosion
4. Edge detection
Example Problem
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.
The Image Processing Toolbox provides a function
for creating a structuring element that can be used,
strel.
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
SE =
Flat STREL object containing 25 neighbors.
Decomposition: 3 STREL objects containing a total of 13
neighbors
Neighborhood:
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
1 1 1 1 1 1 1
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
Syntax
IM2 = imdilate(IM,SE)
IM2 = imerode(IM,SE)
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
To locate the chips on the integrated circuit we can use
the morphological techniques of erosion and dilation.
Edge Detection
The edge function of the Image Processing Toolbox
makes it easy to perform edge detection.
>> I = imread('circuit.tif');
>> BW1 = edge(I,'sobel');
>> BW2 = edge(I,'canny');
>> subplot(131),imshow(I),title(‘Original’)
>> subplot(132),imshow(BW1),title(‘sobel’)
>> subplot(133),imshow(BW2),title(‘canny’)
>> edgedemo
Section Summary:
1. Morphology and segmentation
2. Structuring element
3. Dilation and erosion
4. Edge detection
Motion Detection
Section Outline:
1. Motion detection
[motion_detect.m]
2. Object tracking
[BounceJasc.m]
3. Text recognition
[recognize_text.m]
www.techsource.com.my
www.techsource.com.my
www.techsource.com.my
www.techsource.com.my
www.techsource.com.my
The End
Kindly return your Evaluation Form