Assign 3
Assign 3
Submitted by
Kanish Mahato
21103070
CSE
1/11
1. Read a gray scale image and compress it using Huffman coding.
Reconstruct the image from the compressed code and compute the
error. What do you observe?
Code :-
imgData = imread('second.tiff');
if size(imgData, 3) == 3
imgData = rgb2gray(imgData);
end
uniqueSymbols = unique(imgData(:));
probabilities = histcounts(imgData(:), [uniqueSymbols;
max(uniqueSymbols)+1]) / numel(imgData);
huffmanDict = create_huffman_dict(uniqueSymbols,
probabilities);
imgVector = imgData(:);
figure;
subplot(1, 2, 1);
imshow(imgData); title('Original Image');
subplot(1, 2, 2);
imshow(reconstructedImg); title('Reconstructed Image');
2/11
while numel(nodes) > 1
[prob, idx] = sort(prob);
nodes = nodes(idx);
nodes{2} = {nodes{1}, nodes{2}};
nodes(1) = [];
prob(2) = prob(1) + prob(2);
prob(1) = [];
end
huffmanDict = build_dict(nodes{1}, [], symbols);
end
3/11
for j = 1:length(huffmanDict)
if isequal(temp, huffmanDict{j, 2})
decoded = [decoded huffmanDict{j, 1}];
temp = [];
break;
end
end
end
end
Output :-
4/11
Observation:-
decompressed_img = imread('compressed_image.tif');
Output:-
5/11
Observation:-
3. Read a gray scale image and detect edges using the following:
(a) Sobel Operator
(b) Roberts Operator
(c) Canny Edge Detection
Code :-
img = imread('image.png');
img = rgb2gray(img);
6/11
roberts_edges = edge(img, 'roberts');
figure, imshow(roberts_edges);
Output :-
7/11
4. Read an image and apply erosion and dilation operations on the
same.
Code :-
img = imread('second.tiff');
img = rgb2gray(img);
se = strel('disk', 5);
Output :-
8/11
5. Perform image segmentation for rice.png and cameraman.tif using
watershed segmentation algorithm (These images are available in
MATLAB or may be downloaded from Internet).
Code :-
rice = imread('rice.png');
cameraman = imread('cameraman.tif');
rice_gray = rgb2gray(rice);
cameraman_gray = rgb2gray(cameraman);
rice_gradient = imgradient(rice_gray);
cameraman_gradient = imgradient(cameraman_gray);
rice_ws = watershed(rice_gradient);
cameraman_ws = watershed(cameraman_gradient);
9/11
figure, imshow(label2rgb(rice_ws));
figure, imshow(label2rgb(cameraman_ws));
Output :-
10/11
11/11