Borrar filtros
Borrar filtros

How can I return 4D coordination of my bbox after each for loop iteration?

1 visualización (últimos 30 días)
meri mak
meri mak el 21 de Mzo. de 2017
Editada: meri mak el 22 de Mzo. de 2017
have a video with around 500 frames, and for the detection of the object I have trained a cascade, which does the detection with reasonable accuracy. I need to return 500 *4D array relative to the position of bbox/frame and then calculate the centroid in order to visualize the trajectory of the moving object
I've tried to use a cell array to return the 4D [X Y W H] of each bbox through an if statement inside the for loop ( reading video frames and applying tracker.xml on each frame). the cell array size is (1x500), however it just return 16 bboxes,
the following is my code I have attached one of the frame with the bounding box showing the object.
clc; close all; imtool close all; clear; workspace; fontSize = 22;
folder = fileparts(which('longbr.mp4')); % Determine where videofolder exist
movieFullFileName = fullfile(folder, 'pan-head.mp4');
% if the video exist or not?
if ~exist(movieFullFileName, 'file') strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new one, or cancel', movieFullFileName); response = questdlg(strErrorMessage, 'File not found', 'OK - choose a new movie.', 'Cancel', 'OK - choose a new movie.'); if strcmpi(response, 'OK - choose a new movie.') [baseFileName, folderName, FilterIndex] = uigetfile('*.avi'); if ~isequal(baseFileName, 0) movieFullFileName = fullfile(folderName, baseFileName); else return; end else return; end end
videoObject = VideoReader(movieFullFileName)
% Determine how many frames there are.
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
% open figure to show the images in the upper half of the screen.
screenSize = get(0, 'ScreenSize');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Ask user if they want to write the individual frames out to disk.
promptMessage = sprintf('Do you want to save the individual frames out to individual disk files?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No', 'Yes');
if strcmp(button, 'Yes')
writeToDisk = true;
% Extract out the various parts of the filename.
[folder, baseFileName, extentions] = fileparts(movieFullFileName);
% new output subfolder for all each movie frame which will be
% extracted and save into disk
folder = pwd; % Make it a subfolder of the folder where this m-file lives.
outputFolder = sprintf('%s/Movie Frames from %s', folder, baseFileName);
% Create the folder if it doesn't exist already.
if ~exist(outputFolder, 'dir')
writeToDisk = false;
% preallocate 3D array(s) for truecolor video frame(s)-----> R/G/B
% vidHeight, vidWidth and numberOfFrames are define in first section
% their values is already acheievd and saved in workplace
detector = vision.CascadeObjectDetector('ozzyobj4.xml');
%roi = [262, 197, 152, 23]; % Loop through the movie, writing all frames out. for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = read(videoObject, frame);
bbox = step(detector,thisFrame);
if ~isempty(bbox) Boxes{frame} = {[bbox(1,1), bbox(1,2),bbox(1,3), bbox(1,4)]}; % Store [x,y]; detectedImg = insertObjectAnnotation(thisFrame,'rectangle',bbox,'panhead'); else Boxes{frame} = {[1, 1]}; end
%Display it
hImage = subplot(2, 2, 1);
image( detectedImg);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; %refresh the window.
% Write the image array to the output file, if requested.
%if writeToDisk
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Extract the image with the text "burned into" it.
frameWithText = getframe(gca);
imwrite(frameWithText.cdata, outputFullFileName, 'png');

Respuestas (0)


Más información sobre Feature Detection and Extraction en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by