-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocmax2d.m
59 lines (50 loc) · 1.76 KB
/
locmax2d.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
function fImg=locmax2d(img,mask,keepFlat)
%LOCALMAX searches for local maxima in an image
%
% SYNOPSIS fImg=(img,mask)
%
% INPUT img image matrix
% mask [m n] defines the operator window dimensions
% keepFlat Optional input variable to choose whether to remove
% "flat" maxima or to keep them. Default is 0, to
% remove them. - KJ
%
% OUTPUT fImg map with all the local maxima (zeros elsewhere);
% the non-zero values contain the original value
% of the image at that place
%
% NOTE: convert fImg to uint8 or uint16 for optimal display!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PARAMETER CHECK
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin<2
error('Please define all parameters');
end
if nargin < 3 || isempty(keepFlat)
keepFlat = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DEFINITIONS
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make sure the mask elements are odd numbers (only then, the local max operator is
% properly defined)
indx = find(~mod(mask,2));
mask(indx) = mask(indx) + 1;
% apply a max filter
fImg = ordfilt2(img,prod(mask),ones(mask));
if keepFlat == 0 %change made by KJ
fImg2 = ordfilt2(img,prod(mask)-1,ones(mask));
fImg(fImg2==fImg)=0;
end
% take only those positions where the max filter and the original image value
% are equal -> this is a local maximum
fImg(fImg ~= img) = 0;
% set image border to zero
auxM = zeros(size(img));
auxM(fix(mask(1)/2)+1:end-fix(mask(1)/2),fix(mask(2)/2)+1:end-fix(mask(2)/2)) = ...
fImg(fix(mask(1)/2)+1:end-fix(mask(1)/2),fix(mask(2)/2)+1:end-fix(mask(2)/2));
fImg=auxM;