You are on page 1of 26

Image processing using MATLAB

1
Contents
 What is MATLAB?
 Image reading and showing
 Image Histogram
 Histogram equalization
 Noise add and filtering
 Salt & pepper noise -> median filtering
 Gaussian noise -> mean filtering
 Thresholding
 Feature extraction
 Edge detection
 Sobel operator
 LoG
 Hough Transform

2
What is MATLAB?

 Script language
 MATrix LABoratory
 Numerical Analysis, Matrix
computation, Signal Processing
and GUI
 Advantage of MATLAB :
 "Life is too short to spend
writing DO loops"

3
Composition of MATLAB
run

M-file

command line

SIMULINK

4
Preparation of programming

 Copy images to “work” folder


 C://MATLAB/work/

5
Images for testing

kaist.jpg lib.bmp lib2.bmp

rerc.bmp tool.bmp lena.bmp

6
Image reading and showing

image_r_s.m

%image reading
I = imread( ‘tao.jpg’ );

%image showing
figure; imshow(I);

7
Image Histogram

image_histogram.m

%estimating the histogram


H = imhist( I );

%histogram showing
figure; bar(H);

8
 Histogram equalization
image_histogram_eq.m

%histogram equalization
J = histeq(I);
figure; imshow(J);

9
HISTEQ Enhance contrast using histogram equalization.
HISTEQ enhances the contrast of images by transforming the values in an intensity image, or the values in the colormap of an indexed
image, so that the histogram of the output image approximately matches a specified histogram.

J = HISTEQ(I,HGRAM) transforms the intensity image I so that the histogram of the output image J with length(HGRAM) bins approximately
matches HGRAM. The vector HGRAM should contain integer counts for equally spaced bins with intensity values in the appropriate range:
[0,1] for images of class double, [0,255] for images of class uint8, and [0,65535] for images of class uint16. HISTEQ automatically scales
HGRAM so that sum(HGRAM) = prod(size(I)). The histogram of J will better match HGRAM when length(HGRAM) is much smaller than the
number of discrete levels in I.

J = HISTEQ(I,N) transforms the intensity image I, returning in J an intensity image with N discrete levels. A roughly equal number of pixels is
mapped to each of the N levels in J, so that the histogram of J is approximately flat. (The histogram of J is flatter when N is much smaller
than the number of discrete levels in I.) The default value for N is 64.

[J,T] = HISTEQ(I) returns the gray scale transformation that maps gray levels in the intensity image I to gray levels in J.

NEWMAP = HISTEQ(X,MAP,HGRAM) transforms the colormap associated with the indexed image X so that the histogram of the gray
component of the indexed image (X,NEWMAP) approximately matches HGRAM. HISTEQ returns the transformed colormap in NEWMAP.
length(HGRAM) must be the same as size(MAP,1).

NEWMAP = HISTEQ(X,MAP) transforms the values in the colormap so that the histogram of the gray component of the indexed image X is
approximately flat. It returns the transformed colormap in NEWMAP.

NEWMAP,T] = HISTEQ(X,...) returns the gray scale transformation T that maps the gray component of MAP to the gray component of
NEWMAP.

10
Noise add and filtering
 Salt & pepper noise -> median filtering
salt_pepper_median.m

%Salt & pepper noise add


J = imnoise(I,'salt & pepper', 0.02);
figure; imshow(J);

%median filtering
f2=medfilt2(J,[3 3]);
figure; imshow(f2);

11
IMNOISE Add noise to image.
J = IMNOISE(I,TYPE,...) Add noise of a given TYPE to the intensity image I. TYPE is a string that can have one of these values:

'gaussian' Gaussian white noise with constant mean and variance


'localvar' Zero-mean Gaussian white noise with an intensity-dependent variance
'poisson' Poisson noise
'salt & pepper' "On and Off" pixels
'speckle' Multiplicative noise

Depending on TYPE, you can specify additional parameters to IMNOISE. All numerical parameters are normalized; they correspond to operations
with images with intensities ranging from 0 to 1.

J = IMNOISE(I,'gaussian',M,V) adds Gaussian white noise of mean M and variance V to the image I. When unspecified, M and V default to 0 and 0.01
respectively.

J = imnoise(I,'localvar',V) adds zero-mean, Gaussian white noise of local variance, V, to the image I. V is an array of the same size as I.

