You are on page 1of 6

UNIVERSITY OF ENGINEERING AND TECNOLOGY, TAXILA

DIP(TH) ASSIGNMENT 01

SUBMITTED BY:
MAHEERA AFTEB
21-SE-07
ALPHA
SUBMITTED TO:
Dr. ALI JAVED

DATE: 07-Feb-2024
Q1. For the given binary image strip, implement Connected Component Labeling
Algorithm for 8-connectivity in Matlab. You can use built-in functions only for
reading and displaying images (imread/ imshow etc).
11100000
11101100
11101100
11100010
11100010
11100010
11100110
11100000
Ans. Matlab Code
% Define the binary image strip
binary_strip = [1 1 1 0 0 0 0 0;
1 1 1 0 1 1 0 0;
1 1 1 0 1 1 0 0;
1 1 1 0 0 0 1 0;
1 1 1 0 0 0 1 0;
1 1 1 0 0 0 1 0;
1 1 1 0 0 1 1 0;
1 1 1 0 0 0 0 0];

% Display the binary image strip


disp('Binary Image Strip:')
disp(binary_strip);

% Initialize label matrix


label_matrix = zeros(size(binary_strip));

% Initialize current label


current_label = 1;

% Define 8-connectivity neighbors


neighbors = [-1, -1; % northwest
0, -1; % north
1, -1; % northeast
1, 0; % east
1, 1; % southeast
0, 1; % south
-1, 1; % southwest
-1, 0]; % west

% Iterate through each pixel


for i = 1:size(binary_strip, 1)
for j = 1:size(binary_strip, 2)
% Check if the current pixel is foreground
if binary_strip(i, j) == 1
% Initialize a list of connected labels
connected_labels = [];

% Iterate through neighbors


for k = 1:size(neighbors, 1)
% Calculate neighbor coordinates
neighbor_x = i + neighbors(k, 1);
neighbor_y = j + neighbors(k, 2);

% Check if the neighbor is within the image boundaries


if neighbor_x >= 1 && neighbor_x <= size(binary_strip, 1) &&
...
neighbor_y >= 1 && neighbor_y <= size(binary_strip, 2)
% Check if the neighbor pixel is labeled
if label_matrix(neighbor_x, neighbor_y) ~= 0
% Add the neighbor's label to the connected labels
list
connected_labels = [connected_labels,
label_matrix(neighbor_x, neighbor_y)];
end
end
end

% If there are no connected labels


if isempty(connected_labels)
% Assign a new label
label_matrix(i, j) = current_label;
% Increment the current label
current_label = current_label + 1;
else
% Assign the minimum label among connected labels
label_matrix(i, j) = min(connected_labels);

% Update equivalence among connected labels


for l = 1:length(connected_labels)
label_matrix(label_matrix == connected_labels(l)) =
min(connected_labels);
end
end
end
end
end

% Display the label matrix


disp('Label Matrix:')
disp(label_matrix);
Output:

Q2. Pick the image (cameraman.tif) from the matlab and perform the following
operations.
a) Convert the image into binary without a built-in function (e.g im2bw).
Ans. Matlab code:
% Read the image
image = imread('cameraman.tif');

% Display the original image


figure;
imshow(image);
title('Original Image');

% Define threshold value


threshold = 128; % for example, you can adjust this value as needed

% Convert the image to binary based on the threshold


binary_image = image > threshold;

% Display the binary image


figure;
imshow(binary_image);
title('Binary Image');
Output:

b) Compute the distance transform of that binary image using Euclidean, D-


4, and D-8 distance measures without using any built-in function (e.g
bwdist) and show the results in one image window.
Ans. Matlab code:
% Read the image
binary_image = imread('cameraman.tif');
binary_image = im2bw(binary_image); % Convert to binary if not already

% Compute the size of the image


[m, n] = size(binary_image);

% Initialize distance transform matrices


euclidean_dist = zeros(m, n);
d4_dist = zeros(m, n);
d8_dist = zeros(m, n);

% Iterate over each pixel in the binary image


for i = 1:m
for j = 1:n
if binary_image(i, j) == 1 % If the pixel is part of the object
% Compute Euclidean distance transform
for x = 1:m
for y = 1:n
euclidean_dist(i, j) = max(euclidean_dist(i, j), sqrt((i
- x)^2 + (j - y)^2));
end
end

% Compute D-4 distance transform


d4_dist(i, j) = min(abs(i - 1), abs(m - i)) + min(abs(j - 1),
abs(n - j));

% Compute D-8 distance transform


d8_dist(i, j) = min(abs(i - 1), abs(m - i)) + min(abs(j - 1),
abs(n - j));
d8_dist(i, j) = max(d8_dist(i, j), min(abs(i - 1), abs(j - n)));
d8_dist(i, j) = max(d8_dist(i, j), min(abs(m - i), abs(j - 1)));
d8_dist(i, j) = max(d8_dist(i, j), min(abs(m - i), abs(n - j)));
end
end
end

% Display the results in one image window


figure;
subplot(1, 3, 1);
imshow(euclidean_dist, []);
title('Euclidean Distance Transform');

subplot(1, 3, 2);
imshow(d4_dist, []);
title('D-4 Distance Transform');

subplot(1, 3, 3);
imshow(d8_dist, []);
title('D-8 Distance Transform');
Output:

You might also like