声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4168|回复: 0

[共享资源] 光流(运动估计)经典算法

[复制链接]
发表于 2006-10-24 10:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
光流(运动估计)经典算法Horn and Schunck算法MATLAB程序

  1. function [us,vs] = HSoptflow(Xrgb,n)
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % Author: Gregory Power gregory.power@wpafb.af.mil
  4. % This MATLAB code shows a Motion Estimation map created by
  5. % using a Horn and Schunck motion estimation technique on two
  6. % consecutive frames.  Input requires.
  7. %     Xrgb(h,w,d,N) where X is a frame sequence of a certain
  8. %                height(h), width (w), depth (d=3 for color),
  9. %                and number of frames (N).
  10. %     n= is the starting frame number which is less than N
  11. %     V= the output variable which is a 2D velocity array
  12. %
  13. % Sample Call: V=HSoptflow(X,3);
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. [h,w,d,N]=size(Xrgb)
  16. if n>N-1
  17.    error(1,'requested file greater than frame number required');
  18. end;
  19. %get two image frames
  20. if d==1
  21.     Xn=double(Xrgb(:,:,1,n));
  22.     Xnp1=double(Xrgb(:,:,1,n+1));
  23. elseif d==3
  24.     Xn=double(Xrgb(:,:,1,n)*0.299+Xrgb(:,:,2,n)*0.587+Xrgb(:,:,3,n)*0.114);
  25.     Xnp1=double(Xrgb(:,:,1,n+1)*0.299+Xrgb(:,:,2,n+1)*0.587+Xrgb(:,:,3,n+1)*0.114);
  26. else
  27.     error(2,'not an RGB or Monochrome image file');
  28. end;

  29. %get image size and adjust for border
  30. size(Xn)
  31. hm5=h-5; wm5=w-5;
  32. z=zeros(h,w); v1=z; v2=z;

  33. %initialize
  34. dsx2=v1; dsx1=v1; dst=v1;
  35. alpha2=625;
  36. imax=20;

  37. %Calculate gradients
  38. dst(5:hm5,5:wm5) = ( Xnp1(6:hm5+1,6:wm5+1)-Xn(6:hm5+1,6:wm5+1) + Xnp1(6:hm5+1,5:wm5)-Xn(6:hm5+1,5:wm5)+ Xnp1(5:hm5,6:wm5+1)-Xn(5:hm5,6:wm5+1) +Xnp1(5:hm5,5:wm5)-Xn(5:hm5,5:wm5))/4;
  39. dsx2(5:hm5,5:wm5) = ( Xnp1(6:hm5+1,6:wm5+1)-Xnp1(5:hm5,6:wm5+1) + Xnp1(6:hm5+1,5:wm5)-Xnp1(5:hm5,5:wm5)+ Xn(6:hm5+1,6:wm5+1)-Xn(5:hm5,6:wm5+1) +Xn(6:hm5+1,5:wm5)-Xn(5:hm5,5:wm5))/4;
  40. dsx1(5:hm5,5:wm5) = ( Xnp1(6:hm5+1,6:wm5+1)-Xnp1(6:hm5+1,5:wm5) + Xnp1(5:hm5,6:wm5+1)-Xnp1(5:hm5,5:wm5)+ Xn(6:hm5+1,6:wm5+1)-Xn(6:hm5+1,5:wm5) +Xn(5:hm5,6:wm5+1)-Xn(5:hm5,5:wm5))/4;


  41. for i=1:imax
  42.    delta=(dsx1.*v1+dsx2.*v2+dst)./(alpha2+dsx1.^2+dsx2.^2);
  43.    v1=v1-dsx1.*delta;
  44.    v2=v2-dsx2.*delta;
  45. end;
  46. u=z; u(5:hm5,5:wm5)=v1(5:hm5,5:wm5);
  47. v=z; v(5:hm5,5:wm5)=v2(5:hm5,5:wm5);

  48. xskip=round(h/32);
  49. [hs,ws]=size(u(1:xskip:h,1:xskip:w))
  50. us=zeros(hs,ws); vs=us;

  51. N=xskip^2;
  52. for i=1:hs-1
  53.   for j=1:ws-1
  54.      hk=i*xskip-xskip+1;
  55.      hl=i*xskip;
  56.      wk=j*xskip-xskip+1;
  57.      wl=j*xskip;
  58.      us(i,j)=sum(sum(u(hk:hl,wk:wl)))/N;
  59.      vs(i,j)=sum(sum(v(hk:hl,wk:wl)))/N;
  60.    end;
  61. end;

  62. figure(1);
  63. quiver(us,vs);
  64. colormap('default');
  65. axis ij;
  66. axis tight;
  67. axis equal;
复制代码
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-19 22:40 , Processed in 0.051726 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表