J = imnoise(I,'localvar',IMAGE_INTENSITY,VAR) adds zero-mean, Gaussian noise to an image, I, where the local variance of the noise is a function of
the image intensity values in I. IMAGE_INTENSITY and VAR are vectors of the same size, and PLOT(IMAGE_INTENSITY,VAR) plots the functional
relationship between noise variance and image intensity. IMAGE_INTENSITY must contain normalized intensity values ranging from 0 to 1.

J = IMNOISE(I,'poisson') generates Poisson noise from the data instead of adding artificial noise to the data. In order to respect Poisson statistics,
the intensities of uint8 and uint16 images must correspond to the number of photons (or any other quanta of information). Double-precision images
are used when the number of photons per pixel can be much larger than 65535 (but less than 10^12); the intensities values vary between 0 and 1
and correspond to the number of photons divided by 10^12.

J = IMNOISE(I,'salt & pepper',D) adds "salt and pepper" noise to the image I, where D is the noise density. This affects approximately
D*PROD(SIZE(I)) pixels. The default for D is 0.05.

J = IMNOISE(I,'speckle',V) adds multiplicative noise to the image I, using the equation J = I + n*I, where n is uniformly distributed random noise with
mean 0 and variance V. The default for V is 0.04.

12
MEDFILT2 Perform 2-D median filtering.

B = MEDFILT2(A,[M N]) performs median filtering of the matrix A in two dimensions.


Each output pixel contains the median value in the M-by-N neighborhood around
the corresponding pixel in the input image. MEDFILT2 pads the image with zeros
on the edges, so the median values for the points within [M N]/2 of the edges may
appear distorted.

B = MEDFILT2(A) performs median filtering of the matrix A using the default 3-by-3
neighborhood.

B = MEDFILT2(...,PADOPT) controls how the matrix boundaries are padded. PADOPT


may be 'zeros' (the default), 'symmetric', or 'indexed'. If ADOPT is 'zeros', A is
padded with zeros at the boundaries. If PADOPT is 'symmetric', A is
symmetrically extended at the boundaries. If PADOPT is 'indexed', A is padded
with ones if it is double; otherwise it is padded with zeros.

13
 Gaussian noise -> mean filtering
gaussian_mean.m

%Gaussian noise add


J = imnoise(I, 'gaussian', 0, 0.001);
figure; imshow(J);

%mean filtering
H = fspecial('average', [3 3] );
f2 = imfilter(I,H);
figure; imshow(f2);

14
FSPECIAL Create 2-D special filters.
H = FSPECIAL(TYPE) creates a two-dimensional filter H of the specified type. Possible values for TYPE are:

'average' averaging filter


'disk' circular averaging filter
'gaussian' Gaussian lowpass filter
'laplacian' filter approximating the 2-D Laplacian operator
'log' Laplacian of Gaussian filter
'motion' motion filter
'prewitt' Prewitt horizontal edge-emphasizing filter
'sobel' Sobel horizontal edge-emphasizing filter
'unsharp' unsharp contrast enhancement filter

Depending on TYPE, FSPECIAL may take additional parameters which you can supply. These parameters all have default values.

H = FSPECIAL('average',HSIZE) returns an averaging filter H of size HSIZE. HSIZE can be a vector specifying the number of rows and columns in H or a
scalar, in which case H is a square matrix. The default HSIZE is [3 3].

H = FSPECIAL('disk',RADIUS) returns a circular averaging filter (pillbox) within the square matrix of side 2*RADIUS+1. The default RADIUS is 5.

H = FSPECIAL('gaussian',HSIZE,SIGMA) returns a rotationally symmetric Gaussian lowpass filter of size HSIZE with standard deviation SIGMA (positive).
HSIZE can be a vector specifying the number of rows and columns in H or a scalar, in which case H is a square matrix. The default HSIZE is [3 3], the
default SIGMA is 0.5.

H = FSPECIAL('laplacian',ALPHA) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator. The parameter ALPHA
controls the shape of the Laplacian and must be in the range 0.0 to 1.0. The default ALPHA is 0.2.

H = FSPECIAL('log',HSIZE,SIGMA) returns a rotationally symmetric Laplacian of Gaussian filter of size HSIZE with standard deviation SIGMA (positive).
HSIZE can be a vector specifying the number of rows and columns in H or a scalar, in which case H is a square matrix. The default HSIZE is [5 5], the
default SIGMA is 0.5.

H = FSPECIAL('motion',LEN,THETA) returns a filter to approximate, once convolved with an image, the linear motion of a camera by LEN pixels, with an
angle of THETA degrees in a counter-clockwise direction. The filter becomes a vector for horizontal and vertical motions. The default LEN is 9, the default
THETA is 0, which corresponds to a horizontal motion of 9 pixels.

