-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_easy.m
89 lines (76 loc) · 2.75 KB
/
lab5_easy.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
83
84
85
86
87
88
89
close all;
clear all;
%% Reading image
im = imread('Treasure_easy.jpg'); % Edit mode
imshow(im);
pause;
%% Binarisation
bin_threshold = 0.09; % parameter to vary
bin_im = im2bw(im, bin_threshold);
imshow(bin_im);
pause;
%% Extracting connected components
con_com = bwlabel(bin_im);
imshow(label2rgb(con_com));
pause;
%% Computing objects properties
props = regionprops(con_com);
%% Drawing bounding boxes
n_objects = numel(props);
imshow(im);
hold on;
for object_id = 1 : n_objects
rectangle('Position', props(object_id).BoundingBox, 'EdgeColor', 'b');
end
hold off;
pause;
%% Arrow/non-arrow determination
% You should develop a function arrow_finder, which returns the IDs of the arror objects.
% IDs are from the connected component analysis order. You may use any parameters for your function.
arrow_ind = arrow_finder(props);
%% Finding red arrow
n_arrows = numel(arrow_ind);
start_arrow_id = 0;
% check each arrow until find the red one
for arrow_num = 1 : n_arrows
object_id = arrow_ind(arrow_num); % determine the arrow id
% extract colour of the centroid point of the current arrow
centroid_colour = im(round(props(object_id).Centroid(2)), round(props(object_id).Centroid(1)), :);
if centroid_colour(:, :, 1) > 240 && centroid_colour(:, :, 2) < 10 && centroid_colour(:, :, 3) < 10
% the centroid point is red, memorise its id and break the loop
start_arrow_id = object_id;
break;
end
end
%% Hunting
cur_object = start_arrow_id; % start from the red arrow
path = cur_object;
% while the current object is an arrow, continue to search
i=0;
while ismember(cur_object, arrow_ind)
% You should develop a function next_object_finder, which returns
% the ID of the nearest object, which is pointed at by the current
% arrow. You may use any other parameters for your function.
i=i+1;
%% Finding yellow pixels
path(i) = cur_object; %%next_easy function for easy test
cur_object = next_easy(cur_object,props,path);%%next function is for medium but not successful
path(i) = cur_object;
if i>1 && path(i)==path(i-1)%Stop condition
break;
end
end
disp('The movement is');
disp(path);
%% visualisation of the path
imshow(im);
hold on;
for path_element = 1 : numel(path) - 1
object_id = path(path_element); % determine the object id
rectangle('Position', props(object_id).BoundingBox, 'EdgeColor', 'y');
str = num2str(path_element);
text(props(object_id).BoundingBox(1), props(object_id).BoundingBox(2), str, 'Color', 'r', 'FontWeight', 'bold', 'FontSize', 14);
end
% visualisation of the treasure
treasure_id = path(end);
rectangle('Position', props(treasure_id).BoundingBox, 'EdgeColor', 'g');