-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamic_local_maxima.m
91 lines (87 loc) · 1.94 KB
/
dynamic_local_maxima.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
function [idx,peak,Ntrees] = dynamic_local_maxima(OHM)
% parameters
[M, N]=size(OHM);
thres = 5;
minsrh = 3;
if nanmax(OHM(:)) >15
maxsrh = 5;
else
maxsrh = 5;
end
% remove nan
%hh(isnan(hh))=[];
OHM(isnan(OHM))=0;
hh = OHM;
%step search size
stepss = [minsrh:2:maxsrh];
% vectorise OHM & filter <2m
hh(hh<2)=[];
%[mm,nn] = size(OHM);
% plot histogram & divide data from thres to quantile 0.99
%hh = hist(H,length(stepss));
hq = quantile(hh,0.99);
% generate sequence with length(stepss) interval
ss = thres:(hq-thres)/(length(stepss)-1):hq;
% ???? do not understand logic
Max = OHM;%reshape(OHM(:),N,M);
%Max = fliplr(Max);
Gnew = Max;
Max = zeros(size(Max));
Index = zeros(size(Max));
dd = length(stepss);
% define edges
[j,i] = find(Gnew);
II = [i,j];
c = find(i>=ceil(minsrh/2));
II = II(c,:);
c = find(II(:,1)<=size(Gnew,1)-ceil(minsrh/2));
II = II(c,:);
c = find(II(:,2)>=ceil(minsrh/2));
II = II(c,:);
c = find(II(:,2)<=size(Gnew,2)-ceil(minsrh/2));
II = II(c,:);
clear c i j
index = 1;
for i = 1:length(II)
% each grid
k = II(i,1);
r = II(i,2);
%check height quantile
if Gnew(r,k)<max(ss)
pos = find(Gnew(r,k)<ss, 1 )-1;
else
pos = dd;
end
if pos ==0
pos = 1;
end
% define search size
search = stepss(pos);
minR = r-floor(search/2);
if minR<1
minR = 1;
end
minC = (k-floor(search/2));
if minC<1
minC = 1;
end
maxR = r+floor(search/2);
if maxR>size(Gnew,1)
maxR = size(Gnew,1);
end
maxC = k+floor(search/2);
if maxC>size(Gnew,2)
maxC = size(Gnew,2);
end
FIL = Gnew(minR:maxR,minC:maxC);
if Gnew(r,k)==max(FIL(:)) && Gnew(r,k)~=0
Max(r,k)=1;
Index(r,k)=index;
index=index+1;
end
end
Ntrees=max(Index(:));
peak = Max';
[x,y] = find(peak);
idx = [x,y];
%imshow(Max) %Max is the raster of the local maxima