minperpoly.m ( File view )

  • By yueqiaohuayuan 2014-11-17
  • View(s):3508
  • Download(s):7
  • Point(s): 3
			function [x, y] = minperpoly(B, cellsize)
%MINPERPOLY Computes the minimum perimeter polygon.
%   [X, Y] = MINPERPOLY(F, CELLSIZE) computes the vertices in [X, Y]
%   of the minimum perimeter polygon of a single binary region or
%   boundary in image B. The procedure is based on Slansky's
%   shrinking rubber band approach. Parameter CELLSIZE determines the
%   size of the square cells that enclose the boundary of the region
%   in B. CELLSIZE must be a nonzero integer greater than 1.
%   The algorithm is applicable only to boundaries that are not
%   self-intersecting and that do not have one-pixel-thick
%   protrusions. 

%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004
%   $Revision: 1.6 $  $Date: 2003/11/21 14:41:39 $

if cellsize <= 1
   error('CELLSIZE must be an integer > 1.'); 

% Fill B in case the input was provided as a boundary. Later 
% the boundary will be extracted with 4-connectivity, which 
% is required by the algorithm. The use of bwperim assures 
% that 4-connectivity is preserved at this point.               
B = imfill(B, 'holes');
B = bwperim(B);               
[M, N] = size(B);

% Increase image size so that the image is of size K-by-K
% with (a) K >= max(M,N) and (b)  K/cellsize = a power of 2.
K = nextpow2(max(M, N)/cellsize);
K = (2^K)*cellsize;
% Increase image size to nearest integer power of 2, by 
% appending zeros to the end of the image. This will allow 
% quadtree  decompositions as small as cells of size 2-by-2, 
% which is the smallest allowed value of cellsize.
M = K - M;
N = K - N;
B = padarray(B, [M N], 'post'); % f is now of size K-by-K

% Quadtree decomposition.
Q = qtdecomp(B, 0, cellsize); 

% Get all the subimages of size cellsize-by-cellsize.
[vals, r, c] = qtgetblk(B, Q, cellsize);

% Get all the subimages that contain at least one black 
% pixel. These are the cells of the wall enclosing the boundary.
I = find(sum(sum(vals(:, :, :)) >= 1));
x = r(I);
y = c(I);

% [x', y'] is a length(I)-by-2 array.  Each member of this array is
% the left, top corner of a black cell of size cellsize-by-cellsize.
% Fill the cells with black to form a closed border of black cells
% around interior points. These cells are the cellular complex.
for k = 1:length(I)
   B(x(k):x(k) + cellsize-1, y(k):y(k) + cellsize-1) = 1;
BF = imfill(B, 'holes');

