0% found this document useful (0 votes)
74 views

II MSC Matlab Record Final

The document provides an index of 10 image processing experiments to be performed in MATLAB. Experiment 7 involves writing a program to compress an image using Huffman coding method in MATLAB. The aim is to compress an image file by encoding it into variable length codes using Huffman coding algorithm and write the compressed file. The procedure involves starting the program, writing the code in MATLAB, executing it to get the output, and stopping the process.

Uploaded by

Boobesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

II MSC Matlab Record Final

The document provides an index of 10 image processing experiments to be performed in MATLAB. Experiment 7 involves writing a program to compress an image using Huffman coding method in MATLAB. The aim is to compress an image file by encoding it into variable length codes using Huffman coding algorithm and write the compressed file. The procedure involves starting the program, writing the code in MATLAB, executing it to get the output, and stopping the process.

Uploaded by

Boobesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

INDEX

S.NO DATE PARTICULARS PAGE


NO
1. Choose two grayscale images or RGB images that you will first
have to grayscale (withrgb2gray() function) . Display original
images and the same images after their QUANTIZATION with
different number of bits (1 to 8) using MATLAB

2. Perform Histogram Equalization on a Color image using


MATLAB.

3. Using Spatial Domain technique, write a program in MATLAB to


perform Smoothening operation in an image

4. Write a MATLAB code to transform 1-D FIR Filter to 2-D FIR


Filter using FrequencyTransformation Method. (FIR-Finite
Impulse Response).

5. Write a MATLAB code to transform 1-D FIR Filter to 2-D FIR


Filter using FrequencyTransformation Method. (FIR-Finite
Impulse Response).

6. Write a MATLAB program to detect the edges within the


image and compare the results of both Canny and Prewitt
Methods

7. Write a program to Compress an image using Huffman coding


method in MATLAB.

8. Implement Discrete Cosine Transformation method to compress


an image using MATLAB.

9. Write a MATLAB code for Image Segmentation to convert to a


binary image to improve the legibility of text Using thresholding
technique.

10. Compute the Watershed Transform of the Segmentation


function in an image at foreground and background marker
pixels using Marker-Controlled Watershed Segmentation in
MATLAB.
EX NO : 1 1. Choose two grayscale images or RGB images that you
will first have to grayscale ( withrgb2gray() function) .
Display original images and the same images after their
DATE: QUANTIZATION with different number of bits (1 to 8) using
MATLAB

AIM:

To Choose two grayscale images or RGB images that you will first have to grayscale (withrgb2
gray() function) . Display original images and the same images after their QUANTIZATION with different
number of bits (1 to 8) using MATLAB.

PROCEDURE:
STEPS:

1. Start the program.


2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

RGB = imread('peppers.png');
imshow(RGB)
I = rgb2gray(RGB);
figure
imshow(I)
threshRGB = multithresh(I,7);
threshForPlanes = zeros(3,7);
for i = 1:3
threshForPlanes(i,:) = multithresh(I(:,:,i),7);
end
value = [0 threshRGB(2:end) 255];
quantRGB = imquantize(I, threshRGB, value);
quantPlane = zeros( size(I) );
for i = 1:3value = [0 threshForPlanes(i,2:end) 255];
quantPlane(:,:,i) = imquantize(I(:,:,i),threshForPlanes(i,:),value);
end
quantPlane = uint8(quantPlane);
imshowpair(quantRGB,quantPlane,'montage')
axis off title('Full RGB Image Quantization Plane-by-Plane Quantization')
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 2 2. Perform Histogram Equalization on a Color image using
MATLAB

DATE:

AIM:

To Perform Histogram Equalization on a Color image using MATLAB.

PROCEDURE:

STEPS:

1. Start the program.


2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

%COLOR HISTOGRAM EQUALIZATION


