⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake_demo.mht

📁 Tracking a moving object through several frames, provided changes from frame to frame are on the ord
💻 MHT
📖 第 1 页 / 共 2 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: 
Date: Wed, 28 Mar 2007 16:24:00 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.cc.gatech.edu/classes/cs7322_97_spring/participants/Khramets/midterm/snake_demo.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180

=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META content=3D"MSHTML 6.00.2900.2523" name=3DGENERATOR></HEAD>
<BODY><PRE>function snake_demo(action,arg)
% SNAKES
% By Chris Bregler and Malcolm Slaney, Interval Technical Report IRC =
1995-017
% Copyright (c) 1995 Interval Research Corporation.
%
% Usage
%
% snake_demo ([image_data])
%
% This function provides an interactive GUI for showing how the
% snake m-file works.  For more information about the GUI press the help
% button.  For more information about the snake function itself type
% help snake

% SNAKES - A MatLab MEX file to demonstrate snake contour-following.
% This Software was developed by Chris Bregler and Malcolm Slaney of
% Interval Research Corporation.
% Copyright (c) 1995 Interval Research Corporation.
%
% This is experimental software and is being provided to Licensee
% 'AS IS.'  Although the software has been tested on a PowerMac
% 8100 running version 4.2c of MatLab with MEX support and on an
% SGI running version 4.2c, Interval makes no warranties relating
% to the software's performance on these or any other platforms.
%
% Disclaimer
% THIS SOFTWARE IS BEING PROVIDED TO YOU 'AS IS.'  INTERVAL MAKES
% NO EXPRESS, IMPLIED OR STATUTORY WARRANTY OF ANY KIND FOR THE
% SOFTWARE INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF
% PERFORMANCE, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
% IN NO EVENT WILL INTERVAL BE LIABLE TO LICENSEE OR ANY THIRD
% PARTY FOR ANY DAMAGES, INCLUDING LOST PROFITS OR OTHER INCIDENTAL
% OR CONSEQUENTIAL DAMAGES, EVEN IF INTERVAL HAS BEEN ADVISED OF
% THE POSSIBLITY THEREOF.
%
%   This software program is owned by Interval Research
% Corporation, but may be used, reproduced, modified and
% distributed by Licensee.  Licensee agrees that any copies of the
% software program will contain the same proprietary notices and
% warranty disclaimers which appear in this software program.

global snakeImage snakePoints snakeGradient snakeGlobals theColorMap;

if length(snakeGlobals) ~=3D 10,
  snakeGlobals =3D zeros(10);
end

loadGlobals =3D ['movingPoint =3D snakeGlobals(1);' ...
                           'displayHndl =3D snakeGlobals(2);' ...
                           'useGradHndl =3D snakeGlobals(3);' ...
                           'betaHndl =3D snakeGlobals(4);' ...
                           'sigmaHndl =3D snakeGlobals(5);' ...
                           'XdeltaHndl =3D snakeGlobals(6);' ...
                           'YdeltaHndl =3D snakeGlobals(7);' ...
                           'XresolutionHndl =3D snakeGlobals(8);' ...
                           'YresolutionHndl =3D snakeGlobals(9);' ...
                           'snakeGradientSigma =3D snakeGlobals(10);'];

saveGlobals =3D ['snakeGlobals =3D [movingPoint, displayHndl,' ...
                           'useGradHndl, betaHndl, sigmaHndl, =
XdeltaHndl, ' ...
                           'YdeltaHndl, XresolutionHndl, =
YresolutionHndl, ' ...
                           'snakeGradientSigma];'];

eval(loadGlobals);

