- Edited
@Arithma: I can do my own programming thank you very much. The idea was to see if someone could use the pseudo-code to write the program in C/C++ or any other language. The code presented here is in Matlab, enjoy :)
(PS: If the people on this forum are interested in image processing, I did this great project on liscence plate recognition and character segmentation and could post it)
First import an RGB image, or any image really, and perfrom what is known as edge detection to turn it into a binary image. This is a good way of identfying objects (i.e. edge detection) which is based on determining the contours of the various objects in the image. If this code is to be executed for a randomly generated binary matrix, skip the code sections of figure(1) and figure(2) as they are useless then.
This concerns the iterative approach for the 4-connected labelling. Tomorrow I'll post that of the 8-connected iterative approach.
(PS: If the people on this forum are interested in image processing, I did this great project on liscence plate recognition and character segmentation and could post it)
First import an RGB image, or any image really, and perfrom what is known as edge detection to turn it into a binary image. This is a good way of identfying objects (i.e. edge detection) which is based on determining the contours of the various objects in the image. If this code is to be executed for a randomly generated binary matrix, skip the code sections of figure(1) and figure(2) as they are useless then.
% Object Detection
% This program detects the objects found in the imported image. The
% technique used is the 4-connected iterative approach for connected
% component labeling.
clear all
clc
figure(1)
I = imread('image.bmp'); % Importing an RGB picture from current directory
I = rgb2gray(I);
imshow(I,[]); % Show picture in grayscale
[M,N] = size(I);
figure(2)
I_bw = edge(I,'canny'); % Performing edge detection with Canny method
imshow(I_bw,[]); % Show edge detection
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;
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
while(~isequal(A,B)) % Reiteration for updating label continues
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 4-neighbor connections
if L1(i,j-1)
L2(i,j) = L1(i,j-1);
L1(i,j) = L2(i,j);
elseif L1(i-1,j)
L2(i,j) = L1(i-1,j);
L1(i,j) = L2(i,j);
else
L2(i,j) = L1(i,j);
end
else
L2(i,j) = 0;
end
end
end
L2(M,:) = 0;
L2(:,N) = 0;
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 4-neighbor connections
if L1(i+1,j)
L2(i,j) = L1(i+1,j);
L1(i,j) = L2(i,j);
elseif L1(i,j+1)
L2(i,j) = L1(i,j+1);
L1(i,j) = L2(i,j);
else
L2(i,j) = L1(i,j);
end
else
L2(i,j) = 0;
end
i = i - 1;
end
j = j - 1;
end
L2(M,:) = 0;
L2(:,N) = 0;
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_bw,4); % Labeling with Matlab command bwlabel
imshow(W,[]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Note that the last section figure(4) is used to compare my code to the command used for connected component labeling in Matlab.This concerns the iterative approach for the 4-connected labelling. Tomorrow I'll post that of the 8-connected iterative approach.