%READ THE INPUT IMAGE
I = imread('football.jpg');
%CONVERT THE RGB IMAGE INTO HSV IMAGE FORMAT
HSV = rgb2hsv(I);
%PERFORM HISTOGRAM EQUALIZATION ON INTENSITY COMPONENT
Heq = histeq(HSV(:,:,3));
HSV_mod = HSV;
HSV_mod(:,:,3) = Heq;
RGB = hsv2rgb(HSV_mod);
Figure,subplot(1,2,1),imshow(I);title('BeforeHistogramEqualization') subplot(1,2,2),imshow(
RGB);title('After Histogram Equalization');
%DISPLAY THE HISTOGRAM OF THE ORIGINAL AND THE EQUALIZED IMAGE
HIST_IN = zeros([256 3]);
HIST_OUT = zeros([256 3]);
%HISTOGRAM OF THE RED,GREEN AND BLUE COMPONENTS
HIST_IN(:,1) = imhist(I(:,:,1),256); %RED
HIST_IN(:,2) = imhist(I(:,:,2),256); %GREEN
HIST_IN(:,3) = imhist(I(:,:,3),256); %BLUE
HIST_OUT(:,1) = imhist(RGB(:,:,1),256); %RED
HIST_OUT(:,2) = imhist(RGB(:,:,2),256); %GREEN
HIST_OUT(:,3) = imhist(RGB(:,:,3),256); %BLUE
mymap=[1 0 0; 0.2 1 0; 0 0.2 1];
figure,subplot(1,2,1),bar(HIST_IN);colormap(mymap);legend('RED CHANNEL','GREEN
CHANNEL','BLUE CHANNEL');title('Before Applying Histogram Equalization');
subplot(1,2,2),bar(HIST_OUT);colormap(mymap);legend('REDCHANNEL','GREEN
CHANNEL','BLUE CHANNEL');title('After Applying Histogram Equalization'
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO : 3 3.Spatial Domain Technique Using Smoothening Operation In

DATE: An Image.

AIM :
To Using Spatial Domain technique, write a program in MATLAB to perform
Smoothening operation in an image.

PROCEDURE:
STEPS:
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

>> A = imread(’example.tif’
>> doc imread
>> whos A
>> imageinfo(’example.tif’)
>> imageview(’example.tif’)
>> figure, imagesc(A)
>> axis equal tight
>> figure, imshow(A,[])
>> A(222,320,:)
>> B = A(200:400,100:500,:); >> figure, imagesc(B)
>> close(fig_number)
>> close all
>> imwrite(B,’SavedImage.tif’)
>> doc imwrite
>> run_spatial_resolution
>> run_intensity_resolution
%1-D filtering
>> help conv >> c = conv(a,b)
>> run_convolution
>> help filter
Y = filter(B,A,X)
>> run_filter1D
%2-D filtering
h_size = 11; h_sigma = 5;
h = fspecial (’gaussian ’, h_size , h_sigma )
; figure , mesh ( h ) ; f = imread (’lena .jpg
’) ;
% if not grayscale , use rgb2gray () g = imfilter ( double ( f ) , h , ’replicate ’) ;
figure (’Name ’,’Original image ’) , imshow (f ,[]) figure (’Name ’,’Filtered image ’) , imshow (g
,[])
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO : 4 4. 1-D FIR Filter to 2-D FIR Filter using FrequencyTransformation
Method

DATE:

AIM:
To Write a MATLAB code to transform 1-D FIR Filter to 2-D FIR Filter using
FrequencyTransformation Method. (FIR-Finite Impulse Response).

PROCEDURE:
STEPS:
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

% Reading input image : input_image


input_image = imread('[name of input image file].[file format]');
% Saving the size of the input_image in pixels-
% M : no of rows (height of the image)
% N : no of columns (width of the
image) [M, N] = size(input_image);
% Getting Fourier Transform of the input_image
% using MATLAB library function fft2 (2D fast fourier transform)
FT_img = fft2(double(input_image));
% Assign Cut-off Frequency
D0 = 30; % one can change this value accordingly
% Designing filter
u = 0:(M-1);
idx = find(u>M/2);
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;
% MATLAB library function meshgrid(v, u) returns
% 2D grid which contains the coordinates of vectors
% v and u. Matrix V with each row is a copy
% of v, and matrix U with each column is a copy of u
[V, U] = meshgrid(v, u);
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);
% Comparing with the cut-off frequency and
% determining the filtering
mask H = double(D <= D0);
% Convolution between the Fourier Transformed
% image and the
mask G =
H.*FT_img;
% Getting the resultant image by Inverse Fourier Transform
% of the convoluted image using MATLAB library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));
% Displaying Input Image and Output Image
subplot(2, 1, 1), imshow(input_image),
subplot(2, 1, 2), imshow(output_image, [ ]);
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 5 5 . SOBEL OPERATOR METHOD USING IMAGE

DATE:

AIM:
To Find the Boundaries of Objects within an image by Sobel operator method in
MATLAB

PROCEDURE:
STEPS:
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

I = imread('coins.png');
imshow(I)
%Apply the Sobel edge detector to the unfiltered input image. Then, apply the Canny edge
detector to the unfiltered input image
BW1 = edge(I,'sobel');
BW2 =
edge(I,'canny');
%Display the filtered images side-by-side for comparison.
tiledlayout(1,2)
nexttile
imshow(BW1)
title('Sobel Filter')
nexttile
imshow(BW2)
title('Canny Filter')
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 6 6. COMPARE THE IMAGES USING CANNY AND PREWITT

DATE: METHODS

AIM :

To Write a MATLAB program to detect the edges within the image and compare the results of both
Canny and Prewitt Methods.

PROCEDURE :
STEPS :
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

%Read a grayscale image into the workspace and display it.


I = imread('circuit.tif');
imshow(I)
%Read a grayscale image into the workspace and display it.
BW1 = edge(I,'Canny');
%Read a grayscale image into the workspace and display it.
%Display both results side-by-side.
BW1 = edge(I,'Canny');
imshowpair(BW1,BW2,'montage')
OUTPUT:
RESULT:
Thus the above program has been executed successfully and verified
EX NO: 7 7. COMPRESS AN IMAGE USING HUFFMAN CODING
METHOD

DATE:

AIM:
To Write a program to Compress an image using Huffman coding method in MATLAB

PROCEDURE:
STEPS
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

