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

2 visualizaciones (últimos 30 días)
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);
  2 comentarios
Ameer Hamza
Ameer Hamza el 25 de Abr. de 2020
What is the error message? How is vidFrames calculated?
Ahmed Al-Qarqaz
Ahmed Al-Qarqaz el 25 de Abr. de 2020
never mind someone solved it, but thanks anyway

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 25 de Abr. de 2020
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
  1 comentario
Pamudu Ranasinghe
Pamudu Ranasinghe el 18 de Jun. de 2022
Editada: Pamudu Ranasinghe el 18 de Jun. de 2022
I add some modifications to the above code of Image Analyst (MVP) and create it as a fuction to use
function RGB2GRAY_VIDEO(input_image_name,output_image_name,percentage_need)
% percentage_need --> if you need to convert Full video use 1
% 0.5 for 50 % frams from total Frames
% Likewise 0.2 for 20% Frames
% 0 < percentage_need <= 1
% Sample Function call
% RGB2GRAY_VIDEO('Video_Input.mp4','Video_out.mp4',0.2)
%Create a multimedia reader video
readerobj = VideoReader(input_image_name, 'tag', 'myreader1');
readerobj.Width
readerobj.Height
% Get the number of frames.
numFrames = get(readerobj, 'NumFrames');
%creat an output video file
v = VideoWriter(output_image_name);
%Set the output video framerate to the input frame rate
v.FrameRate = readerobj.FrameRate;
open(v);
outputVideoRows =readerobj.Height;
outputVideoColumns =readerobj.Width ;
Total_frames = numFrames;
numFrames = int8(Total_frames*percentage_need);
fprintf("%d Frames Needed from %d of Frames\n",numFrames,Total_frames);
% Loop through the movie, writing all frames out.
for frame = 1 : numFrames
% Extract the frame from the movie structure.
thisInputFrame = read(readerobj, frame);
% Display it
image(thisInputFrame);
if ndims(thisInputFrame) == 3
thisInputFrame = rgb2gray(thisInputFrame);
end
axis on;
axis image;
caption = sprintf('Frame %4d of %d.', frame, numFrames);
title(caption, 'FontSize', 12);
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(v, outputFrame);
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame, numFrames);
disp(progressIndication);
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by