% Extract the points interior to the black border. This is the region
% of interest around which the MPP will be found. 
B = BF & (~B);
% Extract the 4-connected boundary.
B = boundaries(B, 4, 'cw');
% Find the largest one in case of parasitic regions.
J = cellfun('length', B);
I = find(J == max(J));
B = B{

% Function boundaries outputs the last coordinate pair equal to the
% first.  Delete it. 
B = B(1:end-1,:);
% Obtain the xy coordinates of the boundary.
x = B(:, 1);
y = B(:, 2);

% Find the smallest x-coordinate and corresponding
% smallest y-coordinate.  
cx = find(x == min(x));
cy = find(y == min(y(cx)));

% The cell with top leftmost corner at (x1, y1) below is the first
% point considered by the algorithm.  The remaining points are
% visited in the clockwise direction starting at (x1, y1). 
x1 = x(cx(1));
y1 = y(cy(1));

% Scroll data so that the first point is (x1, y1).
I = find(x == x1 & y == y1);
x = circshift(x, [-(I - 1), 0]);
y = circshift(y, [-(I - 1), 0]);

% The same shift applies to B.
B = circshift(B, [-(I - 1), 0]);

% Get the Freeman chain code.  The first row of B is the required
% starting point. The first element of the code is the transition
% between the 1st and 2nd element of B, the second element of 
% the code is the transition between the 2nd and 3rd elements of B,
% and so on.  The last element of the code is the transition between
% the last and 1st elements of B. The elements of B form a cw
% sequence (see above), so we use 'same' for the direction in
% function fchcode. 
code = fchcode(B, 4, 'same'); 
code = code.fcc;

% Follow the code sequence to extract the Black Dots, BD, (convex
% corners) and White Dots, WD, (concave corners). The transitions are
% as follows: 0-to-1=WD; 0-to-3=BD; 1-to-0=BD; 1-to-2=WD; 2-to-1=BD;
% 2-to-3=WD; 3-to-0=WD; 3-to-2=dot.  The formula t = 2*first - second
% gives the following unique values for these transitions: -1, -3, 2,
% 0, 3, 1, 6, 4.  These are applicable to travel in the cw direction. 
% The WD's are displaced one-half a diagonal from the BD's to form
% the half-cell expansion required in the algorithm. 

% Vertices will be computed as array "vertices" of dimension nv-by-3,
% where nv is the number of vertices. The first two elements of any
% row of array vertices are the (x,y) coordinates 
(Please download the complete source code to view)
Expand> <Close

Want complete source code? Download it here

Point(s): 3

0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
0_Read_Me_First.txt5.00 kB2012-02-29|09:51
Contents.m5.98 kB2012-02-29|09:51
Readme.m5.30 kB2012-02-29|09:51
adpmedian.m1.54 kB2012-02-29|09:51
average.m651.00 B2012-02-29|09:51
bayesgauss.m2.96 kB2012-02-29|09:51
bound2eight.m2.99 kB2012-02-29|09:51
bound2four.m2.42 kB2012-02-29|09:51
bound2im.m2.17 kB2012-02-29|09:51
boundaries.m5.64 kB2012-02-29|09:51
bsubsamp.m3.87 kB2012-02-29|09:51
changeclass.m592.00 B2012-02-29|09:51
colorgrad.m2.30 kB2012-02-29|09:51
colorseg.m2.37 kB2012-02-29|09:51
compare.m1.15 kB2012-02-29|09:51
connectpoly.m1.02 kB2012-02-29|09:51
conwaylaws.m799.00 B2012-02-29|09:51
covmatrix.m977.00 B2012-02-29|09:51
dftcorr.m687.00 B2012-02-29|09:51
dftfilt.m882.00 B2012-02-29|09:51
dftuv.m736.00 B2012-02-29|09:51
diameter.m6.47 kB2012-02-29|09:51
endpoints.m874.00 B2012-02-29|09:51
entropy.m1.00 kB2012-02-29|09:51
fchcode.m8.55 kB2012-02-29|09:51
frdescp.m1.42 kB2012-02-29|09:51
fwtcompare.m992.00 B2012-02-29|09:51
gmean.m447.00 B2012-02-29|09:51
gscale.m2.04 kB2012-02-29|09:51
histroi.m920.00 B2012-02-29|09:51
hough.m2.64 kB2012-02-29|09:51
houghlines.m2.82 kB2012-02-29|09:51
houghpeaks.m2.65 kB2012-02-29|09:51
houghpixels.m1.02 kB2012-02-29|09:51
hpfilter.m1.23 kB2012-02-29|09:51
hsi2rgb.m1.94 kB2012-02-29|09:51
huff2mat.m2.46 kB2012-02-29|09:51
huffman.m2.82 kB2012-02-29|09:51
ice.fig45.71 kB2012-02-29|09:51
ice.m26.30 kB2012-02-29|09:51
ice_stand_alone.m46.55 kB2012-02-29|09:51
ifrdescp.m1.38 kB2012-02-29|09:51
ifwtcompare.m1.12 kB2012-02-29|09:51
im2jpeg.m2.90 kB2012-02-29|09:51
im2jpeg2k.m3.97 kB2012-02-29|09:51
imnoise2.m4.20 kB2012-02-29|09:51
imnoise3.m2.12 kB2012-02-29|09:51
improd.m791.00 B2012-02-29|09:51
imratio.m1.30 kB2012-02-29|09:51
imstack2vectors.m1.83 kB2012-02-29|09:51
intline.m1.20 kB2012-02-29|09:51
intrans.m3.25 kB2012-02-29|09:51
invmoments.m3.24 kB2012-02-29|09:51
jpeg2im.m2.41 kB2012-02-29|09:51
jpeg2k2im.m1.90 kB2012-02-29|09:51
lpc2mat.m1.42 kB2012-02-29|09:51
lpfilter.m1.44 kB2012-02-29|09:51
mahalanobis.m1.97 kB2012-02-29|09:51
manualhist.m1.75 kB2012-02-29|09:51
mat2huff.m2.75 kB2012-02-29|09:51
mat2lpc.m1.28 kB2012-02-29|09:51
minperpoly.m7.15 kB2012-02-29|09:51
paddedsize.m1.29 kB2012-02-29|09:51
pixeldup.m950.00 B2012-02-29|09:51
polyangles.m6.14 kB2012-02-29|09:51
princomp.m2.88 kB2012-02-29|09:51
quantize.m1.33 kB2012-02-29|09:51
randvertex.m1.15 kB2012-02-29|09:51
regiongrow.m1.66 kB2012-02-29|09:51
rgb2hsi.m1.46 kB2012-02-29|09:51
rgbcube.m1.98 kB2012-02-29|09:51
signature.m4.45 kB2012-02-29|09:51
specxture.m2.57 kB2012-02-29|09:51
spfilt.m3.78 kB2012-02-29|09:51
splitmerge.m4.13 kB2012-02-29|09:51
statmoments.m1.72 kB2012-02-29|09:51
statxture.m1.55 kB2012-02-29|09:51
strsimilarity.m1.78 kB2012-02-29|09:51
subim.m713.00 B2012-02-29|09:51
twodsin.m1.04 kB2012-02-29|09:51
twomodegauss.m1.16 kB2012-02-29|09:51
unravel.dll7.00 kB2012-02-29|09:51
unravel.m967.00 B2012-02-29|09:51
unravel.mexaxp24.00 kB2012-02-29|09:51
unravel.mexglx18.97 kB2012-02-29|09:51
unravel.mexhpux12.00 kB2012-02-29|09:51
unravel.mexmac8.68 kB2012-02-29|09:51
unravel.mexrs62.71 kB2012-02-29|09:51
unravel.mexsg24.44 kB2012-02-29|09:51
unravel.mexsol8.54 kB2012-02-29|09:51
vistformfwd.m1.66 kB2012-02-29|09:51
wave2gray.m4.67 kB2012-02-29|09:51
waveback.m4.52 kB2012-02-29|09:51
wavecopy.m940.00 B2012-02-29|09:51
wavecut.m1.06 kB2012-02-29|09:51
wavefast.m4.14 kB2012-02-29|09:51
wavefilter.m4.77 kB2012-02-29|09:51
wavepaste.m1.04 kB2012-02-29|09:51
wavework.m3.45 kB2012-02-29|09:51
wavezero.m696.00 B2012-02-29|09:51
x2majoraxis.m3.59 kB2012-02-29|09:51
Sponsored links

minperpoly.m (160.12 kB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point


Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com


CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D