Hello everyone...!!!
I have introduced motion detection and estimation in the previous post. In this article, I have discussed some ideas for motion detection and surveillance systems using optical flow algorithms.
Optical Flow is an informative and popular way to estimate motion in a video. In this method, spatiotemporal information from the frame sequence of a video is used to estimate the motion vectors between consecutive pair of frames. In MATLAB, there are two famous optical flow algorithms that are included in the Computer Vision System Toolbox given by Horn-Schunk and Lucas-Kanade.
I have used some part of test video of the snooker player, the same that was used in the earlier post. The algorithm for Optical Flow using MATLAB has several steps. In first step, read the video and create a system object for motion estimation. Feel free to use help option provided in the MATLAB window. While declaring for the optical flow, the method was provided as either 'Lucas-Kanade' or 'Horn-Schunck'. In this article, the flow is estimated for only two frame pairs. The first pair is frames 2 and 3 when there was no motion and second for the frames 121 and 122 when the snooker ball was in motion. The optical flow results were obtained as matrix with complex values whose absolute values were further calculated. Resulting images are shown below the code for frame pair(2,3).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Name: Nitin Sharma
%%%%% Code: o_flow.m
%%%%% MATLAB version: R2014a
%%%%% Toolbox required: Computer Vision System Toolbox, Image
%%%%% Processing Toolbox
%%%%% Utility: Motion Detection, Basis of surveillance system design,
%%%%% Understanding Optical Flow Algorithms
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Read the video
videoObj = VideoReader('test_snooker.mp4');
%%% Optical Flow Method and Parameters assigned
opticalFlow = vision.OpticalFlow ('ReferenceFrameDelay',1);
opticalFlow.OutputValue = 'Horizontal and vertical components in complex form';
opticalFlow.Method = 'Lucas-Kanade'; %%% Replace it for Horn-Schunck
%%% Analyzing Pair of frames (121, 122). Write(2,3) for still frames and 1 at the place of 120.
%%% Note: Only grayscale frames are permitted in the MATLAB algorithm
for i = 121:122
frame = read(videoObj,i);
temp = rgb2gray(frame);
im(:,:,i-120) = single(temp);
of(:,:,i-120) = step(opticalFlow, im(:,:,i-120));
end
%%% Calculate Absolute value of optical flow and its maximum deviation
%%% Relative to the maximum and minimum deviation the motion can be
%%% detected
absMotion = abs(of);
disp(max(max(absMotion)));
%%% Analysing frames and the normalized flow value
subplot(2,2,1),imshow(uint8(im(:,:,1))),title('121 frame');
subplot(2,2,2),imshow(uint8(im(:,:,2))),title('122 frame');
subplot(2,2,3)
imshowpair(im(:,:,1),im(:,:,2), 'ColorChannels','red-cyan')
title('Composite Image (Red – Frame 121, Cyan – Frame 122)');
subplot(2,2,4)
imshow(mat2gray(absMotion(:,:,2)))
title('Normalized absolute optical flow value');
%%% Lines of white color are used to show for the optical flow using
%%% videooptflowlines function
shapeInserter = vision.ShapeInserter('Shape','Lines','BorderColor',...
'Custom','CustomBorderColor', 255);
lines = videooptflowlines(of(:,:,2),2);
out = step(shapeInserter, im(:,:,2), lines);
figure,imshow(uint8(out))
%%% To overcome much larger and smaller optical flow lines windowing is
%%% done to permit only desired magnitude of optical flow lines to be
%%% displayed
of(abs(of)>18)=0;
of(abs(of)<4)=0;
lines = videooptflowlines(of(:,:,2), 3);
out = step(shapeInserter, im(:,:,2), lines);
figure,imshow(uint8(out))
Consequently, after running this code, it was found that the optical flow lines have detected successfully the motion of snooker ball. There were many confusing outliers found during analysis that were windowed between a desired magnitude range. It can be used in surveillance systems to detect for the motion of intruder in outdoors as well as in indoors keeping other factors in mind. Results for frame pairs (2,3) and (121,122) are shown below followed by motion detection of snooker ball images in (121, 122) frame pair. The results showed increase in the absolute value of motion estimation vector because of motion of snooker ball.
%%% Result for (2,3) pair %%% Result for (121,122) pair
>> oflow >> oflow
(:,:,1) = (:,:,1) =
1.8208e+03 2.1426e+03
(:,:,2) = (:,:,2) =
18.7389 268.6414
0 comments: