-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmag.m
35 lines (33 loc) · 1.1 KB
/
minmag.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
function z = minmag(c)
%MINMAG Finds the integer of minimum magnitude in a chain code.
% Z = MINMAG(C) finds the integer of minimum magnitude in a given
% 4- or 8-connected Freeman chain code, C. The code is assumed to
% be a 1-by-np array.
% The integer of minimum magnitude starts with min(c), but there
% may be more than one such value. Find them all,
I = find(c == min(c));
% and shift each one left so that it starts with min(c).
J = 0;
A = zeros(length(I), length(c));
for k = I;
J = J + 1;
A(J, :) = circshift(c,[0 -(k-1)]);
end
% Matrix A contains all the possible candidates for the integer of
% minimum magnitude. Starting with the 2nd column, succesively find
% the minima in each column of A. The number of candidates decreases
% as the seach moves to the right on A. This is reflected in the
% elements of J. When length(J)=1, one candidate remains. This is
% the integer of minimum magnitude.
[M, N] = size(A);
J = (1:M)';
for k = 2:N
D(1:M, 1) = Inf;
D(J, 1) = A(J, k);
amin = min(A(J, k));
J = find(D(:, 1) == amin);
if length(J)==1
z = A(J, :);
return
end
end