i have some videos with different resolution but for some computing reason i need to change the resolution of them to same on for example 240×320. how can i do this? please help me?
MATLAB: Changing the resolution of video frame
Image Processing Toolbox
Related Solutions
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
A solution would be to write the video as uncompressed avi, and invoke ffmpeg to compress it to mp4.
I've tested this code in R2019b on Windows and on Ubuntu
%% some example from VideoWriter doc
Z = peaks;surf(Z); axis tight manual set(gca,'nextplot','replacechildren'); %% video file
if isunix % for linux
pathVideoAVI = '~/someVideo.avi'; % filename, used later to generate mp4
elseif ispc % fow windows
pathVideoAVI = 'd:\someVideo.avi'; % filename, used later to generate mp4endwriterObj = VideoWriter(pathVideoAVI,'Uncompressed AVI');open(writerObj);%% animate and write AVI
for k = 1:20 surf(sin(2*pi*k/20)*Z,Z) frame = getframe(gcf); writeVideo(writerObj,frame);endclose(writerObj); % Close the movie file
%% convert AVI to MP4
pathVideoMP4 = regexprep(pathVideoAVI,'\.avi','.mp4'); % generate mp4 filename
if isunix % for linux [~,~] = system(sprintf('ffmpeg -i %s -y -an -c:v libx264 -crf 0 -preset slow %s',pathVideoAVI,pathVideoMP4)); % for this to work, you should have installed ffmpeg and have it available on PATH
elseif ispc % for windows
[~,~] = system(sprintf('ffmpeg.exe -i %s -y -an -c:v libx264 -crf 0 -preset slow %s',pathVideoAVI,pathVideoMP4)); % for this to work, you should have installed ffmpeg and have it available on PATHend
Of course you could finish this by removing the AVI file.
Best Answer