📄 activecontour.m
字号:
function phi = activecontour( u0, center, radius, isinside, d_it, m_it, m_name )
% ACTIVECONTOUR Segment an image using active contours
% ACTIVECONTOUR( u0, center, radius, isinside, d_it, m_it, m_name )
% segments the image u0 with initial segmented region as a
% circle with argument center and radius. The current segmented
% image is displayed every d_it iterations and written to disk
% every m_iterations with name m_name*.png.
% constants
ITERATIONS = 25000;
delta_t = 0.1;
lambda1 = 1;
lambda2 = 1;
nu = 0;
h = 1; h_sq = h^2;
epsilon = 1;
mu = 0.01 * 255^2;
% initialize phi to signed distance function from circle
phi = initphi( size( u0 ), center, radius, isinside );
for ii = 1 : ITERATIONS;
% display current iteration
fprintf( 1, '%d\n', ii );
% display the segmented image every 'd_it' iterations
if( mod( ii - 1, d_it ) == 0 )
disp( 'Displaying Segmented Image' );
segim = createimage( u0, phi );
clf; imshow( segim );
drawnow;
end;
% write current segmented image to file every 'm_it'
% iterations
if( mod( ii - 1, m_it ) == 0 )
segim = createimage( u0, phi );
filename = strcat( m_name, sprintf( '%06d', ( ( ii - 1 )/ m_it ) + 1 ), '.png' );
imwrite( segim, filename );
end;
% compute dirac(phi) * delta_t factor
dirac_delta_t = delta_t * dirac( phi, epsilon );
% calculate inside and outside curve terms
[ inside, outside ] = calcenergy( u0, phi, epsilon );
energy_term = -nu - lambda1 .* inside + lambda2 .* outside;
% calculate central differences
dx_central = ( circshift( phi, [ 0, -1 ] ) - circshift( phi, [ 0, 1 ] ) ) / 2;
dy_central = ( circshift( phi, [ -1, 0 ] ) - circshift( phi, [ 1, 0 ] ) ) / 2;
% calculate the divergence of grad_phi/|grad_phi|
abs_grad_phi = ( sqrt( dx_central.^2 + dy_central.^2 ) + 0.00001 );
x = dx_central ./ abs_grad_phi;
y = dy_central ./ abs_grad_phi;
grad_term = ( mu / h_sq ) .* divergence( x, y );
% calculate phi^n+1
phi = phi + dirac_delta_t .* ( grad_term + energy_term );
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -