DIP Labmanual
-
Upload
guptaarpit108 -
Category
Documents
-
view
238 -
download
0
Transcript of DIP Labmanual
-
8/12/2019 DIP Labmanual
1/21
Digital Image Processing
Lab Manual
Submitted By :-
Prateek Bansal
2K10/EC/100
-
8/12/2019 DIP Labmanual
2/21
Experiment -1
Objective :To perform image enhancement operations.
1. Contrast Stretching2. Image Negative
Platform Used: MatlabR2011b
Code:
I=imread('lena.tiff');[m n]=size(I);I=rgb2gray(I);I=imresize(I,[256 256]);figure(1);imshow(I);Negative=255-I; %Image Negativefigure(2);imshow(Negative);
contrast=1./(1+(256./double(I)).^5); %Contrast Stretchingfigure(3);imshow(contrast);
-
8/12/2019 DIP Labmanual
3/21
OUTPUT:
Original Image
Image Negative
Contrast Stretching
-
8/12/2019 DIP Labmanual
4/21
Experiment 2
Objective: To flip an image horizontally and vertically.
Platform Used:MatlabR2011b
Theory:
Flip: In flipdim(A,dim) When the value of dim is 1, the array is flipped row-wise down that results in
vertical flip. When dim is 2, the array is flipped columnwise left to right. flipdim(A,1)is the same
as flipud(A), and flipdim(A,2) is the same as fliplr(A).
Code:
% flip operation
I = imread('onion.png');I2 = flipdim(I ,2); %# horizontal flip
I3 = flipdim(I ,1); %# vertical flipI4 = flipdim(I3,2); %# horizontal+vertical flipsubplot(2,2,1), imshow(I);subplot(2,2,2), imshow(I2);subplot(2,2,3), imshow(I3);subplot(2,2,4), imshow(I4);
-
8/12/2019 DIP Labmanual
5/21
Output
-
8/12/2019 DIP Labmanual
6/21
Experiment 3
Objective: To perform average filtering of a noisy image.
Platform Used:Matlab
Theory:
In average filter a mask of NXN size is used to move over an image. The corresponding pixel of image tothe central pixel position of mask is replaced by an average of all NXN pixels overlapping with mask.This method is used to remove the noise (generally Gaussian type) .The value of N can be 3,5,.
Code:
%implementation of arthimetic mean filterclc;close all;a=im2double(rgb2gray(imread('lena.tiff')));subplot(2,2,1)imshow(a);title('original image')a=imnoise(a,'gaussian',0,0.005);subplot(2,2,2)imshow(a)title('degraded imgae')b=(1/9)*ones(3);[m n]=size(a);fori=1:m-2forj=1:n-2
a(i,j)=sum(sum((a(i:i+2,j:j+2).*b)));endendsubplot(2,2,3)
imshow(a)title('restored image')
-
8/12/2019 DIP Labmanual
7/21
-
8/12/2019 DIP Labmanual
8/21
Experiment 4
Objective: To perform histogram equalization on an image.
Platform Used:MatlabR2011b
Theory:
Histogram equalization is used to enhance the contrast of the image. It uniformly distributes the values of
intensities over the pixels. Dark region becomes darker and bright region becomes brighter, that
improves the appearance of the image.
Code:
%program for histogram equalizationclc;close all;a=rgb2gray(imread('Penguins.jpg'));% read color image and convert into gray
image[m n]=size(a); %size of imageL=255; % maximum possible gray levelsubplot(2,1,1)imshow(a) %display original imagetitle('original image')fori=0:L-1p(i+1)=length(find(a==i))/(m*n); % find the probabilities of each gray levelendfori=1:mforj=1:n
c=a(i,j);b(i,j)=sum(p(1:c+1)); % sum up the probabilities of pixels (equalization)endendsubplot(2,1,2)imshow(b)title('histogram equalized image')
-
8/12/2019 DIP Labmanual
9/21
OUTPUT:
-
8/12/2019 DIP Labmanual
10/21
Experiment 5
Objective: To implement sobel, Canny edge detection on an image.
Platform Used:MatlabR2011b
Code:
A=imread('lena.tiff');subplot(2,2,1),imshow(A);
B=rgb2gray(A);B=imresize(B,[256 256]);C=double(B);
fori=1:size(C,1)-2forj=1:size(C,2)-2%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image%B(i,j)=abs(Gx)+abs(Gy);B(i,j)=sqrt(Gx.^2+Gy.^2);
endend
subplot(2,2,2),imshow(B); title('Sobel gradient');
%Define a threshold valueThresh=100;B=max(B,Thresh);B(B==round(Thresh))=0;
B=uint8(B);
subplot(2,2,3),imshow(B);title('Edge detected Image');
% Canny Edge detectionI = imread('lena.tiff');
I=rgb2gray(I);I=imresize(I,[256 256]);th=input('Enter the threshold limit=');% th is upper threshold and lower threshold is .4*upper threshold% The value of threshold should be less than 1I2 = edge(I,'canny',th);figure(2);imshow(I2);title('Canny edge detected output ');
-
8/12/2019 DIP Labmanual
11/21
Output:
Enter the threshold limit=0.25
-
8/12/2019 DIP Labmanual
12/21
Experiment 6
Objective: To perform deblurring of an image using Weiner filter.
Platform Used:MatlabR2011b
Theory:Weiner2 performs 2-D adaptive noise filtering.
wiener2lowpass-filters a grayscale image that has been degraded by constant power additive noise.wiener2 uses a pixelwise adaptive Wiener method based on statistics estimated from a local neighborhood
of each pixel ie. The wiener2 function applies a Wiener filter (a type of linear filter) to an imageadaptively. Where the variance is large, wiener2 performs little smoothing. Where the variance is small,
wiener2 performs more smoothing.
This approach often produces better results than linear filtering. The adaptive filter is more selective thana comparable linear filter, preserving edges and other high-frequency parts of an image..wiener2,however, does require more computation time than linear filtering.
wiener2 works best when the noise is constant-power ("white") additive noise, such as Gaussian noise.
wiener2 estimates the local mean and variance around each pixel,
where is the N-by-Mlocal neighborhood of each pixel in the image A. wiener2 then creates a pixelwise
Wiener filter using these estimates,
where 2is the noise variance. If the noise variance is not given, wiener2 uses the average of all the local
estimated variances.
Wiener filter can be applied on an image to deblur(image restoration) it. deconvwnr(I,PSF,NSR)
function is used to deblur the image I, PSF is a point Spread Function with which I was convolved, NSRis a noise to signal ratio.
Code:
RGB = imread('saturn.png');I = rgb2gray(RGB);J = imnoise(I,'gaussian',0,0.025);subplot(2,1,1),imshow(J);K = wiener2(J,[5 5]);subplot(2,1,2), imshow(K),title('filtered image');
-
8/12/2019 DIP Labmanual
13/21
Output:
-
8/12/2019 DIP Labmanual
14/21
Experiment 7
Objective: To apply Median filter to an image.
Platform Used:Matlab
Theory:Median filtering is a nonlinear process useful in reducing impulsive, or salt-and-pepper noise. It
is also useful in preserving edges in an image while reducing random noise. Impulsive or salt-and pepper
noise can occur due to a random bit error in a communication channel. In a median filter, a window slides
along the image, and the median intensity value of the pixels within the window becomes the output
intensity of the pixel being processed
Like lowpass filtering, median filtering smoothes the image and is thus useful in reducing noise. Unlike
lowpass filtering, median filtering can preserve discontinuities in a step function and can smooth a few
pixels whose values differ significantly from their surroundings without affecting the other pixels
If the two impulsive values are due to noise, the result of using a median filter will be the reduce the
noise. If the two values are part of the signal, however, using the median filter will distort the image.
Code:
I=imread('lena.tiff');I=rgb2gray(I);I=imresize(I,[256 256]);subplot(2,3,1);imshow(I);title('Original Image');
I=imnoise(I,'salt & pepper');subplot(2,3,2);imshow(I);
title('Degraded Image');
%Median filtering for 3X3 windowI_3=medfilt2(I,[3 3]);subplot(2,3,3);imshow(I_3);title('Filtered Image (3X3)');
%Median filtering for 5X5 windowI_3=medfilt2(I,[5 5]);subplot(2,3,4);imshow(I_3);title('Filtered Image (5X5)');
%Median filtering for 7X7 windowI_3=medfilt2(I,[7 7]);subplot(2,3,5);imshow(I_3);title('Filtered Image (7X7)');
-
8/12/2019 DIP Labmanual
15/21
Output
-
8/12/2019 DIP Labmanual
16/21
Experiment 8
Objective: To implement Discrete Cosine Transform(DCT) of an image.
Platform Used:MatlabR2011b
Code
%Discrete Cosine Transform (DCT)
clc;
close all
clear all;
a=imread('lena1.jpg');
a=im2double(rgb2gray(a));
a=imresize(a,[256,256]);
imshow(a);
N=256;
for k=0:N-1
for n=0:N-1
if k==0
C(k+1,n+1)=sqrt(1/N);
else
C(k+1,n+1)=sqrt(2/N)*cos((pi*(2*n+1)*k)/(2*N));
end
end
end
b=C*a*C';
figure
imshow(b);
%Reconstruction of Image
d=C'*b*C
figure
imshow(d)
-
8/12/2019 DIP Labmanual
17/21
Output
Original Image Transformed Image
Reconstructed Image
-
8/12/2019 DIP Labmanual
18/21
Experiment 9
Objective: To implement Hadamard Transform(DCT) of an image.
Platform Used:MatlabR2011b
Code%Kronecker Product Function
function [B]=kronecker_product(A)
B=[A A; A -A];
end
%Hadammard Transform
clc;
close all;
clear all;
a=imread('lena.jpg');
a=imresize(a,[128,128]);
a=im2double(a);
figure
imshow(a);
%n=size(a);
h=[1/sqrt(2) 1/sqrt(2);1/sqrt(2) -1/sqrt(2)];
for i=1:log2(128)-1
h=1/sqrt(2)*kronecker_product(h);
end
h
b=h*a*h';
figure
imshow(b);
%Reconstruction
c=h'*b*h;
figure
imshow(c);
-
8/12/2019 DIP Labmanual
19/21
OUTPUT:
Original Image Transformed Image Reconstructed Image
-
8/12/2019 DIP Labmanual
20/21
Experiment 10
Objective: To perform dilation and erosion of an image.
Platform Used:MatlabR2011b
Theory
Erosion and Dilation:Morphological operations apply a structuring element to an input image, creating anoutput image of the same size. In dilation the value of the output pixel is the maximumvalue of all the
pixels in the input pixel's neighborhood. In a binary image, if any of the pixels is set to the value 1, theoutput pixel is set to 1.In Erosion the value of output pixel is minimum value of all the pixels in the input
pixels neighborhood. In binary image, if any of thepixel is set to 0, the output pixel is set to 0.
Code
I = imread('lena.tiff');I=rgb2gray(I);figure(1);subplot(2,2,1);
imshow(I);title('Original Image');
%erode operation%If IM is logical and the structuring element is flat, imerode performs%binary erosion; otherwise it performs grayscale erosion.%If SE is an array of structuring element objects, imerode performs multipleerosions%of the input image, using each structuring element in SE in successionSE=strel('line',10,45);
IM2 = imerode(I,SE);
subplot(2,2,3);imshow(IM2);title('Eroded Image');
%dialation operationBW2 = imdilate(I,SE);subplot(2,2,4);imshow(BW2);title('Dilated Image');
-
8/12/2019 DIP Labmanual
21/21
Output