-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_flies_dist_based.m
174 lines (134 loc) · 6.56 KB
/
find_flies_dist_based.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function [fly_1_coords_over_time, fly_2_coords_over_time, dist_over_time, are_flies_present, num_of_frames_with_no_flies] = find_flies_dist_based(output_folder, mask, circle_area, tolerance_limit_for_num_frames_with_no_flies)
fly_min_area_percent = 0.01;
fly_max_area_percent = 2;
thresold_for_fly_color = 80;
dist_over_time = [];
fly_1_coords_over_time = [];
fly_2_coords_over_time = [];
are_flies_present = 1;
num_of_frames_with_no_flies = 0;
if ~strcmp(computer, 'GLNXA64')
files = dir([output_folder '\*.png']);
else
files = dir([output_folder '/*.png']);
end
counter = 1;
total_num_files = length(files);
progress_bar = waitbar(0, 'Starting');
for file = files'
try
% Your original code
waitbar(counter/total_num_files, progress_bar, sprintf('Identifying Flies in each frame: %d %%', floor(counter/total_num_files*100)));
catch ME
% Error handling code
fprintf('An error occurred: %s\n', ME.message);
fprintf('Current counter value: %d\n', counter);
fprintf('Total number of files: %d\n', total_num_files);
% Include any other relevant information here
% You can also perform other error handling operations here
end
% waitbar(counter/total_num_files, progress_bar, sprintf('Identifying Flies in each frame: %d %%', floor(counter/total_num_files*100)));
% read image
% WINDOWS
if ~strcmp(computer, 'GLNXA64')
img = imread(strcat(output_folder,'\', file.name));
else
img = imread(strcat(output_folder,'/', file.name));
end
if length(size(img)) == 3
img1 = rgb2gray(img);
else
img1 = img;
end
maskedFly1 = double(img1) .* mask;
% threshold for fly color
flies_logical = maskedFly1 < thresold_for_fly_color;
% images processing by matlab to identify blobs
cc = bwconncomp(double(flies_logical).*mask); % 'binaryImage' is the thresholded image
stats = regionprops(cc, 'Area', 'Centroid', 'BoundingBox');
% Visualize centroids on maskedFly1
% figure;
% imagesc(maskedFly1);
% hold on;
% for i = 1:length(stats)
% centroid = stats(i).Centroid;
% text(centroid(1), centroid(2), num2str(i), 'Color', 'red', 'FontSize', 12);
% end
% hold off;
% get fly indices based on area of blobs
fly_indices = [];
for i = 1:length(stats)
if 100*(stats(i).Area/circle_area) > fly_min_area_percent && 100*(stats(i).Area/circle_area) < fly_max_area_percent
fly_indices = [fly_indices i];
end
end
% if More than 2 flies are detected, choose the ones with biggest area, the smallest may be due to noise
if length(fly_indices) > 2
max_area1 = -1;
max_area2 = -1;
max_area1_ind = 0;
max_area2_ind = 0;
for f_ind = 1:length(fly_indices)
if stats(fly_indices(f_ind)).Area > max_area1
% Update max_area2 with old max_area1 before updating max_area1
max_area2 = max_area1;
max_area2_ind = max_area1_ind;
% Now update max_area1
max_area1 = stats(fly_indices(f_ind)).Area;
max_area1_ind = fly_indices(f_ind);
elseif stats(fly_indices(f_ind)).Area > max_area2
max_area2 = stats(fly_indices(f_ind)).Area;
max_area2_ind = fly_indices(f_ind);
end
end % for
fly_indices = [max_area1_ind max_area2_ind];
end % if
% TODO - Clean code, because now number of flies is atmost 2
if length(fly_indices) == 2
fly_coords(1,:) = stats(fly_indices(1)).Centroid;
fly_coords(2,:) = stats(fly_indices(2)).Centroid;
if counter > 1
% Distance based criteria
old_fly_1_coords = fly_1_coords_over_time(counter-1,:);
if pdist([fly_coords(1,:); old_fly_1_coords]) < pdist([fly_coords(2,:); old_fly_1_coords])
fly_1_coords_over_time = [fly_1_coords_over_time; fly_coords(1,:)];
fly_2_coords_over_time = [fly_2_coords_over_time; fly_coords(2,:)];
else
fly_1_coords_over_time = [fly_1_coords_over_time; fly_coords(2,:)];
fly_2_coords_over_time = [fly_2_coords_over_time; fly_coords(1,:)];
end
else
fly_1_coords_over_time = [fly_1_coords_over_time; fly_coords(1,:)];
fly_2_coords_over_time = [fly_2_coords_over_time; fly_coords(2,:)];
end
dist = pdist(fly_coords);
dist_over_time = [dist_over_time dist];
elseif length(fly_indices) == 1
dist_over_time = [dist_over_time 0];
fly_1_coords_over_time = [fly_1_coords_over_time; stats(fly_indices(1)).Centroid];
fly_2_coords_over_time = [fly_2_coords_over_time; stats(fly_indices(1)).Centroid];
else
disp('No flies detected')
% Visualize centroids on maskedFly1
if length(dist_over_time) > 1
dist_over_time = [dist_over_time dist_over_time(end-1)];
else
dist_over_time = [dist_over_time nan];
end
if size(fly_1_coords_over_time,1) > 1
fly_1_coords_over_time = [fly_1_coords_over_time; [fly_1_coords_over_time(end-1,1) fly_1_coords_over_time(end-1,2)]];
fly_2_coords_over_time = [fly_2_coords_over_time; [fly_2_coords_over_time(end-1,1) fly_2_coords_over_time(end-1,2)]];
else
fly_1_coords_over_time = [fly_1_coords_over_time; [nan nan]];
fly_2_coords_over_time = [fly_2_coords_over_time; [nan nan]];
end
num_of_frames_with_no_flies = num_of_frames_with_no_flies + 1;
if num_of_frames_with_no_flies > tolerance_limit_for_num_frames_with_no_flies
are_flies_present = 0;
break;
end
end % if
counter = counter + 1;
end % for file
close(progress_bar)
end