-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_arenas_from_SAM_segments.m
64 lines (47 loc) · 1.97 KB
/
find_arenas_from_SAM_segments.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
all_segments_struct = struct('segment', {}, 'area', {}, 'best_fit_circle_correlation', {}, 'best_fit_circle_radius', {}, 'best_fit_circle', {});
% Pattern to match files starting with 'c' and ending with '.mat'
filePattern = 'c*.mat';
% Get a list of files that match the pattern
files = dir(filePattern);
% Count the number of files
numFiles = numel(files);
all_radius = zeros(numFiles, 1);
all_corrs = zeros(numFiles, 1);
% Display the number of files
disp(['Number of files: ', num2str(numFiles)]);
for i = 1:numFiles
fname = strcat('c', num2str(i), '.mat');
fname_data = load(fname);
seg = fname_data.segment;
area = fname_data.area;
all_segments_struct(i).segment = seg;
all_segments_struct(i).area = area;
% Find centroid of segmented areas
[centroidX, centroidY] = calculate_centroid(seg);
radius_range = 100:2:150;
corr_vals = zeros(length(radius_range), 1);
c = 1;
for r = radius_range
circle_img = generate_circle(centroidX, centroidY, r, size(seg, 1), size(seg, 2));
corr_vals(c) = corr2(seg, circle_img);
c = c + 1;
end
[max_corr, max_idx] = max(corr_vals);
all_radius(i) = radius_range(max_idx);
all_corrs(i) = max_corr;
all_segments_struct(i).best_fit_circle_correlation = max_corr;
all_segments_struct(i).best_fit_circle_radius = radius_range(max_idx);
all_segments_struct(i).best_fit_circle = generate_circle(centroidX, centroidY, radius_range(max_idx)+10, size(seg, 1), size(seg, 2));
end
% remove overlapping segments
overlap_removed_all_segments_struct = remove_overlapping_segments(all_segments_struct);
sorted_all_segments_struct = arrange_masks_area_desc_order(overlap_removed_all_segments_struct);
disp('find arena masks')
arena_masks = arenas_among_segments_algo(sorted_all_segments_struct);
if length(arena_masks) ~= 4
error('Could not find 4 arena masks');
end
for i = 1:4
mat = arena_masks(i).mask;
save(strcat('ARENA', num2str(i), '.mat'), 'mat');
end