MATLAB: I keep trying to convert a video grayscale but it gives me an error

gray scaleImage Processing Toolboxvideo processing

hi, i want to convert a video into grayscale, but it keeps giving me an error, specifically a "parse_input" error
my code:
%Create a multimedia reader video
readerobj = VideoReader('yourvideo.mp4', 'tag', 'myreader1');
readerobj.Width
readerobj.Height
% Read in all video frames.
vidFrames = read(readerobj);
% Get the number of frames.
numFrames = get(readerobj, 'NumberOfFrames');
%creat an output video file
v = VideoWriter('peaks.avi');
%Set the output video framerate to the input frame rate
v.FrameRate = readerobj.FrameRate;
open(v);
% Create a MATLAB movie struct from the video frames.
for k = 1 : numFrames
%get a frame from the video
frame=vidFrames(:,:,:,k);
frame = rgb2gray(frame);
%write the frame to the output video file
writeVideo(v,frame);
%add the frame to a movie object
mov(k).cdata = frame;
mov(k).colormap = [];
%if you want to save the frame
%Create Filename to store
end
%close the output video file. This will finish writing he video file
close(v);
% Create a figure
hf = figure;
% Resize figure based on the video's width and height
set(hf, 'position', [150 150 readerobj.Width readerobj.Height])
% Playback movie once at the video's frame rate
movie(hf, mov, 1, readerobj.FrameRate);

Best Answer

Instead of rgb2gray(), use read() and pass it the frame number. Should look something like this:
% Loop through the movie, writing all frames out.
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisInputFrame = read(inputVideoReaderObject, frame);
% Display it
image(thisInputFrame);
if ndims(thisInputFrame) == 3
thisInputFrame = rgb2gray(thisInputFrame);
end
axis on;
axis image;
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
% Resize the image.
outputFrame = imresize(thisInputFrame, [outputVideoRows, outputVideoColumns]);
% Write this new, resized frame out to the new video file.
writeVideo(outputVideoWriterObject, outputFrame);
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
numberOfFramesWritten = numberOfFramesWritten + 1;
end
Related Question