How to convert Image frame into video ?

1 次查看(过去 30 天)
clc;
output_folder = 'D:\Matlab book and files\SLT\frames'
location = dir('D:\Matlab book and files\SLT\frames\*.tiff');
vidObj = VideoReader('akiyo2_grayscale2.mp4');
numFrames = 0;
frame = cell(1,300) ;
frame2image = cell(1,300);
image2frame = cell(1,300);
rgbtogray = cell(1,300);
graytorgb = cell(1,300);
entrpopygray= cell(1,300);
while hasFrame(vidObj)
F = readFrame(vidObj);
numFrames = numFrames + 1;
frame{numFrames} = F ;
[r,c,n]=size(F)
end
numFrames
for i= 1:300
imshow(frame{1,i});
frame{1,i}=getframe;
rgbtogray{1,i} = rgb2gray(frame2im(frame{1,i}));
outputFileName = fullfile(output_folder, ['frame' num2str(i) '.tiff']);
imwrite(rgbtogray{1,i},outputFileName);
entrpopygray{1,i}= entropy(rgbtogray{1,i}); % calculate a entropy of each image
end
video = VideoWriter('yourvideo.avi'); %create the video object
video.FrameRate= vidObj.FrameRate;
open(video); %open the file for writing
for ii=1:300
img = location(ii).name ;
I = imread(img); % the problem i am facing to concatenate a frames into video
F= getframe(gcf);
writeVideo(video,F); %write the image to file
end
close(video); %close the file

采纳的回答

Anusha
Anusha 2022-8-25
Hi,
I understand that you are trying to convert a video into image frames, calculating the entropy of each frame/image and then recombining the image sequence into a video.
I found two issues with the code posted.
  • The ‘location’ variable is declared and accessed at the start of the code. At this point, the video frames are not extracted and the ‘frames’ folder is empty.
  • To access the folder containing the individual video frames in .tiff format, the location variable should be declared after the frames are extracted from the video sequence.
  • VideoWriter function was unable to find the output path to store the video object.
  • The correct path for the video object should be updated. Note that the individual frames and the final video sequence will be stored in the “output_folder = 'D:\Matlab book and files\SLT\frames' “.
The working code with the modifications is shown below:
clc;
output_folder = 'D:\Matlab book and files\SLT\frames';
vidObj = VideoReader('akiyo2_grayscale2.mp4');
numFrames = 0;
frame = cell(1,300) ;
frame2image = cell(1,300);
image2frame = cell(1,300);
rgbtogray = cell(1,300);
graytorgb = cell(1,300);
entrpopygray= cell(1,300);
while hasFrame(vidObj)
F = readFrame(vidObj);
numFrames = numFrames + 1;
frame{numFrames} = F ;
[r,c,n]=size(F)
end
numFrames
for i= 1:300
imshow(frame{1,i});
frame{1,i}=getframe;
rgbtogray{1,i} = rgb2gray(frame2im(frame{1,i}));
outputFileName = fullfile(output_folder, ['frame' num2str(i) '.tiff']);
imwrite(rgbtogray{1,i},outputFileName);
entrpopygray{1,i}= entropy(rgbtogray{1,i}); % calculate a entropy of each image
end
% Access the frames folder with individual frames
location = dir('D:\Matlab book and files\SLT\frames\*.tiff');
imageNames = {location.name}';
% Define the correct folder path to store the combined video sequence
video = VideoWriter(fullfile(output_folder,'yourvideo.avi'));
%create the video object
video.FrameRate= vidObj.FrameRate;
open(video)
for ii = 1:length(imageNames)
img = imread(fullfile(output_folder,imageNames{ii}));
writeVideo(video,img)
end
close(video)
Please refer to the following documentation for more details regarding ‘image to video sequence conversion’ : Convert Between Image Sequences and Video - MATLAB & Simulink (mathworks.com)
Thanks,
Anusha

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by