冈萨雷斯(bound2im、frdescp、boundaries)
%bound2im源程序
function B=bound2im(b,M,N,x0,y0)
% BOUND2IM Converts a boundary to an image.
% B = bound2im(b) converts b, an np-by-2 or 2-by-np array
% representing the integer coordinates of a boundary, into a binary
% image with 1s in the locations defined by the coordinates in b
% and 0s elsewhere.
%
% B = bound2im(b, M, N) places the boundary approximately centered
% in an M-by-N image. If any part of the boundary is outside the
% M-by-N rectangle, an error is issued.
%
% B = bound2im(b, M, N, X0, Y0) places the boundary in an image of
% size M-by-N, with the topmost boundary point located at X0 and
% the leftmost point located at Y0. If the shifted boundary is
% outside the M-by-N rectangle, an error is issued. XO and X0 must
% be positive integers.
[np,nc]=size(b);
if np<nc
b=b';%To convert to size np-by-2. [np,nc]=size(b); end %Make sure the coordinates are integers. x=round(b(:,1)); y=round(b(:,2)); %Set up the default size parameters. x=x-min(x)+1; y=y-min(y)+1; B=false(max(x),max(y)); C=max(x)-min(x)+1; D=max(y)-min(y)+1; if nargin==1 %Use the preceding defualt valuaes. elseif nargin==3 if C>M | D>N error('The boundary is outsize the M-by-N region.') end %The image size will be M-by-N.Set up the parameters for this. B=false(M,N); %Distribute extra rows appox. even between top and botton. NR=round((M-C)/2); NC=round((N-D)/2);%The same for columns. x=x+NR;%Offset the boundary to new position. y=y+NC; elseif nargin==5 if x0<0 | y0<0 error('x0 and y0 must be positive integers.') end x=x+round(x0)-1; y=y+round(y0)-1; C=C+x0-1; D=D+y0-1; if C>M | D>N error('The shifted boundary is outside the M-by-N region.') end B=false(M,N); else error('Incorrect number of inputs.')
end
B(sub2ind(size(B),x,y))=true;
%frdescp源程序
function z = frdescp(s)
%FRDESCP Computes Fourier descriptors.
% Z = FRDESCP(S) computes the Fourier descriptors of S, which is an
% np-by-2 sequence of image coordinates describing a boundary.
%
% Due to symmetry considerations when working with inverse Fourier
% descriptors based on fewer than np terms, the number of points in
% S when computing the descriptors must be even. If the number of
% points is odd, FRDESCP duplicates the end point and adds it at
% the end of the sequence. If a different treatment is desired, the
% sequence must be processed externally so that it has an even
% number of points.
%
% See function IFRDESCP for computing the inverse descriptors.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.4 $ $Date: 2003/10/26 23:13:28 $
% Preliminaries
[np, nc] = size(s);
if nc ~= 2
error('S must be of size np-by-2.');
end
if np/2 ~= round(np/2);
s(end + 1, :) = s(end, :);
np = np + 1;
end
% Create an alternating sequence of 1s and -1s for use in centering
% the transform.
x = 0:(np - 1);
m = ((-1) .^ x)';
% Multiply the input sequence by alternating 1s and -1s to
% center the transform.
s(:, 1) = m .* s(:, 1);
s(:, 2) = m .* s(:, 2);
% Convert coordinates to complex numbers.
s = s(:, 1) + i*s(:, 2);
% Compute the descriptors.
z = fft(s);
%boundaries源程序
function B=boundaries(BW,conn,dir)
%BOUNDARIES Trace object boundaries.
%B=BOUNDARIES(BW) traces the exterior boundaries of objects in the binary
%image BW.B is a p_by_1 cell array,where p is the number of objects in the
%image.Each cell contains a Q_by_2 matrix,each row of which contains the
%row and column coordinates of a boundary pixel.Q is the number of boundary
%pixels for the corresponding object.Object boundaries are traced in the
%clockwise direction.
%
%B=BOUNDARIES(BW,CONN) specifies the connectivity to use when tracing
%boundaries.CONN may be either 8 or 4.The default value for CONN is 8.
%
%B=BOUNDARIES(BW,CONN,DIR) specifies the direction used for tracing
%boundaries.DIR should be either 'cw'(trace boundaries clockwise) or
%'ccw'(trace boundaries counterclockwise).If DIR is omitted BOUNDARIES
%traces in the clockwise direction.
if nargin<3 %nargin定义在用户自定义函数体内,nargin返回
%用来调用函数的变量的个数。
dir='cw';
end
if nargin<2
conn=8;
end
L=bwlabel(BW,conn); %返回值是矩阵L,大小同BW,包含了BW中的连通部分的标记
%The number of objects is the maximum value of L.Initialize the cell
%array(元包数组)B so that each cell initially contains a 0_by_2 matrix.
numObjects=max(L(:));%寻找L中所作标记中的最大值,这个最大值实际上就对应着L中
% 包含的最多的连通部分的数目。
if numObjects>0
B={
zeros(0,2)};%元包数组中仅包含一个元素。
B=repmat(B,numObjects,1);%将B进行numObjects*1个复制构成新的B。
else
B={
};
end
%Pad label matrix with zeros.This lets us write the boundary_following loop
%without worrying about going off the edge of the image.
Lp=padarray(L,[1 1],0,'both');
%Compute the linear indexing offsets to take us from a pixel to its
%neighbors.
M=size(Lp,1);%SIZE(X,1) returns the number of rows.
if conn==8
%Order is N NE E SE S SW W NW.
offsets=[-1,M-1,M,M+1,1,-M+1,-M,-M-1];
else
%Order is N E S W.
offsets=[-1,M,1,-M];
end
%next_search_direction_lut is a lookup table.Given the direction from pixel
%k to pixel k+1,what is the direction to start with when examining the
%neighborhood of pixel k+1?
if conn==8
next_search_direction_lut=[8 8 2 2 4 4 6 6];
else
next_search_direction_lut=[4 1 2 3];
end
%next_direction_lut is a lookup table.Given that we just looked at neighbor
%in a given direction,which neighbor do we look at next?
if conn==8
next_direction_lut=[2 3 4 5 6 7 8 1];
else
next_direction_lut=[2 3 4 1];
end
%Values used for marking the starting and boundary pixels.
START=-1;
BOUNDARY=-2;
%Initialize scratch space in which to record the boundary pixels as well as
%follow the boundary.
scratch=zeros(100,1);
%Find candidate starting locations for boundaries.
[rr,cc]=find((Lp(2:end-1,:)>0)&(Lp(1:end-2,:)==0));
rr=rr+1;
for k=1:length(rr)
r=rr(k);
c=cc(k);
if (Lp(r,c)>0)&(Lp(r-1,c)==0)&isempty(B{
Lp(r,c)})
%We've found the start of the next boundary.Compute its linear %offset,record which boundary it is,mark it,and initialize the %counter for the number of boundary pixels. idx=(c-1)*size(Lp,1)+r; which=Lp(idx); scratch(1)=idx; Lp(idx)=START; numpixels=1; currentpixel=idx; initial_departure_direction=[]; done=0; next_search_direction=2; while ~done %Find the next boundary pixel. direction=next_search_direction; found_next_pixel=0; for k=1:length(offsets) neighbor=currentpixel+offsets(direction); if Lp(neighbor)~=0 %Found the next boundary pixel. if (Lp(currentpixel)==START)&... isempty(initial_departure_direction) %We are making the initial departure from the starting %pixel. initial_departure_direction=direction; elseif (Lp(currentpixel)==START)&... (initial_departure_direction==direction) % We are about to retrace our path. %That means we're done.
done=1;
found_next_pixel=1;
break;
end
%Take the next step along the boundary.
next_search_direction=...
next_search_direction_lut(direction);
found_next_pixel=1;
numpixels=numpixels+1;
if numpixels>size(scratch,1)
%Double the scratch space.
scratch(2*size(scratch,1))=0;
end
scratch(numpixels)=neighbor;
if Lp(neighbor)~=START
Lp(neighbor)=BOUNDARY;
end
currentpixel=neighbor;
break;
end
direction=next_direction_lut(direction);
end
if ~found_next_pixel
%If there is no next neighbor,the object must just have a
%single pixel.
numpixels=2;
scratch(2)=scratch(1);
done=1;
end
end
%Convert linear indices to row_column coordinates and save in the
%output cell array.
[row,col]=ind2sub(size(Lp),scratch(1:numpixels));
B{
which}=[row-1,col-1];
end
end
if strcmp(dir,'ccw')
for k=1:length(B)
B{
k}=B{
k}(end:-1:1,:);
end
end