H = FSPECIAL('prewitt') returns 3-by-3 filter that emphasizes horizontal edges by approximating a vertical gradient. If you need to emphasize vertical edges,
transpose the filter H: H'.
[1 1 1;0 0 0;-1 -1 -1].

H = FSPECIAL('sobel') returns 3-by-3 filter that emphasizes horizontal edges utilizing the smoothing effect by approximating a vertical gradient. If you need
to emphasize vertical edges, transpose the filter H: H'.
[1 2 1;0 0 0;-1 -2 -1].

H = FSPECIAL('unsharp',ALPHA) returns a 3-by-3 unsharp contrast enhancement filter. FSPECIAL creates the unsharp filter from the negative of the
Laplacian filter with parameter ALPHA. ALPHA controls the shape of the Laplacian and must be in the range 0.0 to 1.0. The default ALPHA is 0.2.

15
IMFILTER Multidimensional image filtering.
B = IMFILTER(A,H) filters the multidimensional array A with the multidimensional filter H. A can be logical or it can be a nonsparse
numeric array of any class and dimension. The result, B, has the same size and class as A.

Each element of the output, B, is computed using double-precision floating point. If A is an integer or logical array, then output elements
that exceed the range of the given type are truncated, and fractional values are rounded.

B = IMFILTER(A,H,OPTION1,OPTION2,...) performs multidimensional filtering according to the specified options. Option arguments can have
the following values:

- Boundary options

X : Input array values outside the bounds of the array are implicitly assumed to have the value X. When no boundary option is specified,
IMFILTER uses X = 0.
'symmetric‘ : Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.
'replicate‘ : Input array values outside the bounds of the array are assumed to equal the nearest array border value.
'circular' : Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic.

- Output size options (Output size options for IMFILTER are analogous to the SHAPE option in the functions CONV2 and FILTER2.)

'same' : The output array is the same size as the input array. This is the default behavior when no output size options are specified.
'full‘ : The output array is the full filtered result, and so is larger than the input array.

- Correlation and convolution

'corr‘ : IMFILTER performs multidimensional filtering using correlation, which is the same way that FILTER2 performs filtering. When no
correlation or
convolution option is specified, IMFILTER uses correlation.
'conv‘ : IMFILTER performs multidimensional filtering using convolution.

