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

조회 수: 2 (최근 30일)
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
Ameer Hamza
Ameer Hamza 2020년 4월 25일
What is the error message? How is vidFrames calculated?
Ahmed Al-Qarqaz
Ahmed Al-Qarqaz 2020년 4월 25일
never mind someone solved it, but thanks anyway

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 4월 25일
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
Pamudu Ranasinghe
Pamudu Ranasinghe 2022년 6월 18일
편집: Pamudu Ranasinghe 2022년 6월 18일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by