forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindPeaks.m
65 lines (53 loc) · 1.66 KB
/
findPeaks.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
function corners = findPeaks( img, varargin )
% corners = findPeaksCorners( img [, N, 'buffer', buffer ] )
%
% Inputs:
% img - a 2D array
% N - the number of features to identify (default is 50)
% buffer - the minimum spacing between features (default is 20)
%
% Outputs:
% corners - an Nx2 array. The first/second column is the x/y coordinate
% of each feature.
%
% Written by Nicholas Dwork 2018
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
defaultN = 50;
defaultBuffer = 20;
p = inputParser;
p.addOptional( 'N', defaultN );
p.addParameter( 'buffer', defaultBuffer, @isnumeric );
p.parse( varargin{:} );
N = p.Results.N;
buffer = p.Results.buffer;
sImg = size( img );
Ix = zeros( sImg );
Ix(:,1:end-1) = img(:,2:end) - img(:,1:end-1);
IxSq = Ix .* Ix;
Iy = zeros( sImg );
Iy(1:end-1,:) = img(2:end,:) - img(1:end-1,:);
IySq = Iy .* Iy;
score = IxSq + IySq;
minScore = min( score(:) );
score(1:buffer,:) = minScore;
score(:,1:buffer) = minScore;
score(end-buffer:end,:) = minScore;
score(:,end-buffer:end) = minScore;
corners = zeros(N,2);
for img=1:N
[~,maxIndx] = max( score(:) );
[y,x] = ind2sub( sImg, maxIndx );
corners(img,1) = x;
corners(img,2) = y;
bIndx = max( y-buffer, 1 );
uIndx = min( y+buffer, sImg(1) );
LIndx = max( x-buffer, 1 );
rIndx = min( x+buffer, sImg(2) );
score(bIndx:uIndx,LIndx:rIndx) = minScore;
if max(score(:)) == minScore, break; end;
end
end