% Object Detection
% This program detects the objects found in the imported image. The
% technique used is the 8-connected iterative approach for connected
% component labeling.
clear all
clc
figure(1)
I = imread('image.bmp'); % Importing RGB image from current directory
I = rgb2gray(I);
imshow(I,[]); % Show image as grayscale picture
[M,N] = size(I');
figure(2)
I_bin = edge(I,'canny'); % Performing edge detection with Canny method
imshow(I_bin,[]); % Show edge detection
I_bw = I_bin';
figure(3) % Labeling with connected component algorithm
label = 1; % iterative approach
for i = 1:M
for j = 1:N
if I_bw(i,j) % Generate labels for all pixels with binary
L(i,j) = label; % value 1
label = label + 1;
else
L(i,j) = 0;
end
end
end
L1 = L;
C = zeros(M,N);
counter = 0;
A = I_bw; % Temporary image used for comparison purposes
B = L1; % Temporary image used for comparison purposes
flip = 0; % Flag for tracking path of label generation
% 0: top to bottom, left to right
% 1: bottom to top, right to left
L2 = zeros(M,N);
while ~(isequal(A,B)||(isequal(B,C)&& ~isequal(A,B)))
% Reiteration for updating label continues
C = A; % Matrix from two prior iterations
A = L1; % Updating comparison matrix before alteration
% is incurred to the original matrix L1
if(~flip) % until no further change occurs
for i = 2:M-1
for j = 2:N-1
if L1(i,j) % Labeling with respect to 8-neighbor connections
if L1(i-1,j-1)
L2(i,j) = L1(i-1,j-1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i,j-1)
L2(i,j) = L1(i,j-1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i+1,j-1)
L2(i,j) = L1(i+1,j-1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i-1,j)
L2(i,j) = L1(i-1,j);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
else
L2(i,j) = L1(i,j);
end
end
end
end
B = L2; % Updating comparison matrix
L1 = L2; % Updating labeling matrices
flip = ~flip;
else
j = N - 1;
while (j ~= 1)
i = M - 1;
while (i ~= 1)
if L1(i,j) % Labeling with respect to 8-neighbor connections
if L1(i+1,j+1)
L2(i,j) = L1(i+1,j+1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i,j+1)
L2(i,j) = L1(i,j+1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i-1,j+1)
L2(i,j) = L1(i-1,j+1);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
elseif L1(i+1,j)
L2(i,j) = L1(i+1,j);
if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
if L2(i-1,j-1)
L2(i-1,j-1) = min(C(i,j),L1(i,j));
L1(i-1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i-1,j)
L2(i-1,j) = min(C(i,j),L1(i,j));
L1(i-1,j) = min(C(i,j),L1(i,j));
end
if L2(i-1,j+1)
L2(i-1,j+1) = min(C(i,j),L1(i,j));
L1(i-1,j+1) = min(C(i,j),L1(i,j));
end
if L2(i,j-1)
L2(i,j-1) = min(C(i,j),L1(i,j));
L1(i,j-1) = min(C(i,j),L1(i,j));
end
if L2(i,j+1)
L2(i,j+1) = min(C(i,j),L1(i,j));
L1(i,j+1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j-1)
L2(i+1,j-1) = min(C(i,j),L1(i,j));
L1(i+1,j-1) = min(C(i,j),L1(i,j));
end
if L2(i+1,j)
L2(i+1,j) = min(C(i,j),L1(i,j));
L1(i+1,j) = min(C(i,j),L1(i,j));
end
if L2(i+1,j+1)
L2(i+1,j+1) = min(C(i,j),L1(i,j));
L1(i+1,j+1) = min(C(i,j),L1(i,j));
end
end
L1(i,j) = L2(i,j);
else
L2(i,j) = L1(i,j);
end
end
i = i - 1;
end
j = j - 1;
end
B = L2; % Updating comparison matrix
L1 = L2; % Updating labeling matrices
flip = ~flip;
end
counter = counter + 1
end
imshow(L2',[]); % Displaying labelled objects
figure(4)
[W,num] = bwlabel(I_bin,8); % Labeling with Matlab command bwlabel
imshow(W,[]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%