0% found this document useful (0 votes)
15 views11 pages

Assign 3

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Assign 3

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment 3:

Digital Image Processing

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(:);

encodedData = huffman_encode(imgVector, huffmanDict);

decodedVector = huffman_decode(encodedData, huffmanDict);

reconstructedImg = reshape(decodedVector, size(imgData));

mseError = immse(imgData, reconstructedImg);


disp(['Reconstruction Error (MSE): ', num2str(mseError)]);

figure;
subplot(1, 2, 1);
imshow(imgData); title('Original Image');
subplot(1, 2, 2);
imshow(reconstructedImg); title('Reconstructed Image');

function huffmanDict = create_huffman_dict(symbols, prob)


nodes = num2cell(symbols);

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

function huffmanDict = build_dict(node, prefix, symbols)


if iscell(node)
left = build_dict(node{1}, [prefix 0], symbols);
right = build_dict(node{2}, [prefix 1], symbols);
huffmanDict = [left; right];
else
huffmanDict = {node, prefix};
end
end

function encoded = huffman_encode(imgVector, huffmanDict)


encoded = [];
for i = 1:length(imgVector)
symbol = imgVector(i);
for j = 1:length(huffmanDict)
if huffmanDict{j, 1} == symbol
encoded = [encoded huffmanDict{j, 2}];
break;
end
end
end
end

function decoded = huffman_decode(encoded, huffmanDict)


decoded = [];
temp = [];
for i = 1:length(encoded)
temp = [temp encoded(i)];

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:-

Huffman coding provides lossless compression, so the reconstructed


image is identical to the original one.
Since Huffman coding is a lossless compression method, the MSE
(Mean Squared Error) between the original and reconstructed
images should be 0, meaning that there is no difference between the
two images.

2. Read a gray scale image and compress it using LZW coding.


Reconstruct the image from the compressed code and compute the
error. What do you observe?
Code:-
img = imread('cameraman.tif');

imwrite(img, 'compressed_image.tif', 'Compression', 'lzw');

decompressed_img = imread('compressed_image.tif');

error = immse(double(img), double(decompressed_img));

figure, subplot(1, 2, 1), imshow(img), title('Original


Image');
subplot(1, 2, 2), imshow(decompressed_img, []),
title('Reconstructed Image');

disp(['Mean Squared Error: ', num2str(error)]);

Output:-

5/11
Observation:-

The reconstructed image after LZW compression looks almost identical


to the original. The mean squared error is very low, indicating minimal
loss during compression. LZW compression works well for lossless image
formats like TIFF, preserving image quality while reducing file size.

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);

sobel_edges = edge(img, 'sobel');


figure, imshow(sobel_edges);

6/11
roberts_edges = edge(img, 'roberts');
figure, imshow(roberts_edges);

canny_edges = edge(img, 'canny');


figure, imshow(canny_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);

eroded_img = imerode(img, se);


figure, imshow(eroded_img);

dilated_img = imdilate(img, se);


figure, imshow(dilated_img);

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

You might also like