Hello Mat

 找回密码
 立即注册
查看: 5649|回复: 3

光流法optical flow

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-2-20 22:10:00 | 显示全部楼层 |阅读模式
光流法optical flow代码如下:
游客,如果您要查看本帖隐藏内容请回复

  1. function [output] = optical_flow(imgs, options)

  2. t = tic;
  3. position = [];
  4. velocity = [];
  5. if nargin == 0
  6.     display('running demo...')
  7.     display('loading data...')
  8.     try
  9.     load cat_data
  10.     catch
  11.         display('missing demo file "cat_data.mat"')
  12.         display('demo aborted.')
  13.         return
  14.     end
  15.     imgs = single(imgs);
  16. end

  17. if size(imgs,3) < 2
  18.     error('need at least 2 image frames for optical flow algorithm to operate')
  19. end

  20. if nargin < 2
  21.     x_blk_size = floor(.04*size(imgs,2));
  22.     y_blk_size = floor(.04*size(imgs,1));
  23. else
  24.     if ~isfield(options,'x_blk_size')
  25.         x_blk_size = floor(.04*size(imgs,2));
  26.         display(sprintf('no x_blk_size options found, using %g',x_blk_size))
  27.     else
  28.         x_blk_size=options.x_blk_size;
  29.     end

  30.     if ~isfield(options,'y_blk_size')
  31.         y_blk_size = floor(.04*size(imgs,1));
  32.         display(sprintf('no x_blk_size options found, using %g',y_blk_size))
  33.     else
  34.         y_blk_size=options.y_blk_size;
  35.     end

  36.     if ~isfield(options,'displayResults')
  37.         displayResults = 0;
  38.     else
  39.         displayResults=options.displayResults;
  40.     end
  41. end

  42. nframe = size(imgs,3);

  43. for iframe = 1:nframe-1
  44.     display(sprintf('processing frame: %g of %g...',iframe,nframe-1))
  45.    
  46.     frame1 = squeeze(imgs(:,:,iframe));
  47.     frame2 = squeeze(imgs(:,:,iframe+1));
  48.     %--------------------------------------------------------------------------
  49.     %  calculate change in image with respect to position
  50.     [Ix,Iy] = gradient(frame1);
  51.     [ny, nx] = size(Ix);
  52.     %--------------------------------------------------------------------------
  53.     % calculate change in image with respect to time using nearest frames
  54.     It = frame2-frame1;
  55.    
  56.     %--------------------------------------------------------------------------
  57.     % initialize counter
  58.     ct = 1;
  59.    
  60.     %--------------------------------------------------------------------------
  61.     % calculate x-range of first block
  62.     x1 = 1;
  63.     x2 = x1+x_blk_size;
  64.    
  65.     %--------------------------------------------------------------------------
  66.     % calculate steps in x and y direction
  67.     xs = floor((nx-x_blk_size)/x_blk_size);
  68.     ys = floor((ny-y_blk_size)/y_blk_size);
  69.    
  70.     %--------------------------------------------------------------------------
  71.     % initialzes velocity vectors
  72.     % vx,vy are used to store the x-components of the velocity vector and
  73.     % y-components of the velocity vector, respectively.
  74.     vx = zeros(1,ys*xs);
  75.     vy = zeros(1,ys*xs);
  76.    
  77.     %--------------------------------------------------------------------------
  78.     % initializes position vectors
  79.     % x,y store the center position of the block used to calculate the optical
  80.     % flows values.
  81.     x = zeros(1,ys*xs);
  82.     y = zeros(1,ys*xs);
  83.    
  84.    
  85.     %--------------------------------------------------------------------------
  86.     % loop through image
  87.     for ix = 1:xs
  88.         
  89.         y1 = 1;
  90.         y2 = y1 + y_blk_size;
  91.         
  92.         for iy = 1:ys
  93.             
  94.             %------------------------------------------------------------------
  95.             % select a sub-domain from gradient and difference image to perform
  96.             % calculation on
  97.             
  98.             Ix_block = Ix(y1:y2,x1:x2);
  99.             Iy_block = Iy(y1:y2,x1:x2);
  100.             It_block = It(y1:y2,x1:x2);
  101.             
  102.             
  103.             %------------------------------------------------------------------
  104.             % Cast problem as linear equation and solve in a lsqr sense
  105.             % This approach is known as the Lucas-Kanade Method
  106.             % A*u = f
  107.             % A'*A*u = A'*f
  108.             % u = inv(A'*A)*A*f
  109.             % solve inv(A'*A) using pseudo-inv (pinv)
  110.             % f -> It  (change in image with repsect to time)
  111.             % A -> [Ix,Iy] (chainge in image with respect to position)
  112.             % u -> [vx,vy] (velocities, what we want to solve for)
  113.             
  114.             A = [Ix_block(:) , Iy_block(:)];
  115.             b = -It_block(:);
  116.             
  117.             A = A(1:1:end,:);
  118.             b = b(1:1:end);
  119.             
  120.             P = pinv(A'*A);
  121.             u = P*A'*b;
  122.             
  123.             %------------------------------------------------------------------
  124.             % realtive velocities from current sub-domain.
  125.             %
  126.             % Note: to convert this to a real velocity e.g. [m/s] you would
  127.             % need to include information on the rate between frames
  128.             % (delta-t) and the distance between neighbooring pixels in the
  129.             % images (delta-x, delta-y). Otherwise, the result is a
  130.             % relative velocity.
  131.             vx(ct) = u(1);
  132.             vy(ct) = u(2);
  133.             
  134.             %------------------------------------------------------------------
  135.             % calculate mid point of sub-domain
  136.             y(ct) = (x1+x2)/2;
  137.             x(ct) = (y1+y2)/2;
  138.             
  139.             ct = ct+1;
  140.             
  141.             %------------------------------------------------------------------
  142.             % get the y range of the new block
  143.             y1 = y1 + y_blk_size + 1;
  144.             y2 = y1 + y_blk_size;
  145.             
  146.             %------------------------------------------------------------------
  147.             % make sure you don't exceed the image size in the y direction
  148.             if y2 > ny
  149.                 y2  = ny;
  150.             end
  151.         end
  152.         %----------------------------------------------------------------------
  153.         % get the x range of the new block
  154.         x1 = x1 + x_blk_size + 1;
  155.         x2 = x1 + x_blk_size;
  156.         %----------------------------------------------------------------------
  157.         % make sure you don't exceed the image size in the x direction
  158.         if x2 > nx
  159.             x2 = nx;
  160.         end
  161.     end
  162.    
  163.     position(iframe,:,:) = [y ; x];
  164.     velocity(iframe,:,:) = [vy ; vx];
  165.     x = [];
  166.     y = [];
  167.     vx = [];
  168.     vy = [];
  169.     ct = 1;
  170.    
  171. end
  172. %--------------------------------------------------------------------------
  173. % if there are no input arguments, plot results
  174. if nargin == 0 || options.displayResults
  175.    
  176.    
  177.     figure
  178.     subplot(1,2,1)
  179.     imagesc(frame1),colormap gray
  180.     axis image
  181.     hold on
  182.     x = squeeze(position(10,2,:));
  183.     y = squeeze(position(10,1,:));
  184.     vx = squeeze(velocity(10,2,:));
  185.     vy = squeeze(velocity(10,1,:));
  186.     plot(y,x,'.k','markersize',1)
  187.     quiver(y,x,vy,vx,'r')
  188.     subplot(1,2,2)
  189.     imagesc(reshape(sqrt(vx.^2+vy.^2),[ys,xs]))
  190.     axis image
  191.    
  192.     figure
  193.    
  194.     for iframe = 1:size(velocity,1);
  195.         
  196.         subplot(1,2,1)
  197.         imagesc(squeeze(imgs(:,:,iframe+1))),colormap gray
  198.         title(sprintf('velocity vectors + image\n frame %g',iframe))
  199.         hold on
  200.         x = squeeze(position(iframe,2,:));
  201.         y = squeeze(position(iframe,1,:));
  202.         vx = squeeze(velocity(iframe,2,:));
  203.         vy = squeeze(velocity(iframe,1,:));
  204.         plot(y,x,'.k','markersize',1)
  205.         quiver(y,x,vy,vx,'r'), hold off
  206.         axis image, axis([1 nx 1 ny])
  207.         subplot(1,2,2)
  208.         imagesc(reshape(sqrt(vx.^2+vy.^2),[ys,xs])),colorbar
  209.         title(sprintf('speed map\nsqrt(vx^2+vy^2) \n frame %g',iframe))
  210.         axis image
  211.         drawnow
  212.         pause(.1)
  213.     end
  214.    
  215.    
  216. end

  217. output.position = position;
  218. output.velocity = velocity;
  219. output.num_blks_x = xs;
  220. output.num_blks_y = ys;
  221. output.blk_size_x = x_blk_size;
  222. output.blk_size_y = y_blk_size;
  223. output.nframes = nframe;
  224. output.process_time = sprintf('%3.2f [sec]',toc(t));
复制代码




算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

0

主题

1

帖子

3

金钱

新手上路

Rank: 1

积分
4
发表于 2018-3-29 19:55:23 | 显示全部楼层
麻烦了,亲
回复

使用道具 举报

0

主题

25

帖子

1

金钱

新手上路

Rank: 1

积分
26
发表于 2018-12-28 17:44:36 | 显示全部楼层
666666666666
回复 支持 反对

使用道具 举报

0

主题

13

帖子

1

金钱

新手上路

Rank: 1

积分
17
发表于 2019-8-3 19:33:55 | 显示全部楼层
谢谢楼主分享!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 08:48 , Processed in 0.211087 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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