2

我正在尝试使用 quiver 绘制光流。我可以绘制光流线,但不能绘制箭头。此代码与 Matlab 教程中的类似示例。以下是代码

function display_optical_flow (video_file_name)
    vid_reader_obj = vision.VideoFileReader(video_file_name,    'VideoOutputDataType','single');

   optical_flow_calculator = vision.OpticalFlow('Method','Lucas-  Kanade','TemporalGradientFilter','Derivative of Gaussian', 'MotionVectorImageOutputPort', ...
    true, 'OutputValue',  'Horizontal and vertical components in complex form', 'NoiseReductionThreshold',0.02);


    screen_size = get(0, 'ScreenSize');
    player_window_pos = [20 screen_size(4)-300 400 400];
    video_display_originalVideo = vision.VideoPlayer('Name', 'Original Input Video',    'Position', player_window_pos);
    player_window_pos(1) = player_window_pos(1) + 500;
    video_display_opticalFlow = vision.VideoPlayer('Name','Optical Flow Vector', 'Position',player_window_pos);
    player_window_pos(1) = player_window_pos(1) + 500;
video_display_of_image = vision.VideoPlayer('Name','Optical Flow image', 'Position',player_window_pos);

shape_inserter_line = vision.ShapeInserter('Shape','Lines', 'BorderColor','Custom','CustomBorderColor', [255 255 0]);

motion_vector_gain=20;
decimFactorRow = 5;
decimFactorCol = 5;
borderOffset   = 5;

flag_first_time = true;

while ~isDone(vid_reader_obj)
   cur_rgb_frame = step(vid_reader_obj);
   cur_gray_frame = rgb2gray(cur_rgb_frame);

   [optical_flow_vector IMV]= step(optical_flow_calculator, cur_gray_frame);
   %magnitude of optical flow vectors by dot product
   %of_magnitude= optical_flow_vector .* conj(optical_flow_vector);



   if true == flag_first_time
       flag_first_time = false;
      [row col] = size(optical_flow_vector);
       rowV = borderOffset:decimFactorRow:(row-borderOffset);
      colV = borderOffset:decimFactorCol:(col-borderOffset);
      [Y X] = meshgrid(rowV, colV);
   end

     lines =videooptflowlines(optical_flow_vector, 20);
   if ~isempty(lines)
      motion_vectors =  step(shape_inserter_line, cur_rgb_frame, lines);
      step(video_display_opticalFlow, motion_vectors);
   end

   %Display 
   step(video_display_originalVideo, cur_rgb_frame);
   step(video_display_of_image, IMV);
% 
   [Xsize Ysize] = meshgrid(1:size(cur_gray_frame, 2), 1:size(cur_gray_frame, 1));

   imshow(cur_gray_frame); hold on;
   Real_part = real(optical_flow_vector(:));
   Imag_part = imag(optical_flow_vector(:));
   quiver(Xsize(:), Ysize(:), Real_part(:), Imag_part(:), 0);
   pause;

end
release (video_display_opticalFlow);
release (video_display_originalVideo);
release (vid_reader_obj);

这不会像我期望的那样通过颤动在每帧图像上看到的箭头显示。我应该做些什么来纠正代码?尼丁

4

0 回答 0