%%
clear all; close all;

%%
I = imread('widgets.jpg');
I1 = im2bw(I, graythresh(I));

%%
figure, imshow(I1);

%%

figure, imshow(~I1);

%%
I3 = imfill(I1,'holes');
imshow(I3);

%%

[B1,L1] = bwboundaries (I3, 'noholes');

%%
[B2,L2] = bwboundaries (~I1, 'noholes');
holes = length(B2)-1;

%%
statsHoles = regionprops(L2,'Centroid', 'Area');
		

%%
statsObjs = regionprops(L1,'PixelList');

%%
clear count;
clear found;
count = 0;
for i=1:length(statsObjs)
	found = 0;
	for j=1:length(statsHoles)
		if (statsHoles(j).Area < 50)
			for k=1:length(statsObjs(i).PixelList)
				if ((int32(statsHoles(j).Centroid(1)) == statsObjs(i).PixelList(k,1)) && (int32(statsHoles(j).Centroid(2)) == statsObjs(i).PixelList(k,2)))
					count = count +1;
					found=1;
					break,
				end
				
			end
				if (found == 1)
					break;
				end
		end
	end
end
