Added some documentation to make further development easier.
function object_count(imgfile)
img = imread(imgfile);
% Converts RGB image to B&W
imgbw = im2bw(img);
%============== Filters image ===============%
for a = 1 : 6
imgbw = medfilt2(imgbw);
end
%============= Counts objects ===============%
% Detects all object bounds (edges) in image,
% including hole bounds.
totalbound = bwboundaries(imgbw, 'holes');
% Detects all object bounds, excluding hole bounds.
objectcount = bwboundaries(imgbw, 'noholes');
% Outputs the result.
total_objects = size(objectcount, 1)
hole_objects = size(totalbound, 1) - size(objectcount, 1)
solid_objects = size(objectcount, 1) - hole_objects
%============= Image Labeling ===============%
L = bwlabel(imgbw, 8);
%============= Euler numbers ================%
s = regionprops(L, 'EulerNumber');
fprintf('Object Number\tEuler Number\tDescription\n');
for i = 1 : total_objects
fprintf('%d\t\t\t\t%d\t\t\t\t', i, s(i).EulerNumber);
if (s(i).EulerNumber == 1) fprintf('solid\n');
else fprintf('holed\n');
end
end
%=========== Colorizes the regions ==========%
% Takes the pixels
PxList = regionprops(L, 'PixelList');
% Converts B&W image to RGB.
imgbw2 = uint8(zeros(size(imgbw, 1), size(imgbw, 2), 3));
% For each object, do
for i = 1 : total_objects
plist = PxList(i).PixelList;
% If the region has a hole
if (s(i).EulerNumber == 0)
% For each pixels
for j = 1 : size(plist, 1)
% Colorizes it with red
imgbw2(plist(j, 2), plist(j, 1), 1) = 255;
end
% If the region has no hole
else
% For each pixels
for j = 1 : size(plist, 1)
% Colorizes it with green
imgbw2(plist(j, 2), plist(j, 1), 2) = 255;
end
end
end
%=========== Displays the result ===========%
figure('Name', 'Membedakan Objek Padat dan Berlubang', 'NumberTitle', 'off');
subplot(1, 2, 1), imshow(img), title('Citra Asli');
subplot(1, 2, 2), imshow(imgbw2), title('Citra Hasil');


mbois tok wes, fotone pisan...wkwkwkwk
ReplyDelete.... lupa komen trus... fotomu kok sok imut ngono... XD
ReplyDeletety for the documentation, help me understand the script a lot..
文君
thanks2 :D
ReplyDeleteWoi ganteng..
ReplyDeleteTambah kereng e saiki bos ndal wkwkwkwk....