Display video and save frames in the same time !
2 次查看(过去 30 天)
显示 更早的评论
I want to display a video on GUI and in the same time I want it to convert video to frames and save on another folder automatically !
It's create a folder but the folder is empty ! also, I have some errors after I closed the window !
Code:
function Display_Callback(hObject, eventdata, handles)
filename = get(handles.edit1, 'String');
if ~exist(filename, 'file')
warndlg( 'Text in edit box is not the name of a file');
return
end
try
obj = VideoReader(filename);
catch
warndlg( 'File named in edit box does not appear to be a usable movie file');
return
end
ax = handles.ax1;
while hasFrame(obj)
vidFrame = readFrame(obj);
image(vidFrame, 'Parent', ax);
set(ax, 'Visible', 'off');
pause(1/obj.FrameRate);
end
OutVideoDir = 'Frames';
mkdir(OutVideoDir);
for i = 1:obj.NumberOfFrames
img = read(obj,i);
baseFileName = sprintf('%d.png', i);
fullFileName = fullfile(OutVideoDir, baseFileName);
imwrite(img, fullFileName);
end
1 个评论
Jan
2018-2-4
If you get some errors and want us to help you to fix them, it is a good idea to post the messages. Please do not let the readers guess, which problem you have and what you observe.
回答(1 个)
Jan
2018-2-4
What does "in the same time" mean? Currently you run two loops after each other. What about inserting the code in one loop?
OutVideoDir = 'Frames';
mkdir(OutVideoDir);
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
ImageH = image([], 'Parent', ax);
while hasFrame(obj)
vidFrame = readFrame(obj);
set(ImageH, 'CData', vidFrame);
baseFileName = sprintf('%d.png', i);
fullFileName = fullfile(OutVideoDir, baseFileName);
imwrite(img, fullFileName);
pause(1/obj.FrameRate);
end
It is cheaper to create one image and update its CData afterwards.
2 个评论
Jan
2018-2-4
If handles.ax1 is the handle of an axes object, this line:
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
tries to convert the character arrays 'NextPlot', 'add' and so on to numerical indices. You mean:
ax = handles.ax1;
set(ax, 'NextPlot', 'add', 'Visible', 'off');
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!