0001 function positions = GetPositions(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 global DATA SETTINGS;
0047 if isempty(DATA),
0048 error('No session defined (did you forget to call SetCurrentSession? Type ''help <a href="matlab:help Data">Data</a>'' for details).');
0049 end
0050
0051
0052 mode = '';
0053 coordinates = '';
0054 pixel = [];
0055 distances = [];
0056 discard = 'partial';
0057
0058 if mod(length(varargin),2) ~= 0,
0059 error('Incorrect number of parameters (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0060 end
0061
0062
0063 for i = 1:2:length(varargin),
0064 if ~ischar(varargin{i}),
0065 error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).']);
0066 end
0067 switch(lower(varargin{i})),
0068 case 'mode',
0069 mode = lower(varargin{i+1});
0070 if ~isastring(mode,'clean','all'),
0071 error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0072 end
0073 case 'coordinates',
0074 coordinates = lower(varargin{i+1});
0075 if ~isastring(coordinates,'video','normalized','real'),
0076 error('Incorrect value for property ''coordinates'' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0077 end
0078 case 'pixel',
0079 pixel = varargin{i+1};
0080 if ~isdscalar(pixel,'>0'),
0081 error('Incorrect value for property ''pixel'' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0082 end
0083 case 'discard',
0084 discard = lower(varargin{i+1});
0085 if ~isastring(discard,'partial','none'),
0086 error('Incorrect value for property ''discard'' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0087 end
0088 case 'distances',
0089 distances = lower(varargin{i+1});
0090 if ~isdvector(distances,'#2','>=0'),
0091 error('Incorrect value for property ''distances'' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).');
0092 end
0093 otherwise,
0094 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).']);
0095 end
0096 end
0097
0098
0099 mode = GetCustomDefaults(mode,'mode','clean');
0100 coordinates = GetCustomDefaults(coordinates,'coordinates','normalized');
0101 pixel = GetCustomDefaults(pixel,'pixel',[]);
0102 distances = GetCustomDefaults(distances,'distances',[0 Inf]);
0103
0104 minDistance = distances(1);
0105 maxDistance = distances(2);
0106
0107 if strcmp(coordinates,'real') & isempty(pixel),
0108 error(['Missing ''pixel'' property-value pair (type ''help <a href="matlab:help GetPositions">GetPositions</a>'' for details).']);
0109 end
0110
0111 positions = DATA.positions;
0112 if isempty(positions), return; end
0113
0114
0115 if strcmp(mode,'clean'),
0116 if size(positions,2) == 5,
0117
0118 distance = sqrt((positions(:,4)-positions(:,2)).^2+(positions(:,5)-positions(:,3)).^2);
0119 selected = ( ...
0120 distance >= minDistance ...
0121 & distance <= maxDistance ...
0122 );
0123 positions = positions(selected,:);
0124 end
0125 end
0126
0127
0128 if strcmp(discard,'none'),
0129 undetected = all(positions(:,2:end) == -1,2);
0130 partiallyDetected = positions == -1;
0131 positions(partiallyDetected) = NaN;
0132 else
0133 undetected = any(positions(:,2:end) == -1,2);
0134
0135 end
0136
0137 good = ~undetected;
0138
0139
0140 m = min(positions(good,2:end));
0141 M = max(positions(good,2:end));
0142 if any(m<0)||any(M(1:2:end)>DATA.maxX|M(2:2:end)>DATA.maxY),
0143 warning(['Position data should stay within [0,' num2str(DATA.maxX) ']x[0,' num2str(DATA.maxY) ']. The data will now be clipped accordingly.']);
0144 positions(good,2) = Clip(positions(good,2),0,DATA.maxX);
0145 positions(good,3) = Clip(positions(good,3),0,DATA.maxY);
0146 end
0147
0148 if strcmp(coordinates,'normalized'),
0149
0150 maxima = [DATA.maxX DATA.maxY];
0151 positions(good,2:end) = positions(good,2:end) ./ repmat(maxima,sum(good),(size(positions,2)-1)/2);
0152 elseif strcmp(coordinates,'real'),
0153
0154 positions(good,2:end) = positions(good,2:end) * pixel;
0155 end
0156
0157
0158 if strcmp(mode,'clean'),
0159 positions(undetected,:) = [];
0160 end