Introduction To Image Processing Using Matlab
Introduction To Image Processing Using Matlab
About Matlab
MatLab : Matrix Laboratory
Why Matlab?
User Friendly (GUI)
Easy to work with
Powerful tools for complex mathematics
2
Desktop Tools
Command Window
type commands
Workspace
view program variables
clear to clear
double click on a variable to see it in the Array Editor
Command History
view past commands
Command Window
Workspace
Command History
Getting Started
Declare variable in matlab using
>> A=9;
>> B=10;
>> A
9
who: current variables in the workspace
>> who
Your variables are:
A B
whos
Name Size Bytes Class
A 1x1 8 double array
B 1x1 8 double array
Matrices
a vector >> x = [1 2 5 1]
x =
1 2 5 1
x =
1 2 3
5 1 4
3 2 -1
transpose >> y = x
y =
1 5 3
2 1 2
3 4 -1
Colon Operator
>> y=x(2,3)
y = 1 2 3
x= 5 1 4
4 Comment Statement 3 2 -1
Multiplication:
>> E = A * B (Matrix multiplication)
>> E = A .* B (Element wise multiplication)
Division:
>> F = A . / B (Element wise division)
8
Generating basic matrices
Matrix with ZEROS
>> z = zeros(r, c);
z=zeros(3,3)
Matrix with ONES
>> o = ones (r, c);
o=ones(3,3)
IDENTITY Matrix
>> I = EYE (r, c);
i=eye(3,3)
Random Numbers
>> R=rand(r, c);
r Rows
c Columns
zeros, ones, eye Matlab functions
9
Operators
>> x = [zeros(1,3) ones(1,2)]
[] concatenation x =
0 0 0 1 1
>> x = [1 3 5 7 9]
x =
1 3 5 7 9
>> y = x(2)
() subscription
y =
3
>> y = x(2:4)
y =
3 5 7
Load and Save Variables
>> A = [1 2 3] % declare A
>> B = [4 5 6] % declare B
>> who
Your variables are:
A B
Flow Control
if A > B
if statement
'greater'
switch statement
elseif A < B
'less'
else
'equal'
end
Functions
which can accept input arguments and return output arguments.
Internal variables are local to the function.
Example - Script
Example - Function
Run .m file
From editor
we can run a script from editor using button
1,1 1,2
columns
2,1
1 2 3
1 34 32 33 ..
2 17 22 25 ..
rows
3 18 21 24 ..
.
.
Some basic concepts
Reading an image
>> I = imread('pout.tif');
Display an image
>> imshow(I);
>> imtool(I);
>> imagesc(I);
>> image(I);
16 bit image
Values range from 0 to 65535 (Class uint16)
Double image
Values range from 0 to 1 (Class double)
Image Formats in MATLAB
MATLAB can import/export several image formats
RGB images
Indexed Images
Binary Images
Intensity Images
An intensity image is a data matrix, whose values
represent intensities within some range.
Matrix with image data image filename as a string image format as a string
m-by-n-by-3 data array that defines red, green, and blue color
components for each individual pixel
Read rgb image
>> rgb=imread(pills.bmp);
mapping
Displaying indexed images
>> I=imread(mandrill.bmp);
>> I2=I(:,:,2); % green values of I
>> image(I2)
>> colorbar % display colourmap
Index
Associated color
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
Converting Images
After image has been read it has to be converted from
one form to another
Functions Usage
gray2ind intensity image to index image
im2bw image to binary
im2double image to double precision
im2uint8 image to 8-bit unsigned integers
im2uint16 image to 16-bit unsigned integers
ind2gray indexed image to intensity image
mat2gray matrix to intensity image
rgb2gray RGB image to grayscale
rgb2ind RGB image to indexed image
Example Image Conversion
>> im=imread(capsule.jpg);
>> im1=rgb2gray(im);
50
100
150
200
250
300
350
400
450
500
Complement
Image Enhancement using Histogram
Processing Techniques
44
4
x 10
4
3.5
2.5
1.5
0.5
7000
6000
5000
4000
3000
2000
1000
Over-exposed image
Histogram of an Image
Histogram of a digital image with gray levels in the range
[0 L-1], is a discrete function
h(rk) = nk
Where
rk : the kth gray level
nk : the number of pixels in the image having gray level rk
h(rk) : histogram of a digital image with gray levels rk
Histogram Processing Techniques (cont.)
48
Before
After Equalization
Histogram Processing Techniques (cont.)
49
After Equalization
Before
Histogram Processing Techniques (cont.)
50
After Equalization
Before
Histogram Processing Techniques (cont.)
51
n
j 0
j
k nj
s
j 0 n
Histogram Processing Techniques (cont.)
53
%% Histogram plot
imhist(a)
%% Histogram equalization
b=histeq(a);
Low contrast image Histogram plot
Statistic parameters
Mean, Median, Mode,
Min, Max, Etc.
Moving
window
Output image
Order Statistics Filters
Median filter
f ( x, y ) median g ( s, t )
( s ,t )S xy
Max filter
Min filter
Midpoint filter
f ( x, y ) 1 max g ( s, t ) min g ( s, t )
2 ( s ,t )S xy ( s ,t )S xy
Median Filter: How it works?
A median filter is good for removing impulse, isolated noise
Sorted
Degraded image Moving array
window
Salt noise Filter output
Pepper noise
Normally, impulse noise has high magnitude
and is isolated. When we sort pixels in the
moving window, noise pixels are usually
at the ends of the array.
Therefore, its rare that the noise pixel will be a median value.
Remove salt and pepper noise from images
59
Gaussian blur
motion blur
63
From [Gonzalez & Woods]
Problem statement
g x, y f x, y hx, y x, y
Goal: To design a restoration filter to minimize error = F f f
The blurring function is called Point spread function
Variations:
h unknown
Constraints on f
Model for f
64
Image Restoration: Deblurring/Deconvolution
Non-blind deblurring/deconvolution
Given: observation y(m,n) and blurring function h(m,n)
Design: g(m,n), such that the distortion between x(m,n) and ^
x(m,n)
is minimized
Blind deblurring/deconvolution
Given: observation y(m,n)
^
Design: g(m,n), such that the distortion between x(m,n) and x(m,n)
is minimized 65
Image blurring with MATLAB
66
I = im2double(imread('cameraman.tif'));
imshow(I); title('Original Image (courtesy of MIT)');
% Add motion blur to the image
LEN = 21; THETA = 11;
PSF = fspecial('motion', LEN, THETA);
blurred = imfilter(I, PSF, 'conv', 'circular');
figure, imshow(blurred)
% simulate additive noise
noise_mean = 0;
noise_var = 0.0001;
blurred_noisy = imnoise(blurred, 'gaussian', ...
noise_mean, noise_var);
figure, imshow(blurred_noisy);title('Simulate Blur and Noise')
%% deblurring with Wiener filter
estimated_nsr = noise_var / var(I(:));
wnr3 = deconvwnr(blurred_noisy, PSF, estimated_nsr);
figure, imshow(wnr3)
title('Restoration of Blurred, Noisy Image Using Estimated
NSR');
Visualizing the Fourier Spectrum using
MATLAB
70
% Create an image
a=zeros(200);
for i=1:size(a,1)
for j=1:size(a,2)
if (i>=41 && i<=160)&&(j>=81 && j<=120)
a(i,j)=1;
end
end
end
figure; imshow(a);title('original image')
% Calculate the DFT
F=fft2(a);
%There are real and imaginary parts to F. Use the abs
function to compute the magnitude of the components.
S=abs(F);
figure;imshow(S,[])title('Fourier spectrum')
Visualizing the Fourier Spectrum using
MATLAB
71
% Read Image
img=imread(cameraman.tif');
subplot(121);
imshow(img);
title('original image)
% Fourier Tranform
fimg=fftshift(fft2(img));
subplot(122);
imshow(abs(fimg)/10000)
title('transformed image')
Image Transformation with MATLAB
74
3. Salt and pepper noise filtering using weighted median and centre
weighted median filter. Find the weight vector using real coded GA to
maximize the quality of the image in terms of PSNR.
Image Processing Projects
4. Compare the quality of the image using PSNR and SSIM for compression of
a bitmap image to jpg and jpg2000.