image thumbnail

March 2013 Japanese webinar "Introduction to Image Processing and Computer Vision with MATLAB"

by

 

25 Mar 2013 (Updated )

Script used in the Japanese webinar for image processing and computer vision held on Mar. 01, 2013.

S2_2a_optflow_webcam.m
%% Copyright 2013 The MathWorks, Inc.
% This is a simple script for optical flow
%
% Original version created by Takuya Otani
% Senior Application Engineer, MathWorks, Japan


%% Rs[^rWfF
% ZNVs
clear all; close all; clc; imaqreset;

% rfIJ̏ݒ(IuWFNg`)   [s]
hCamera = imaq.VideoDevice('winvideo', 1, 'RGB24_640x480');

% IveBJt[õIuWFNg`
optical = vision.OpticalFlow( ...
    'OutputValue','Horizontal and vertical components in complex form');

% PC̉ʂɃrfI\r[̒`
viewer = vision.DeployableVideoPlayer;
 
% oxNgƂĉ摜̏ɕ`悷IuWFNg
shapes = vision.ShapeInserter('Shape','Lines', ...
   'BorderColor','Custom', 'CustomBorderColor',[255 0 0]);

% t[[g摜̏ɕ\
fps = single(0.0);
texts = vision.TextInserter('Running at %2.2f fps', ...
  'Color',[0, 255, 0], 'FontSize',30, 'Location',[50 50]);

% t[[gvZpɃ^C}[X^[g
t = tic();
cnt = 1;

a=true;
sz = get(0,'ScreenSize');
figure('MenuBar','none','Toolbar','none','Position',[20 sz(4)-100 100 70])
uicontrol('Style', 'pushbutton', 'String', 'Stop',...
        'Position', [20 20 100 40],...
        'Callback', 'a=false;');

%% ͓摜1t[郋[v
while (a)
  frame = step(hCamera);         % J1t[捞
  frame = im2single(frame);
  
  of = step(optical,rgb2gray(frame));% IveBJt[
  lines = videooptflowlines(of, 100);% xNg̎n_I_
  
  Ishp = step(shapes,frame,lines);   % xNg`
  Itxt = step(texts,Ishp,fps);       % t[[g̑}
  step(viewer,Itxt);                 % ʃt[XV

   %Ԍv
   % 10t[̏vԂt[[gvZ
   cnt = cnt + 1;
   if (mod(cnt,10) == 0)
    t = toc(t);
    fps = single(10/t);
    t = tic();
   end
end

release(hCamera);
release(optical);
release(viewer);
release(shapes);
release(texts);

%% FigureStop{^ŏI



% IFɉp̌Љ


Contact us