forked from evodevosys/AroSpotFindingSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_trim.m
executable file
·123 lines (104 loc) · 2.29 KB
/
auto_trim.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
function [] = auto_trim(filename, output)
I = imread(filename, 1);
rows = size(I, 1);
cols = size(I, 2);
sensitive = false;
if rows > 1000 || cols > 1000
sensitive = true;
end
decrementer = ones(rows, cols);
info = imfinfo(filename);
num_frames = length(info);
num_frames = num_frames - 1;
num_frames = num_frames/2;
bg_brightness = mean(mean(I));
totals = [num_frames, 1];
for i = 1:num_frames
I = imread(filename, i);
total = sum(sum(I));
totals(i, 1) = total;
end
sigma = std(totals, 0, 1);
dim = false;
if (sigma(1)/bg_brightness) < 3.000e+03
dim = true;
end
gradients = [num_frames, 1];
start = 0;
if ~dim
for i = 1:num_frames
I = imread(filename, i);
I = imgaussfilt(I, 5);
cutoff = 1.1;
if sensitive
cutoff = 1.5;
end
seg_I = imquantize(I, cutoff * bg_brightness);
seg_I = seg_I - decrementer;
total = sum(sum(seg_I));
gradients(i, 1) = total;
threshold = 1;
if sensitive
threshold = 20;
end
if total > threshold
if start == 0
start = i;
end
end
end
else
for i = 1:num_frames
I = imread(filename, i);
I = imgaussfilt(I, 5);
cutoff = 1.07;
seg_I = imquantize(I, cutoff * bg_brightness);
seg_I = seg_I - decrementer;
total = sum(sum(seg_I));
gradients(i, 1) = total;
threshold = 90;
if total > threshold
if start == 0
start = i;
end
end
end
end
start = start - 2;
if sensitive
start = start - 4;
end
finish = 0;
if sensitive
finish = num_frames;
else
for j = 1:(num_frames - 5)
dx = gradient(gradients(j:j+5, 1));
if dx < 0
if finish == 0 || j < 151 - 15
if j < 151 - 15
finish = j+15;
else
finish = j+5;
end
end
end
end
end
if start < 1
disp('reset');
start = 1;
end
if finish > num_frames
finish = num_frames;
end
if finish == 0
finish = num_frames;
end
disp(start);
disp(finish);
for k = start:finish
I = imread(filename, k);
imwrite(I(:, :), output, 'WriteMode', 'append', 'Compression','none');
end
return;