Uploaded by alixhr999

Dip assignment 1

advertisement
UNIVERSITY OF ENGINEERING AND
TECHNOLOGY TAXILA
SOFTWARE ENGINEERING DEPARTMENT
Session 2k20
6th Semester
Digital Image Processing
Assignment 1
Submitted to
Dr. Ali Javed
Submitted by
Muhammad Ali Ejaz
Reg number
20-SE-40
Question No: 1
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
Answer:
MATLAB Code:
% Load binary image
img = [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];
% Initialize label matrix
label = zeros(size(img));
% Set current label to 1
curr_label = 1;
% Iterate over each pixel in the image
for i = 1:size(img,1)
for j = 1:size(img,2)
% If pixel is part of an object and not yet labeled
if img(i,j) == 1 && label(i,j) == 0
% Initialize a list to store connected pixels
pix_list = [i, j];
% Set current pixel label to current label
label(i,j) = curr_label;
% Check 8-connected neighbors for connectivity
while ~isempty(pix_list)
% Get the first pixel from the list
curr_pix = pix_list(1,:);
% Check the 8-connected neighbors
for m = -1:1
for n = -1:1
% Skip current pixel
if m == 0 && n == 0
continue;
end
% Get the neighbor pixel coordinates
nbr_pix = curr_pix + [m, n];
% Check if neighbor pixel is within image bounds
if nbr_pix(1) < 1 || nbr_pix(1) > size(img,1) || nbr_pix(2) < 1 || nbr_pix(2) >
size(img,2)
continue;
end
% Check if neighbor pixel is part of object and not yet labeled
if img(nbr_pix(1), nbr_pix(2)) == 1 && label(nbr_pix(1), nbr_pix(2)) == 0
% Add neighbor pixel to list
pix_list = [pix_list; nbr_pix];
% Set neighbor pixel label to current label
label(nbr_pix(1), nbr_pix(2)) = curr_label;
end
end
end
% Remove current pixel from list
pix_list(1,:) = [];
end
% Increment current label
curr_label = curr_label + 1;
end
end
end
% Display results
figure;
subplot(1,2,1);
imshow(img);
title('Binary image');
subplot(1,2,2);
imshow(label, []);
title('Connected component labels');
Resulted Images:
Question No: 2
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).
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.
Answer: (Part 1)
MATLAB Code:
x = imread('cameraman.tif');
figure,imshow(x);
[r,c] = size (x);
output=zeros(r,c);
for i = 1 : r
for j = 1 : c
if x(i,j) > 128
output(i,j)=1;
else
output(i,j)=0;
end
end
end
figure,imshow(output);
Resulted Images:
Answer: (Part 2)
MATLAB Code:
img = imread('cameraman.tif');
binary_img = imbinarize(img);
% Initialize output arrays
distance_euclidean = zeros(size(binary_img));
distance_d4 = zeros(size(binary_img));
distance_d8 = zeros(size(binary_img));
% Compute Euclidean distance transform
for i = 1:size(binary_img,1)
for j = 1:size(binary_img,2)
if binary_img(i,j) == 0
% If pixel is not part of object, set distance to 0
distance_euclidean(i,j) = 0;
else
% Compute Euclidean distance to nearest edge pixel
distance_euclidean(i,j) = sqrt(min((i - (1:size(binary_img,1))).^2 + (j (1:size(binary_img,2))).^2));
end
end
end
% Compute D-4 distance transform
for i = 1:size(binary_img,1)
for j = 1:size(binary_img,2)
if binary_img(i,j) == 0
% If pixel is not part of object, set distance to 0
distance_d4(i,j) = 0;
else
% Compute D-4 distance to nearest edge pixel
d4_vals = [distance_d4(max(i-1,1),j), distance_d4(i,max(j-1,1)),
distance_d4(i,min(j+1,size(binary_img,2))), distance_d4(min(i+1,size(binary_img,1)),j)];
distance_d4(i,j) = min(d4_vals) + 1;
end
end
end
% Compute D-8 distance transform
for i = 1:size(binary_img,1)
for j = 1:size(binary_img,2)
if binary_img(i,j) == 0
% If pixel is not part of object, set distance to 0
distance_d8(i,j) = 0;
else
% Compute D-8 distance to nearest edge pixel
d8_vals = [distance_d8(max(i-1,1),j), distance_d8(i,max(j-1,1)),
distance_d8(i,min(j+1,size(binary_img,2))), distance_d8(min(i+1,size(binary_img,1)),j), ...
distance_d8(max(i-1,1),max(j-1,1)), distance_d8(min(i+1,size(binary_img,1)),max(j-1,1)),
distance_d8(max(i-1,1),min(j+1,size(binary_img,2))),
distance_d8(min(i+1,size(binary_img,1)),min(j+1,size(binary_img,2)))];
distance_d8(i,j) = min(d8_vals) + 1;
end
end
end
% Display results
figure;
subplot(2,2,1);
imshow(binary_img);
title('Binary image');
subplot(2,2,2);
imshow(distance_euclidean, []);
title('Euclidean distance transform');
subplot(2,2,3);
imshow(distance_d4, []);
title('D-4 distance transform');
subplot(2,2,4);
imshow(distance_d8, []);
title('D-8 distance transform');
Resulted Images:
Download