clear all
clc
A=imread('cameraman.tif');
A1=double(A(:));
[p,symbols]=hist(A1,unique(A1));
p=p/sum(p);
[dict,avglen] = huffmandict(symbols,p);
comp = huffmanenco(A1,dict);
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 8 8. COMPRESS AN IMAGE USING COSINE
TRANSFORMATION METHOD

DATE:

AIM:
To Implement Discrete Cosine Transformation method to compress an image using
MATLAB.

PROCEDURE:
STEPS:
1. Start the program.
2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

%Read an image into the workspace and convert it to class


double. I = imread('cameraman.tif');
I = im2double(I);
%Compute the two-dimensional DCT of 8-by-8 blocks in the image. The function dctmtx returns
the N-by-N DCT transform matrix.
T = dctmtx(8);
dct = @(block_struct) T * block_struct.data *
T'; B = blockproc(I,[8 8],dct);
%Discard all but 10 of the 64 DCT coefficients in each block.
mask = [1 1 1 1 0 0 0 0
11100000
11000000
10000000
00000000
00000000
00000000
0 0 0 0 0 0 0 0];
B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
%Reconstruct the image using the two-dimensional inverse DCT of each block.
invdct = @(block_struct) T' * block_struct.data * T;
I2 = blockproc(B2,[8 8],invdct);
%Display the original image and the reconstructed image, side-by-side. Although there is some
loss of quality in the reconstructed image, it is clearly recognizable, even though almost 85%
of the DCT coefficients were discarded.
imshow(I)figure
imshow(I2)
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 9 9. LEGIBILITY OF TEXT USING THRESHOLDING

DATE: TECHNIQUE

AIM:
To Write a MATLAB code for Image Segmentation to convert to a binary image to
improve the legibility of text Using thresholding technique.

PROCEDURE:

STEPS:

1. Start the program.


2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

%Read a grayscale image into the workspace.


I = imread('coins.png');
Calculate a threshold using graythresh. The threshold is normalized to the range [0, 1].
level = graythresh(I)
level = 0.4941
%Convert the image into a binary image using the threshold.
BW = imbinarize(I,level);
%Display the original image next to the binary image.
imshowpair(I,BW,'montage')
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.
EX NO: 10 10. IMAGE USING MARKER-CONTROLLED WATERSHED
SEGMENTATION

DATE:

AIM:
To Compute the Watershed Transform of the Segmentation function in an image at
foreground and background marker pixels using Marker-Controlled Watershed Segmentation in
MATLAB.

PROCEDURE:

STEPS:

1. Start the program.


2. Write the code in MATLAB R2014a.
3. To execute the image as output.
4. Stop the process.
CODING:

rgb = imread('pears.png');
I = rgb2gray(rgb);
imshow(I)
text(732,501,'Image courtesy of Corel(R)',...
'FontSize',7,'HorizontalAlignment','right')
rgb = imread('pears.png');
I = rgb2gray(rgb);
imshow(I)
text(732,501,'Image courtesy of Corel(R)',...
'FontSize',7,'HorizontalAlignment','right')
L = watershed(gmag);
Lrgb = label2rgb(L);
imshow(Lrgb)
title('Watershed Transform of Gradient Magnitude')
se = strel('disk',20);
Io = imopen(I,se);
imshow(Io)
title('Opening')
Ie = imerode(I,se);
Iobr =
imreconstruct(Ie,I);
imshow(Iobr)
title('Opening-by-Reconstruction')
Ioc = imclose(Io,se);
imshow(Ioc)
title('Opening-Closing')
Iobrd =
imdilate(Iobr,se);
Iobrcbr = imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
imshow(Iobrcbr)
title('Opening-Closing by Reconstruction')
fgm = imregionalmax(Iobrcbr);
imshow(fgm)
title('Regional Maxima of Opening-Closing by Reconstruction')
I2 = labeloverlay(I,fgm);
imshow(I2)
title('Regional Maxima Superimposed on Original Image')
se2 = strel(ones(5,5));
fgm2 = imclose(fgm,se2);
fgm3 = imerode(fgm2,se2);
fgm4 = bwareaopen(fgm3,20);
I3 = labeloverlay(I,fgm4);
imshow(I3)
title('Modified Regional Maxima Superimposed on Original Image')
bw = imbinarize(Iobrcbr);
imshow(bw)
title('Thresholded Opening-Closing by Reconstruction')
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
imshow(bgm)
title('Watershed Ridge Lines')
gmag2 = imimposemin(gmag, bgm |
fgm4); L = watershed(gmag2);
labels = imdilate(L==0,ones(3,3)) + 2*bgm + 3*fgm4;
I4 = labeloverlay(I,labels);
imshow(I4)
title('Markers and Object Boundaries Superimposed on Original Image')
Lrgb = label2rgb(L,'jet','w','shuffle');
imshow(Lrgb)
title('Colored Watershed Label
Matrix') figure
imshow(I)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.3;
title('Colored Labels Superimposed Transparently on Original Image')
OUTPUT:
RESULT:
Thus the above program was executed successfully and output was verified.

You might also like