Hi all,
I have a function designed to take video and split it into individual frames and output those frames to a folder with a predetermined prefix attached to the name of each output frame. The problem here is that this function works perfectly in matlab 2016, which I wrote it in, but no longer works in matlab 2019 and I cannot figure out why this is. Matlab tells me that the error is on line 74, with the sprintf function. I can't see it.
Hopefully, is a really simple one for someone. The full code is listed below Thanks in advance.
% -------------------------------------------------------------------------
% v2i3 function
% -------------------------------------------------------------------------function [ ] = v2i3(absolute_video_file_path, output_image_file_prefix, output_file_ext)%% ------------------------------------------------------------------------
% Discussion
% -------------------------------------------------------------------------% Function video2image3 takes a video object and seperate it into individual
% frames. The number of frames that can be selected is arbitrary but for
% this work, the number is the standard 25 fps.
% -------------------------------------------------------------------------%% Construct a multimedia reader object associated with file
% 'absolute_video_file_path' with user tag set to 'myreader1'.
[input_file_path, input_file_name, input_file_extension] = fileparts(absolute_video_file_path);%readerobj = VideoReader('\video\MVI_0002.AVI', 'tag', 'myreader1');
readerobj = VideoReader(absolute_video_file_path, 'tag', 'myreader1');% Get the number of frames.
numFrames = get(readerobj, 'NumberOfFrames');% Ask user if they want to write the individual frames out to disk.
promptMessage = sprintf(['There are ', num2str(numFrames), ' frames. Do you want to save the individual frames out to individual disk files?']);button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No', 'Yes');% Create a MATLAB movie struct from the video frames.
mov = struct();written_frames = 0;for v = 1:numFrames% Read in all video frames.
vidFrames = read(readerobj, v);%folder = fullfile('C:\Users\jkissi\Downloads\research\SfM\examples\wall3\video\');
%movieFullFileName = fullfile(folder, 'MVI_0002.AVI');
%movieFullFileName = fullfile(folder, input_file_name);
%prefix = 'MVI02';
prefix = output_image_file_prefix;if strcmp(button, 'Yes') writeToDisk = true; % Extract out the various parts of the filename.
[folder, video_file_name, extension] = fileparts(absolute_video_file_path); % Make up a special new output subfolder for all the separate
% movie frames that we're going to extract and save to disk.
% (Don't worry - windows can handle forward slashes in the folder name.)
%folder = [folder, '\']; % Make it a subfolder of the folder where this m-file lives.
output_folder = sprintf('%s/frames_from_%s', folder, input_file_name); % Create the folder if it doesn't exist already.
if(~exist(output_folder, 'dir')) mkdir(output_folder); endelse writeToDisk = false;end%for k = 1:numFrames
mov.cdata = vidFrames(:,:,:); mov.colormap = []; thisFrame = mov.cdata; hImage = subplot(1,1,1); image(thisFrame); %axis square;
axis off; drawnow; % Force it to refresh the window.
set(findobj(gcf, 'type','axes'), 'Visible','off') % Write the image array to the output file, if requested.
if writeToDisk % Construct an output image file name.
%output_file_name = sprintf([prefix,'_','%4.4d','.',output_file_ext], v);
output_file_name = sprintf([prefix,'_','%04d','.',output_file_ext], v); output_file_path = fullfile(output_folder, output_file_name); % Extract the image with the text "burned into" it.
frameWithText = getframe(gca); % Write it out to disk.
imwrite(frameWithText.cdata, output_file_path, output_file_ext); end % Update user with the progress. Display in the command window.
if writeToDisk progressIndication = sprintf('Wrote frame %4d of %d.', v, numFrames); else progressIndication = sprintf('Processed frame %4d of %d.', v, numFrames); end disp(progressIndication); % Increment frame count (should eventually = numberOfFrames
% unless an error happens).
written_frames = written_frames + 1; end% Inform of completion
if writeToDisk finishedMessage = sprintf('Done! It wrote %d frames to folder\n"%s"', written_frames, output_folder);else finishedMessage = sprintf('Done! It processed %d frames of\n"%s"', written_frames, [input_file_name, input_file_extension]);enddisp(finishedMessage); % Write to command window.
uiwait(msgbox(finishedMessage)); % Also pop up a message box.
%--------------------------------------------------------------------------
% Terminate
%--------------------------------------------------------------------------disp('Read complete. Function v2i3.m terminating.');end
Best Answer