1+ % Hakan Kurtulus
2+ % HXMKGP
3+
4+ clear all ; close all ; clc ; % clear defined variable from previous run
5+
6+ im = imread(' objects.jpg' ); % circles are greater than rectangles
7+ % im = imread('objects_1.jpg'); %circles and rectangles are equal
8+
9+ [h , w , ~ ] = size(im ); % get height and width of image
10+
11+ distR = double(im(: , : , 1 )) - 0 ; % since we have only one dimention, first dimention of image will be enough
12+
13+ % L2 distance
14+ d2 = (distR .^ 2 ); % highlited to background, at the end we will have so big number for background but white parts will be close to 0
15+
16+ thres = 13000 ;
17+ results = d2 <= thres ; % we set the boundary for image not to detect image itself but objects
18+
19+ results = bwmorph(results , ' erode' , 1 );
20+ results = bwmorph(results , ' open' , 2 );
21+ results = bwmorph(results , ' close' , 2 );
22+
23+
24+ stats = regionprops(results , ' BoundingBox' ); % this gets the stats of each object, we used this with Gabor
25+
26+ figure , imshow(results );
27+ hold on
28+
29+ circle_num = 0 ;
30+ rec_num = 0 ;
31+ for i = 1 : length(stats )
32+
33+ dimentions = stats(i ).BoundingBox;
34+
35+ YourText = sprintf(int2str(i )); % put the iteration numbe rto variable to print as string
36+
37+ if dimentions(3 )== dimentions(4 ) % 3 and 4 stand for height and width of object, if the values are equal it is circle ortherwise ellipse
38+ circle_num = circle_num + 1 ;
39+ hText = text(dimentions(1 ) + dimentions(3 )/2 ,dimentions(2 )+dimentions(4 )/2 ,YourText ,' Color' ,[1 0 1 ],' FontSize' ,15 ); % put the pink string to image
40+ elseif dimentions(3 )~= dimentions(4 )
41+ rec_num = rec_num + 1 ;
42+ hText = text(dimentions(1 ) + dimentions(3 )/2 ,dimentions(2 )+dimentions(4 )/2 ,YourText ,' Color' ,[0.1 0 0.5 ],' FontSize' ,15 ); % put the blue string to image
43+ end
44+
45+ rectangle(' Position' , stats(i ).BoundingBox, ' EdgeColor' , ' g' , ' LineWidth' , 3 ); % draw a rectangle also we used this with Gabor
46+
47+ end
48+
49+ h = [int2str(circle_num ) ' Circle is found' ]
50+ k = [int2str(rec_num ) ' Rectangle is found' ]
51+
52+ if circle_num == rec_num
53+ final = ' Circle and Rectangle numbers are equal.'
54+ elseif circle_num < rec_num
55+ final = ' Rectangle numbers are greater than circle numbers.'
56+ elseif circle_num > rec_num
57+ final = ' Circle numbers are greater than rectangler numbers.'
58+ end
59+
0 commit comments