|
1 | | -clear all; close all; clc; % clear defined variable from previous run |
| 1 | +clear all; close all; clc; % clear console and close all windows |
2 | 2 |
|
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 |
5 | 5 |
|
6 | | -[h, w, ~] = size(im); %get height and width of image |
| 6 | +[h, w, ~] = size(im); % get height and width of the image |
7 | 7 |
|
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 |
9 | 9 |
|
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 |
12 | 12 |
|
13 | 13 | 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 |
15 | 15 |
|
16 | 16 | results = bwmorph(results, 'erode', 1); |
17 | 17 | results = bwmorph(results, 'open', 2); |
18 | 18 | results = bwmorph(results, 'close', 2); |
19 | 19 |
|
20 | 20 |
|
21 | | -stats = regionprops(results, 'BoundingBox'); %this gets the stats of each object |
| 21 | +stats = regionprops(results, 'BoundingBox'); % this gets the stats of each object |
22 | 22 |
|
23 | 23 | figure, imshow(results); |
24 | 24 | hold on |
25 | 25 |
|
26 | 26 | circle_num = 0; |
27 | 27 | rec_num = 0; |
28 | 28 | for i = 1 : length(stats) |
29 | | - |
30 | 29 | dimensions = stats(i).BoundingBox; |
| 30 | + text = sprintf(int2str(i)); % put the iteration number to variable to print as string |
31 | 31 |
|
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 |
35 | 33 | 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) |
38 | 36 | 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 |
40 | 38 | end |
41 | 39 |
|
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 |
43 | 41 |
|
44 | 42 | end |
45 | 43 |
|
|
0 commit comments