www.pudn.com > snippets(1).rar > extract_flowfeatures.m, change:2009-10-12,size:2729b


function G = extract_flowfeatures(I,Globals)

DISPLAY = 0;

% save typing...
numvel = Globals.flow_numvel;
numdir = Globals.flow_numdir;
numscales = length(Globals.flow_scales);

% sort out velocities
velocities = zeros(numscales,numvel);
velocity_step = zeros(numscales,1);
for u = 1:numscales
  velocity_step(u) = Globals.flow_scales(u)/2/numvel;
  for v = 1:numvel
    velocities(u,v) = v*velocity_step(u);
    if velocities(u,v)<=0
      error('Desired flow for filter is 0 - check parameters!');
    end % if
  end
end
% if desired,add velocity zero
if Globals.flow_velocityzero
  velocities = [zeros(numscales,1) velocities];
  numvel = numvel+1;
end

% get preferred bin centers
angle = 2*pi/numdir;
directions = (0:numdir)*angle;

directions3 = zeros(1,1,numdir+1);
for u = 1:numdir+1
  directions3(1,1,u) = directions(u);
end

% raw optic flow
F = cell(numscales,2);
for u = 1:numscales
  [F{u,1},F{u,2}] = flowmap(I,...
			    Globals.flow_scales(u),...
			    velocities(u,end),...
			    Globals.flow_useSSD,...
			    Globals.flow_smooth);
end

% output structure
[ht,wt,fr] = size(I);
fr = fr-1;
G = cell(numdir,numvel,numscales);

% loop through frames
for u = 1:numscales

  % auxiliary variables to exploit fast MATLAB array-handling
  dir_h = zeros(ht,wt,numdir+1);
  vel_h = zeros(ht,wt,numvel);
  for v = 1:numdir+1
    dir_h(:,:,v) = repmat(directions(v),ht,wt);
  end
  for v =1:numvel
    vel_h(:,:,v) = repmat(velocities(u,v),ht,wt);
  end

  % loop through frames
  for v = 1:fr

    % convert current flow-field from (x,y) to (direction,velocity)
    Fdir = atan2(F{u,1}(:,:,v),F{u,2}(:,:,v)+eps);
    belowzero = (Fdir<0);
    Fdir(belowzero) = Fdir(belowzero)+2*pi;
    Fvel = sqrt(F{u,1}(:,:,v).^2+F{u,2}(:,:,v).^2);

    % linearly distribute flow to nearest direction and velocity bins
    % somewhat cryptic implementation to exploit fast MATLAB array handling
    d_dir = repmat(Fdir,[1 1 numdir+1]);
    d_dir = abs(d_dir-dir_h);
    d_dir(:,:,1) = min(d_dir(:,:,1),d_dir(:,:,end));
    d_dir = max(0,angle-d_dir(:,:,1:end-1));

    d_vel = repmat(Fvel,[1 1 numvel]);
    d_vel = abs(d_vel-vel_h);
    d_vel = max(0,velocity_step(u)-d_vel);

    % combine direction and velocity weights
    denom = angle*velocity_step(u);
    for q = 1:numdir
      for r = 1:numvel

	G{q,r,u}(:,:,v) = d_dir(:,:,q).*d_vel(:,:,r)/denom;

      end % for r
    end % for q

  end % for v

  % show results
  if DISPLAY

    for v = 1:fr

      himg = [];
      ct=1;
      for r = 1:numvel
        hcol = [];
        for q = 1:numdir
          hcol = [hcol ; G{q,r,u}(:,:,v)];
        end
        himg = [himg hcol];
      end

      figure;
      imshow(himg);

    end % for v

  end % if DISPLAY

end % for u