if nargin &lt; 1
        action =3D 'init';
        o=3Dones(64,48);
        x=3Dcumsum(o')';
        y=3Dcumsum(o);
        snakeImage =3D =
100*exp(-(y-50+20*exp(-(x-size(x,2)/2).^2/32/32)).^2/100);
%
%
        theColorMap =3D 1 - gray;
%
%
end

if ~isstr(action)
        snakeImage =3D action;
        action =3D 'init';
end

if strcmp(action, 'help')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The HELP command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ttlStr=3D'Snakes Help';
    hlpStr=3D ...
    [' Welcome to SNAKES.  This demonstration allows you to experiment   =
   '
     ' with the image processing technique known as snakes.  Snakes are  =
   '
     ' used to interactively align splines to contours or other features =
   '
     ' in images.                                                        =
   '
     '                                                                   =
   '
     ' By Chris Bregler and Malcolm Slaney                               =
   '
     ' Interval Technical Report IRC 1995-017                            =
   '
     ' Copyright (c) 1995 Interval Research Corporation.                 =
   '
     '                                                                   =
   '
     ' To select Contour Points:  Use the mouse button to place points =
in   '
     '   the image.  For example put 20 points along the upper half of =
the  '
     '   head. Press shift and the mouse button to move a point to a new =
   '
     '   location. Only the points are considered in the algorithm.      =
   '
     '                                                                   =
   '
     ' Each time you press the iterate button the snake will move so as =
to  '
     '   minimize the curvature and maximize the value of the data under =
   '
     '   the contour.                                                    =
   '
     '                                                                   =
   '
     ' A detailed description of the snake algorithm can be found in:    =
   '
     ' "Using Dynamic Programming for Solving Variational Problems in    =
   '
     ' Vision" by Amir A. Amini, Terry E. Weymouth, and Ramesh C. Jain,  =
   '
     ' IEEE Transactions on Pattern Analysis and Machine Intelligence,   =
   '
     ' Vol. 12, No. 9, September 1990, pp 855-867.                       =
   '];
    old_fig=3Dwatchon;
    pos =3D get(0,'DefaultFigurePosition');
    help_fig=3Dfigure('Name','Snakes Help =
Window','NumberTitle','off',...
                    'Position',pos, 'Colormap',[]);
    uicontrol('Style','edit', 'units','normalized', ...
              'Position',[0.05 0.05 0.9 0.9],...
              'HorizontalAlignment','Left',...
              'BackgroundColor',[0.5 0.5 0.5], ...
              'ForegroundColor',[1 1 1], ...
              'Max',30,'String',hlpStr);
   watchoff(old_fig);
elseif strcmp(action, 'helpparm')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The PARAMETER HELP command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ttlStr=3D'Snakes Parameters';
    hlpStr=3D ...
    [' X / Y Resolution:  Pixel step size for the snake alignment =
algorithm.'
     '   Increasing this value speeds up the computation.                =
   '
     ' X / Y Range:  Maximum number of pixels that each point will be =
moved '
     '   per iteration.                                                  =
   '
     ' Beta:  "Smoothness" parameter.  Increasing beta results in =
smoother  '
     '   contours.  Smaller values allow the snake to track more =
locally.   '
     ' Sigma:  Spread (in pixels) of the gaussian image gradient =
operator.  '
     ' Fit to Gradient:  Snakes should be aligned to the image gradient. =
   '
     ' Iterate:  Press this button to run one iteration of the snake =
code.  '
     '                                                                   =
   '
     ' Note: The run time of this algorithm is proportional to the =
product  '
     ' of the number of pixel searched in the x direction, the number of =
   '
     ' pixel searched in the y direction, and the number of snake =
points.   '
     ' The number of search locations is equal to =
length(-range:resol:range)'
     '                                                                   =
   '
     ' The optimal value of beta is proportional to the values in the =
image.'];
    old_fig=3Dwatchon;
    pos =3D get(0,'DefaultFigurePosition');
    help_fig=3Dfigure('Name','Snakes Parameter Help =
Window','NumberTitle', ...
                                        'off','Position',pos, =
'Colormap',[]);
    uicontrol('Style','edit', 'units','normalized', ...
              'Position',[0.05 0.05 0.9 0.9],...
              'HorizontalAlignment','Left',...
              'BackgroundColor',[0.5 0.5 0.5], ...
              'ForegroundColor',[1 1 1], ...
              'Max',30,'String',hlpStr);
   watchoff(old_fig);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The INIT command - clear the arrays and set up the demo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(action, 'init')
        snakePoints =3D [];
        snakeGradient =3D [];
        movingPoint =3D 0;
        clg;
        eval(saveGlobals); snake_demo('initFrame'); eval(loadGlobals);
        eval(saveGlobals); snake_demo('redraw'); eval(loadGlobals);

        set(gcf,'WindowButtonDownFcn','snake_demo ''down'';');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The clear command - Reset the point list
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(action, 'clear')
        snakePoints=3D[];
        eval(saveGlobals); snake_demo('redraw'); eval(loadGlobals);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The computegrad command - Compute the gradient of the image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(action, 'computegrad')
        sigma =3D str2num(get(sigmaHndl,'String'));
        if sigma &lt;=3D 1
                sigma =3D 1;
                set(sigmaHndl, 'String', sprintf('%g',sigma));
        end

%        if exist('snakeGradientSigma') =3D=3D 1 &amp; sigma =3D=3D =
snakeGradientSigma
%                eval(saveGlobals); return;
%        elseif
            snakeGradientSigma =3D sigma;
%        end
        % Calculate a normal distribution with a standard deviation of =
sigma.
        % Cut off when tails reach 1% of the maximum value.
        i =3D 1;
        maxval =3D 1/(sqrt(2*pi)*snakeGradientSigma);
        gi =3D maxval;
        g =3D [gi];
        while gi &gt;=3D 0.01*maxval
                gi =3D maxval * exp(-0.5*i^2/snakeGradientSigma^2);
                g =3D [gi g gi];
                i =3D i + 1;
        end

        % Calculate the derivative of a normal distribution with a =
standard
        % deviation of sigma.
        % Cut off when tails reach 1% of the maximum value.
        i =3D 1;
        maxval =3D 0;
        dgi =3D [maxval];
        dg =3D [dgi];
        while dgi &gt;=3D 0.01*maxval
                dgi =3D i / (sqrt(2*pi) * snakeGradientSigma^3) * ...
                                exp(-0.5*i^2/snakeGradientSigma^2);
                dg =3D [dgi dg -dgi];
                i =3D i + 1;
                if dgi &gt; maxval
                        maxval =3D dgi;
                end
        end

        % Calculate the derivative of a Gaussian in x convolved with =
snakeImage
        sub =3D =
1+floor(length(dg)/2):(1+size(snakeImage,2)+length(dg)/2-1);
        fi1 =3D zeros(size(snakeImage));
        for i=3D1:size(snakeImage,1)
                new =3D conv(snakeImage(i,:),dg);
                fi1(i,:) =3D new(sub);
        end

        % Smooth the resulting derivative in y
        fi2 =3D zeros(size(fi1));
        sub =3D 1+floor(length(g)/2):(1+size(fi1,1)+length(g)/2-1);
        for i=3D1:size(fi1,2)
                new =3D conv(fi1(:,i)',g');
                fi2(:,i) =3D new(sub)';
        end

        % Calculate the derivative of a Gaussian in y convolved with =
snakeImage
        fi3 =3D zeros(size(snakeImage));
        sub =3D =
1+floor(length(dg)/2):(1+size(snakeImage,1)+length(dg)/2-1);
        for i=3D1:size(snakeImage,2)
                new =3D conv(snakeImage(:,i)',dg');
                fi3(:,i) =3D new(sub)';
        end

        % Smooth the resulting derivative in x
        sub =3D =
1+floor(length(g)/2):(1+size(snakeImage,2)+length(g)/2-1);
        fi4 =3D zeros(size(fi3));
        for i=3D1:size(fi3,1)
                new =3D conv(fi3(i,:),g);
                fi4(i,:) =3D new(sub);
        end

        if 0
                subplot(2,2,1);
                imagesc(fi1);
                title('fi1 =3D dGx * snakeImage');
                subplot(2,2,2);
                imagesc(fi2);
                title('fi2 =3D Gy * fi1');
                subplot(2,2,3);
                imagesc(fi3);
                title('fi3 =3D dGy * snakeImage');
                subplot(2,2,4);
                imagesc(fi4);
                title('fi4 =3D Gx * fi3');
        end

        snakeGradient =3D sqrt(fi2.^2+fi4.^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The (mouse) down command - Add a point to the path, or dispatch to =
move
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(action, 'down')
        if ~strcmp(get(gcf,'SelectionType'),'normal')
                eval(saveGlobals); snake_demo('movePoint'); =
eval(loadGlobals);
                =
set(gcf,'WindowButtonMotionFcn','snake_demo(''movePoint'');');
                set(gcf,'WindowButtonUpFcn','snake_demo(''up'');');
                eval(saveGlobals); return;
        end
        currPt =3D get(gca,'CurrentPoint');
    currPt =3D round(currPt(1,1:2));
        if (currPt(1))&gt;-1&amp;(currPt(1)&lt;size(snakeImage,2))&amp; =
...
                        =
(currPt(2)&gt;0)&amp;(currPt(2)&lt;size(snakeImage,1)),
            snakePoints =3D [snakePoints;currPt];
        else
%           set(txtHndl,'String',' Please click inside the axis =
square');
                eval(saveGlobals); return;
        end
        line(currPt(1),currPt(2), ...
                        'LineStyle','.', ...
                        'Color','r', ...
                        'MarkerSize', 25, ...
                        'EraseMode','none');

        if size(snakePoints,1) &gt; 1
                numPts =3D size(snakePoints,1);
                line(snakePoints([numPts-1 numPts],1),...
                     snakePoints([numPts-1 numPts],2), ...
                     'Color','b', ...
                     'EraseMode','none');
        end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The initFrame command - Draw the GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(action, 'initFrame')
    axes( ...
        'Units','normalized', ...
        'Position',[0.05 0.05 0.65 0.90], ...
        'XTick',[],'YTick',[], ...
        'Box','on');

    =
%=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -