-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeDistances.m
82 lines (72 loc) · 2.83 KB
/
computeDistances.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function computeDistances(t, width, commandsD, lbOpened, originalImage, ...
inds, imageProps)
delete(t);
t = text(width + 50, 200, commandsD, 'FontWeight', 'bold');
t.BackgroundColor = 'w';
t.Color = 'k';
t.FontSmoothing = 'on';
t.FontSize = 13;
t.Margin = 5;
t1 = 0;
while(true)
[x0, y0, button4] = ginput(1);
if (button4 == 1)
% Object that was clicked on
ret = lbOpened(round(y0),round(x0));
if (ret ~= 0)
imshow(originalImage);
% Compute distance between objects
listDistance = struct('Distance', {}, 'Index', {});
totalDistance = 0;
for a=1:length(inds)
if(a ~= ret)
x1 = imageProps(ret).Centroid(1);
y1 = imageProps(ret).Centroid(2);
x2 = imageProps(inds(a)).Centroid(1);
y2 = imageProps(inds(a)).Centroid(2);
distance = sqrt((x1-x2).^2 + (y1-y2).^2);
totalDistance = totalDistance + distance;
% Add distance to struct
sim = struct('Distance', distance, 'Index', inds(a));
listDistance = [listDistance ; sim];
end
end
[sorted, ind] = sort([listDistance.Distance]);
for a=1:length(ind)
x1 = imageProps(ret).Centroid(1);
y1 = imageProps(ret).Centroid(2);
x2 = imageProps(listDistance(ind(a)).Index).Centroid(1);
y2 = imageProps(listDistance(ind(a)).Index).Centroid(2);
plot([round(x1) round(x2)], [round(y1) round(y2)], 'k', 'LineWidth', 0.5 + 0.8 * a);
plot(x2, y2, 'ro', 'LineWidth', 6);
end
if t1 ~= 0
delete(t1);
end
texto = {'Total relative distance: ', ' ', totalDistance};
t1 = text(width + 50, 500, texto, 'FontWeight', 'bold');
t1.BackgroundColor = 'w';
t1.Color = 'k';
t1.FontSmoothing = 'on';
t1.FontSize = 13;
t1.Margin = 5;
% Plot centroid of chosen region
plot(imageProps(ret).Centroid(1), imageProps(ret).Centroid(2), 'ro', 'LineWidth', 6);
end
end
if (button4 == 114) % Press r to reset
if t1 ~= 0
delete(t1);
end
imshow(originalImage);
end
if (button4 == 113) % Press q to quit
if t1 ~= 0
delete(t1);
end
delete(t);
imshow(originalImage);
break;
end
end
end