Resize Frame for Optical Flow

2 vues (au cours des 30 derniers jours)
Kamil Stepien
Kamil Stepien le 23 Mar 2016
I have problem with changing the resolution of the video file. There is a problem with optical flow if the frame size have been manipulated in any way this gives me error. There are two options either change the resolution of the video at the beginning or somehow how change the frame size in a way that optical flow will work. This is a small prototype for detecting micro-expressions and I will want to add a cascade object to detect nose, mouth and eyes in further development therefore I need solution that will work for individual regions without necessary setting optical flow individually for those regions especially that a bounding box does not have a fixed size and it will displace itself slightly from frame to frame. Here is my code so far, the error is that it is exceeding matrix dimensions.
close all
clear
clc
%Create a cascade face detector object.
faceDetector = vision.CascadeObjectDetector();
%Create video object. Input Video File and set some properties / Read video frames and audio samples from video file
vidObj = vision.VideoFileReader('MEXTest.mp4','ImageColorSpace','Intensity','VideoOutputDataType','uint8');
%Convert and scale input image to specified output data type (This need to be changed for future releases of Matlab
converter = vision.ImageDataTypeConverter;
%Estimate object velocities
opticalFlow = vision.OpticalFlow('ReferenceFrameDelay', 1);
%Form of velocity output
opticalFlow.OutputValue = 'Horizontal and vertical components in complex form';
%Draw rectangles, lines, polygons, or circles on an image (This need to be changed for future releases of Matlab
shapeInserter = vision.ShapeInserter('Shape','Lines','BorderColor','Custom','CustomBorderColor', 255);
%Play video or display image
vidPlayer = vision.VideoPlayer('Name','Motion Vector');
while ~isDone(vidObj);
%set individual frames
frame = step(vidObj);
%resize frame for faster computation
fraRes = imresize(frame,0.5);
%set bounding box on the frame
fbbox = step(faceDetector,fraRes);
%Annotate truecolor or grayscale image or video stream / set bbox properites
addfbbox = insertObjectAnnotation(fraRes,'rectangle',fbbox,'Face');
%imshow(addfbbox,'border','tight');
if size(fbbox,1) == 0 %if there is no face detected
disp('Face not detected'); %display this
pause(0.5) %and pasue the programme
else if size(fbbox,1) > 1 %if there is more than one face detected
disp('Too many Faces') %display this
else %or for one face follow on
I = imcrop(fraRes,fbbox);
%imshow(I);
%Convert the image to single precision, then compute optical flow for the video. Generate coordinate points and draw lines to indicate flow. Display results.
im = step(converter,I);
of = step(opticalFlow,im);
lines = videooptflowlines(of, 20);
if ~isempty(lines)
out = step(shapeInserter,im,lines);
step(vidPlayer,out);
end
end
end
end
release(vidPlayer);
release(VidObj);

Réponses (1)

Kamil Stepien
Kamil Stepien le 31 Mar 2016
UPDATE: I went and edited the function for optical flow which creates lines and this sorts out the size issue however it is necessary to to input this manually for each input however there is a problem that I need to use release() method before making another step but non of the variables are allowed to be entered. Does anyone know how to create this release() method to make it work. Here is the updated code:
faceDetector = vision.CascadeObjectDetector();
faceDetector.MinSize = [150 150];
vidRead = vision.VideoFileReader('MEXTest.mp4','ImageColorSpace','Intensity','VideoOutputDataType','uint8');
convert = vision.ImageDataTypeConverter;
optFlo = vision.OpticalFlow('ReferenceFrameDelay', 1);
optFlo.OutputValue = 'Horizontal and vertical components in complex form';
shapeInserter = vision.ShapeInserter('Shape','Lines','BorderColor','Custom', 'CustomBorderColor', 255);
while ~isDone(vidRead)
frame = step(vidRead);
fraRes = imresize(frame,0.3);
fraSin = im2single(fraRes);
bbox = step(faceDetector,fraSin);
I = imcrop(fraSin, bbox);
im = step(convert, I);
of = step(optFlo, im); %I need to release something in here
lines = optfloo(of, 50); %use videooptflowlines instead of (optfloo)
out = step(shapeInserter, im, lines);
imshow(out);
end

Catégories

En savoir plus sur Tracking and Motion Estimation dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by