Professional Documents
Culture Documents
The image processing Toolbox is a collection of functions that extend the capability of
MATLAB.
• Morphological operations
• Transforms
• Image registration
• Deblurring
Many of the toolbox functions are matlab M-files. A Series of MATLAB statements that
implement specialised image processing algorithms. You can view matlab codes for these
functions using the statement
The Math Works provides several product products that are especially relevant to the
kinds of tasks you can perform with image processing toolbox.
• The online documentations for that product if it is installed or if you are reading
the documentation on CD.
The toolboxes listed below all include function that extends MATLAB.
Product Description
OUTPUT:
OUTPUT:
>> imwrite(i,'cameraman.png'); %writes the image to file & you can specify othe format also.
OUTPUT:
ans =
Filename: 'cameraman.png'
FileSize: 38267
Format: 'png'
FormatVersion: []
Width: 256
Height: 256
BitDepth: 8
ColorType: 'grayscale'
Colormap: []
Histogram: []
InterlaceType: 'none'
Transparency: 'none'
SimpleTransparencyData: []
BackgroundColor: []
RenderingIntent: []
Chromaticities: []
Gamma: []
XResolution: []
YResolution: []
ResolutionUnit: []
XOffset: []
YOffset: []
OffsetUnit: []
SignificantBits: []
Title: []
Author: []
Description: []
Copyright: []
CreationTime: []
Software: []
Disclaimer: []
Warning: []
Source: []
Comment: []
OtherText: []
%To read image, to display image and its histogram in same plot
Solution:
a=imread('cameraman.tif');
subplot(1,2,1);
imshow(a);
subplot(1,2,2);
imhist(a);
OUTPUT:
% To display image in separate figure..
figure(1);
imshow(a);
b=imread('tire.tif');
figure(2);
imshow(b);
c=imread('trees.tif');
figure(3);
imshow(c);
OUTPUT:
subplot(1,3,1);
imshow(a);
b=imread('tire.tif');
subplot(1,3,2);
imshow(b);
c=imread('trees.tif');
subplot(1,3,3);
imshow(c);
OUTPUT:
• Objectives...
→ Image Sampling
→ Image Cropping.
→ Flipping of image..
→Adjusting contrasts....
Solution:
a=imread('cameraman.tif');
subplot(2,2,1);
b=imshow(a);
subplot(2,2,2);
c=a(1:2:a(1),1:2:a(2));
subplot(2,2,3);
imshow(c);
subplot(2,2,4);
imhist(c);
OUTPUT:
% Image Cropping.
imcrop(a);
OUTPUT:
%Cropping the image
% Flipping of image...
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
imhist(a);
c=a(b(1):-1:1,b(2):-1:1);
subplot(2,2,3);
imshow(c);
subplot(2,2,4);
imhist(c);
OUTPUT:
OUTPUT:
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
imhist(a);
subplot(2,2,3);
b=imrotate(a,35);
imshow(b);
subplot(2,2,4);
imhist(b);
OUTPUT:
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
imhist(a);
b=histeq(a);
subplot(2,2,3);
imshow(b);
subplot(2,2,4);
imhist(b);
OUTPUT:
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
imhist(a);
subplot(2,2,3);
imshow(b);
subplot(2,2,4);
imhist(b);
OUTPUT:
%Adjusting contrasts....
i=imread('tire.tif');
c=imadjust(i,[],[0.2 0.5]);
subplot(5,2,1);
imshow(i);
subplot(5,2,2);
imhist(i);
subplot(5,2,3);
imshow(a);
subplot(5,2,4);
imhist(a);
subplot(5,2,5);
imshow(b);
subplot(5,2,6);
imhist(b);
subplot(5,2,7);
imshow(c);
subplot(5,2,8);
imhist(c);
subplot(5,2,9);
imshow(d);
subplot(5,2,10);
imhist(d);
OUTPUT.
500
0
0 50 100 150 200 250
1000
500
0
0 50 100 150 200 250
5000
0
0 50 100 150 200 250
1000
0
0 50 100 150 200 250
500
0
0 50 100 150 200 250
Conclusion: ...................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
.....................................................................................................................................................
Practical-3
Image Enhancement In The Spatial Domain.
1) Program to increase the contrast in a low contrast grayscale image and display the
histogram of the output image. Compare the histogram before and after intensity adjustments.
Solution:-
OUTPUT:
2] Program to display an image with and without gamma correction
X=imread('forest.tif');
figure(1);
subplot(1,3,1);
imshow(X);
% Gamma correction is applied & the value of gamma is taken as 0.5.
b=imadjust(a,[],[],0.5);
subplot(1,3,3);
imshow(b);
title('Image after gamma correction');
figure(2);
subplot(1,3,1);
imshow(a);
title('Image before gamma correction');
subplot(1,3,2);
imshow(b);
title('Image after gamma correction');
K=imabsdiff(a,b);
subplot(1,3,3);
imshow(K,[]);
title('Absolute difference between two images');
Output:
a) Histogram Equalization
Solution:-
I=imread('cameraman.tif');
subplot(3,2,1);
imshow(I);
title('Original image');
J=imhist(I);
subplot(3,2,2);
plot(J);
title('Histogram of the original image');
% Adjusting intensity values using Histogram equalization
K=histeq(I);
subplot(3,2,3);
imshow(K);
title('Histogram equalized image');
L=imhist(K);
subplot(3,2,4);
plot(L);
title('Histogram of the Equalized Image');
% Adjusting intensity values using contrast-Limited Adaptive Histogram Equalization
M=adapthisteq(I);
subplot(3,2,5);
imshow(M);
title('Contrast- Limited Adaptive Histogram Equalized image');
N=imhist(M);
subplot(3,2,6);
plot(N);
title('Histogram of the Contrast-Limited adaptive Equalized Image');
OUTPUT:
• Enhance contrast of an intensity image using histogram equalization. Display the
histogram of the original image and compare it with histogram of the processed image.
i=imread('cameraman.tif');
subplot(2,2,1);
imshow(i);
subplot(2,2,2);
imhist(i);
title('histogram of original');
subplot(2,2,3);
b=histeq(i);
imshow(b);
subplot(2,2,4);
imhist(b);
title('histogram of histequalization.');
Conclusion: ...................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
.....................................................................................................................................................
Practical-4
Filtering in spatial domain
• Create 3x3 mask in m file without using its function and implement on image.
Solution:
i=imread('cameraman.tif');
figure();
subplot(3,2,1);
imshow(i);
title('original image');
%smoothing mask of 8x8
h=fspecial('average',8);
result1=imfilter(i,h,'replicate');
subplot(3,2,2);
imshow(result1);
title('smoothing with mask of 8x8');
h=fspecial('average',16);
result2=imfilter(i,h,'replicate');
subplot(3,2,3);
imshow(result2);
title('smoothing with mask of 16x16');
h=fspecial('average',35);
result3=imfilter(i,h,'replicate');
subplot(3,2,4);
imshow(result3);
title('smoothing with mask of 35x35');
h=fspecial('average',64);
result4=imfilter(i,h,'replicate');
subplot(3,2,5);
imshow(result4);
title('smoothing with mask of 64x64');
%absolute difference between original and using mask of 8,16,35,64
a=imabsdiff(i,result1);
figure();
subplot(1,2,1);
imshow(a);
title('difference between original and the filtered 8');
b=imhist(result1);
subplot(1,2,2);
plot(b);
title('histogram of filter size 8');
a=imabsdiff(i,result2);
figure();
subplot(1,2,1);
imshow(a);
title('difference between original and the filtered 16');
b=imhist(result2);
subplot(1,2,2);
plot(b);
title('histogram of filter size 16');
a=imabsdiff(i,result3);
figure();
subplot(1,2,1);
imshow(a);
title('difference between original and the filtered 35');
b=imhist(result3);
subplot(1,2,2);
plot(b);
title('histogram of filter size 35');
a=imabsdiff(i,result4);
figure();
subplot(1,2,1);
imshow(a);
title('difference between original and the filtered 64');
b=imhist(result4);
subplot(1,2,2);
plot(b);
title('histogram of filter size 64');
• Program to illustrate the grayscale image and to obtain the following types of filtered
images.Motion blurred image, Blurred image, Sharpened image, Sobeled image,
Laplaced image, Gaussianed image, Avereged image.
Solution:
i=imread('cameraman.tif');
subplot(3,3,1);
imshow(i);
title('original image');
%obtaining motion blurred image
h=fspecial('motion',15,45);
motion=imfilter(i,h,'replicate');
subplot(3,3,2);
imshow(motion);
title('image formed due to motion');
%blurred image
h=fspecial('disk',15);
disk=imfilter(i,h,'replicate');
subplot(3,3,3);
imshow(disk);
title('image formed due to disk');
%sharpened image
h=fspecial('unsharp');
sharp=imfilter(i,h,'replicate');
subplot(3,3,4);
imshow(sharp);
title('image formed due to sharpining');
%averaged image
h=fspecial('average',3);
average=imfilter(i,h,'replicate');
subplot(3,3,5);
imshow(average);
title('image formed due to averaging');
%sobeled image
h=fspecial('sobel');
sobel=imfilter(i,h,'replicate');
subplot(3,3,6);
imshow(sobel);
title('image formed due to sobel');
%gaussianed image
h=fspecial('gaussian',[3 3],0.4);
gaussian=imfilter(i,h,'replicate');
subplot(3,3,7);
imshow(gaussian);
title('image formed due to gaussian');
%laplaced image
h=fspecial('laplacian',0.3);
laplacian=imfilter(i,h,'replicate');
subplot(3,3,8);
imshow(laplacian);
title('image formed due to laplaced');
• Program to illustrate median filtering over averaging filter in sitution when image is
corrupted with salt and pepper noise.
Solution:
i=imread('cameraman.tif');
figure();
subplot(1,2,1);
imshow(i);
title('original image');
%adding the salt and pepper noise to original image
j=imnoise(i,'salt & pepper',0.02);
subplot(1,2,2);
imshow(j);
title('salt and pepper noise');
%filtering the image with 3x3 averaging filter
h=fspecial('average',3);
average=imfilter(i,h,'symmetric');
figure();
subplot(1,2,1);
imshow(average);
title('averaged image with filter size of 3x3');
median=medfilt2(i,[3 3],'symmetric');
subplot(1,2,2);
imshow(median);
title('median image with mask of 3x3');
%absolute difference between the original and averaged image
x=imabsdiff(i,average);
figure();
subplot(1,2,1);
imshow(x);
title('difference between original median image');
y=imabsdiff(i,median);
subplot(1,2,2);
imshow(y);
title('difference between original image and median filtered image');
Conclusion: ...................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
..........................................................................................................................................................................
.....................................................................................................................................................
PRACTICAL – 5
for u=1:M
for v=1:N
D(u,v) = sqrt((u-M/2)^2+(v-N/2).^2);
end
end
D=fftshift(D);
switch type
case 'Ideal'
H = double(D<=D0);
case 'Butterworth'
if nargin == 4;
n = 1;
end
H = 1./(1 + (D./D0).^(2*n));
case 'Gaussian'
H = exp(-(D.^2)./(2*(D0^2)));
otherwise
error('Unknown filter type.')
end
1] Program to illustrate the results of following low pass filters with cut of
frequencies set at radii values of 5,15,30,80 resp. Also obtain the corresponding
frequency spectrum and filter displayed as an image.
• Ideal low pass filter
• Gaussian low pass filter
• Butterworth low pass filter
%Ideal Low pass filtered image with cutoff frequecy set at radius value of 5
clc
clear all
ip = imread('peppers.png');
ip=rgb2gray(ip);
[M,N] = size(ip);
ip=double(ip);
f_ip = fft2(ip);
ip=uint8(ip);
lf_ip=fftshift(f_ip);
lf_ip = log(1 + abs(lf_ip));
subplot(1,2,1);
imshow(lf_ip,[])
title('Frequency Spectrum')
subplot(1,2,2);
imshow(ip)
title('Original Image')
figure;
filt = lpfilter('Ideal',M,N,5);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Ideal Lowpass filtered image [cutoff freq=5]')
%Ideal Low pass filtered image with cutoff frequecy set at radius value of 15
figure;
filt = lpfilter('Ideal',M,N,15);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Ideal Lowpass filtered image [cutoff freq=15]')
%Ideal Low pass filtered image with cutoff frequecy set at radius value of 30
figure;
filt = lpfilter('Ideal',M,N,30);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Ideal Lowpass filtered image [cutoff freq=30]')
%Ideal Low pass filtered image with cutoff frequecy set at radius value of 80
figure;
filt = lpfilter('Ideal',M,N,80);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Ideal Lowpass filtered image [cutoff freq=80]')
%Low pass filtered image with cutoff frequecy set at radius value of 230
figure;
filt = lpfilter('Ideal',M,N,230);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Ideal Lowpass filtered image [cutoff freq=230]')
OUTPUT:
FrequencySpectrum Original Image
FilterFrequencydomain Ideal Lowpassfilteredimage[cutofffreq=5]
%Gaussian Low pass filtered image with cutoff frequecy set at radius value of 5
clc
clear all
ip = imread('peppers.png');
ip=rgb2gray(ip);
[M,N] = size(ip);
ip=double(ip);
f_ip = fft2(ip);
ip=uint8(ip);
lf_ip=fftshift(f_ip);
lf_ip = log(1 + abs(lf_ip));
subplot(1,2,1);
imshow(lf_ip,[])
title('Frequency Spectrum')
subplot(1,2,2);
imshow(ip)
title('Original Image')
figure;
filt = lpfilter('Gaussian',M,N,5);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Gaussian Lowpass filtered image [cutoff freq=5]')
%Gaussian Low pass filtered image with cutoff frequecy set at radius value of 15
figure;
filt = lpfilter('Gaussian',M,N,15);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Gaussian Lowpass filtered image [cutoff freq=15]')
%Gaussian Low pass filtered image with cutoff frequecy set at radius value of 30
figure;
filt = lpfilter('Gaussian',M,N,30);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Gaussian Lowpass filtered image [cutoff freq=30]')
%Gaussian Low pass filtered image with cutoff frequecy set at radius value of 80
figure;
filt = lpfilter('Gaussian',M,N,80);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Gaussian Lowpass filtered image [cutoff freq=80]')
%Guassian Low pass filtered image with cutoff frequecy set at radius value of 230
figure;
filt = lpfilter('Gaussian',M,N,230);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Gaussian Lowpass filtered image [cutoff freq=230]')
FrequencySpect Original Img
FilterFreqdomain GaussianLpfilteredimg[cutoff freq=5]
FilterFreqdomain GaussianLpfilteredimg[cutoff freq=15]
FilterFreqdomain GaussianLpfilteredimg[cutoff freq=30]
FilterFreqdomain GaussianLpfilteredimg[cutoff freq=80]
FilterFrequencydomain GaussianLowpassfilteredimage[cutoff freq=230]
%Butterworth Low pass filtered image with cutoff frequecy set at radius value of 5
clc
clear all
ip = imread('onion.png');
ip=rgb2gray(ip);
[M,N] = size(ip);
ip=double(ip);
f_ip = fft2(ip);
ip=uint8(ip);
lf_ip=fftshift(f_ip);
lf_ip = log(1 + abs(lf_ip));
subplot(1,2,1);
imshow(lf_ip,[])
title('Frequency Spectrum')
subplot(1,2,2);
imshow(ip)
title('Original Image')
figure;
filt = lpfilter('Butterworth',M,N,5);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Butterworth Lowpass filtered image [cutoff freq=5]')
%Butterworth Low pass filtered image with cutoff frequecy set at radius value of 15
figure;
filt = lpfilter('Butterworth',M,N,15);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Butterworth Lowpass filtered image [cutoff freq=15]')
%Butterworth Low pass filtered image with cutoff frequecy set at radius value of 30
figure;
filt = lpfilter('Butterworth',M,N,30);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Butterworth Lowpass filtered image [cutoff freq=30]')
%Butterworth Low pass filtered image with cutoff frequecy set at radius value of 80
figure;
filt = lpfilter('Butterworth',M,N,80);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Butterworth Lowpass filtered image [cutoff freq=80]')
%Guassian Low pass filtered image with cutoff frequecy set at radius value of 230
figure;
filt = lpfilter('Butterworth',M,N,230);
f_op = filt.*f_ip;
op = real(ifft2(f_op));
filt=fftshift(filt);
lf_op=log(1+abs(f_op));
subplot(1,2,1);
imshow(filt,[]);
title('Filter Frequency domain')
subplot(1,2,2);
imshow(op,[]);
title('Butterworth Lowpass filtered image [cutoff freq=230]')
FrequencySpectrum Original Image
FilterFrequencydomain ButterworthLowpassfilteredimage[cutofffreq=5]
FilterFrequencydomain ButterworthLowpassfilteredimage[cutoff freq=15]
FilterFrequencydomain ButterworthLowpassfilteredimage[cutofffreq=30]
FilterFrequencydomain ButterworthLowpassfilteredimage[cutoff freq=80]
FilterFrequencydomain ButterworthLowpassfilteredimage[cutoff freq=230]
\Conclusion:
...................................................................................................................................................................
...................................................................................................................................................................
...................................................................................................................................................................
...................................................................................................................................................................
.........................................................................................................................