16
Thresholding
threshold.m
clear all
% reading image File
image = imread( ‘kaist.jpg' );
[y,x] = size(image);
threshold=128; % defined by user
% threshold
for i=1:x
for j=1:y
if image(j,i) > threshold
image_th(j,i) = 255;
else
image_th(j,i) = 0;
end
end
end
% showing image
figure
subplot(1,2,1), imshow(image)
subplot(1,2,2), imshow(image_th)

17
IM2BW Convert image to binary image by thresholding.
IM2BW produces binary images from indexed, intensity, or RGB images. To do this, it converts
the input image to grayscale format (if it is not already an intensity image), and then converts
this grayscale image to binary by thresholding. The output binary image BW has values of 0
(black) for all pixels in the input image with luminance less than LEVEL and 1 (white) for all
other pixels. (Note that you specify LEVEL in the range [0,1], regardless of the class of the
input image.)

BW = IM2BW(I,LEVEL) converts the intensity image I to black and white.

BW = IM2BW(X,MAP,LEVEL) converts the indexed image X with colormap MAP to black and white.

BW = IM2BW(RGB,LEVEL) converts the RGB image RGB to black and white.

Note that the function GRAYTHRESH can be used to compute LEVEL automatically.

J=im2bw( I, 0.5 );
figure; imshow(J);

18
Threshold value

0.1 0.3 0.7 0.9

19
7000

6000

5000

4000 background
3000

2000
object
1000

0
0 50 100 150 200 250 300

threshold value = 120

20
Feature extraction

* Corner
*
SUSAN corner detector
Harris corner detector
* * ………………………….

Center Point

feature_center.m

21
Edge detection
 Sobel operator
edge_sobel.m

%Sobel operator
BW=edge(I, 'sobel' );
figure; imshow(BW);

22
EDGE Find edges in intensity image.
EDGE takes an intensity or a binary image I as its input, and returns a binary image BW of the same size as I, with 1's where the function finds edges in I and 0's elsewhere.

EDGE supports six different edge-finding methods:

The Sobel method finds edges using the Sobel approximation to the derivative. It returns edges at those points where the gradient of I is maximum.

The Prewitt method finds edges using the Prewitt approximation to the derivative. It returns edges at those points where the gradient of I is maximum.

The Roberts method finds edges using the Roberts approximation to the derivative. It returns edges at those points where the gradient of I is maximum.

The Laplacian of Gaussian method finds edges by looking for zero crossings after filtering I with a Laplacian of Gaussian filter.

The zero-cross method finds edges by looking for zero crossings after filtering I with a filter you specify.

The Canny method finds edges by looking for local maxima of the gradient of I. The gradient is calculated using the derivative of a Gaussian filter. The method uses two
thresholds, to detect strong and weak edges, and includes the weak edges in the output only if they are connected to strong edges. This method is therefore less likely than the
others to be "fooled" by noise, and more likely to detect true weak edges.

The parameters you can supply differ depending on the method you specify. If you do not specify a method, EDGE uses the Sobel method.

Sobel Method
------------
BW = EDGE(I,'sobel') specifies the Sobel method.

BW = EDGE(I,'sobel',THRESH) specifies the sensitivity threshold for the Sobel method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify
THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.

BW = EDGE(I,'sobel',THRESH,DIRECTION) specifies directionality for the Sobel method. DIRECTION is a string specifying whether to look for 'horizontal' or 'vertical' edges, or
'both' (the default).

[BW,thresh] = EDGE(I,'sobel',...) returns the threshold value.

Prewitt Method
--------------
BW = EDGE(I,'prewitt') specifies the Prewitt method.

BW = EDGE(I,'prewitt',THRESH) specifies the sensitivity threshold for the Prewitt method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify
THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.

BW = EDGE(I,'prewitt',THRESH,DIRECTION) specifies directionality for the Prewitt method. DIRECTION is a string specifying whether to look for 'horizontal' or 'vertical' edges, or
'both' (the default).

[BW,thresh] = EDGE(I,'prewitt',...) returns the threshold value.

23
Roberts Method
--------------
BW = EDGE(I,'roberts') specifies the Roberts method.

BW = EDGE(I,'roberts',THRESH) specifies the sensitivity threshold for the Roberts method. EDGE ignores all edges that are not stronger than THRESH. If you do not specify
THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.

[BW,thresh] = EDGE(I,'roberts',...) returns the threshold value.

Laplacian of Gaussian Method


----------------------------
BW = EDGE(I,'log') specifies the Laplacian of Gaussian method.

BW = EDGE(I,'log',THRESH) specifies the sensitivity threshold for the Laplacian of Gaussian method. EDGE ignores all edges that are not stronger than THRESH. If you do
not specify THRESH, or if THRESH is empty ([]), EDGE chooses the value automatically.

BW = EDGE(I,'log',THRESH,SIGMA) specifies the Laplacian of Gaussian method, using SIGMA as the standard deviation of the LoG filter. The default SIGMA is 2; the size of
the filter is N-by-N, where N=CEIL(SIGMA*3)*2+1.

[BW,thresh] = EDGE(I,'log',...) returns the threshold value.

Zero-cross Method
-----------------
BW = EDGE(I,'zerocross',THRESH,H) specifies the zero-cross method, using the specified filter H. If THRESH is empty ([]), EDGE chooses the sensitivity threshold
automatically.

[BW,THRESH] = EDGE(I,'zerocross',...) returns the threshold value.

Canny Method
----------------------------
BW = EDGE(I,'canny') specifies the Canny method.

BW = EDGE(I,'canny',THRESH) specifies sensitivity thresholds for the Canny method. THRESH is a two-element vector in which the first element is the low threshold, and the
second element is the high threshold. If you specify a scalar for THRESH, this value is used for the high threshold and 0.4*THRESH is used for the low threshold. If
you do not specify THRESH, or if THRESH is empty ([]), EDGE chooses low and high values automatically.

BW = EDGE(I,'canny',THRESH,SIGMA) specifies the Canny method, using SIGMA as the standard deviation of the Gaussian filter. The default SIGMA is 1; the size of the filter
is chosen automatically, based on SIGMA.

[BW,thresh] = EDGE(I,'canny',...) returns the threshold values as a two-element vector.

24
 LoG ( Laplacian of Gaussian )
edge_LOG.m
%LoG
BW=edge(I, 'log');
figure; imshow(BW);

25
Hough Transform
Using “hough.m”

original image parameter space result

26

You might also like