Skip to content

Commit 396d793

Browse files
author
omegion
committed
Update Code
1 parent 588bd09 commit 396d793

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

main.m

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
clear all; close all; clc; % clear defined variable from previous run
1+
clear all; close all; clc; % clear console and close all windows
22

3-
im = imread('objects.jpg'); %circles are greater than rectangles
4-
%im = imread('objects_1.jpg'); %circles and rectangles are equal
3+
im = imread('objects.jpg'); % circles are greater than rectangles
4+
% im = imread('objects_1.jpg'); % circles and rectangles are equal
55

6-
[h, w, ~] = size(im); %get height and width of image
6+
[h, w, ~] = size(im); % get height and width of the image
77

8-
distR = double(im(:, :, 1)) - 0; %since we have only one dimension, first dimension of image will be enough
8+
distR = double(im(:, :, 1)) - 0; % since we have only one dimension, first dimension of image will be enough
99

10-
%L2 distance
11-
d2 = (distR .^2); %highlighted to background, at the end we will have so big number for background but white parts will be close to 0
10+
% L2 distance
11+
d2 = (distR .^2); % highlighted to background, at the end we will have so big number for background but white parts will be close to 0
1212

1313
thres = 13000;
14-
results = d2 <= thres; %we set the boundary for image not to detect image itself but objects
14+
results = d2 <= thres; % we set the boundary for image not to detect image itself but objects
1515

1616
results = bwmorph(results, 'erode', 1);
1717
results = bwmorph(results, 'open', 2);
1818
results = bwmorph(results, 'close', 2);
1919

2020

21-
stats = regionprops(results, 'BoundingBox'); %this gets the stats of each object
21+
stats = regionprops(results, 'BoundingBox'); % this gets the stats of each object
2222

2323
figure, imshow(results);
2424
hold on
2525

2626
circle_num = 0;
2727
rec_num = 0;
2828
for i = 1 : length(stats)
29-
3029
dimensions = stats(i).BoundingBox;
30+
text = sprintf(int2str(i)); % put the iteration number to variable to print as string
3131

32-
YourText = sprintf(int2str(i)); %put the iteration number to variable to print as string
33-
34-
if dimensions(3)== dimensions(4) %3 and 4 stand for height and width of object, if the values are equal it is circle otherwise ellipse
32+
if dimensions(3) == dimensions(4) % 3 and 4 stand for height and width of object, if the values are equal it is circle otherwise ellipse
3533
circle_num = circle_num+1;
36-
hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,YourText,'Color',[1 0 1],'FontSize',15); %put the pink string to image
37-
elseif dimensions(3)~= dimensions(4)
34+
hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,text,'Color',[1 0 1],'FontSize',15); % put the pink string to image
35+
elseif dimensions(3) ~= dimensions(4)
3836
rec_num = rec_num+1;
39-
hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,YourText,'Color',[0.1 0 0.5],'FontSize',15); %put the blue string to image
37+
hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,text,'Color',[0.1 0 0.5],'FontSize',15); % put the blue string to image
4038
end
4139

42-
rectangle('Position', stats(i).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 3); %draw a rectangle
40+
rectangle('Position', stats(i).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 3); % draw a rectangle
4341

4442
end
4543

0 commit comments

Comments
 (0)