For a project of mine I need to extract frames from hd cameras. The cameras save in .mts with 1920x1080. This is converted to .avi with h264 codec of same dimensions.
mmreader loads the movie and is able to read the info from it.
There are however a few issues:
1: Each time I ask my GUI to show the next frame it does load the image, but it gives an error as well. Since the image does load and the error doesnt occur on lower quality movies (of for example 640x480), I think its because either matlab of my computer is not fast enough in loading the images. Can somebody confirm if its matlab or the computer? Else I can ask my university if I can use a faster pc.
2 Related to that issue. The frames 251 to 255 are the same, whereas the movie appears to be smooth. It would be strange if 3 different cameras would all lagg at the same time. So either its the cvonversion to avi that causes the lagg, or mmreader was too slow and read those 4 frames as all the same. The result is when I save the selected frames as jpegs those frames are still the same. Since this is a project about motion tracking, this is quite bad. Any help or advise would be greatly appreciated.
This is the part of the code where the movie file is loaded with mmreader:
global hfig_extract
global extract_dat
global hfig_main
global setting
% open file browser to select AVI file, but only if no name has been set
% for the current_camera. If user cancels the uigetfile, the name is
% changed to 0, so also check that.
if isequal(extract_dat.file(setting.current_camera).name,'noname.avi') && isequal(extract_dat.file(setting.current_camera).pathname,'nodir')
[extract_dat.file(setting.current_camera).name, extract_dat.file(setting.current_camera).pathname] = uigetfile('*.*');
if isequal(extract_dat.file(setting.current_camera).name,0) && isequal(extract_dat.file(setting.current_camera).pathname,0)
extract_dat.file(setting.current_camera).name = 'noname.avi';
extract_dat.file(setting.current_camera).pathname = 'nodir';
end
else
end
extract_dat.file(setting.current_camera).Name = [extract_dat.file(setting.current_camera).pathname extract_dat.file(setting.current_camera).name];
addpath(extract_dat.file(setting.current_camera).pathname);
% check if OK button pressed
if ~isequal(extract_dat.file(setting.current_camera).name,'noname.avi') && ~isequal(extract_dat.file(setting.current_camera).pathname,'nodir')
[pathstr,name,ext,versn] = fileparts([extract_dat.file(setting.current_camera).pathname extract_dat.file(setting.current_camera).name]);
ext_size = size(ext');
if ext_size ~= 4
errordlg('Invalid Image File Extension');
else
% read movie
extract_dat.file(setting.current_camera).movie = mmreader(extract_dat.file(setting.current_camera).name);
% extract_dat.lastFrame = read(extract_dat.movie, inf);
extract_dat.file(setting.current_camera).numFrames = extract_dat.file(setting.current_camera).movie.NumberOfFrames;
-------------------------------------------------------------------------------------------
And these are the codes for the previous and next frame
% --- Executes on button press in pushbutton_reverse.
function pushbutton_reverse_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_reverse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global hfig_extract
global extract_dat
global setting
if extract_dat.file(setting.current_camera).current_frame > 1
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).current_frame - 1;
else
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).numFrames;
end
extract_dat.file(setting.current_camera).frame = read(extract_dat.file(setting.current_camera).movie,extract_dat.file(setting.current_camera).current_frame);
extract_dat.file(setting.current_camera).show = imshow(extract_dat.file(setting.current_camera).frame,'Parent',hfig_extract.axes_preview);
set(hfig_extract.listbox_names,'Value',extract_dat.file(setting.current_camera).current_frame);
set(hfig_extract.text13,'String',num2str(extract_dat.file(setting.current_camera).current_frame));
% --- Executes on button press in pushbutton_forward.
function pushbutton_forward_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_forward (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global hfig_extract
global extract_dat
global setting
if extract_dat.file(setting.current_camera).current_frame < extract_dat.file(setting.current_camera).numFrames
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).current_frame + 1;
else
extract_dat.file(setting.current_camera).current_frame = 1;
end
extract_dat.file(setting.current_camera).frame = read(extract_dat.file(setting.current_camera).movie,extract_dat.file(setting.current_camera).current_frame);
extract_dat.file(setting.current_camera).show = imshow(extract_dat.file(setting.current_camera).frame,'Parent',hfig_extract.axes_preview);
set(hfig_extract.listbox_names,'Value',extract_dat.file(setting.current_camera).current_frame);
set(hfig_extract.text13,'String',num2str(extract_dat.file(setting.current_camera).current_frame));
mmreader loads the movie and is able to read the info from it.
There are however a few issues:
1: Each time I ask my GUI to show the next frame it does load the image, but it gives an error as well. Since the image does load and the error doesnt occur on lower quality movies (of for example 640x480), I think its because either matlab of my computer is not fast enough in loading the images. Can somebody confirm if its matlab or the computer? Else I can ask my university if I can use a faster pc.
2 Related to that issue. The frames 251 to 255 are the same, whereas the movie appears to be smooth. It would be strange if 3 different cameras would all lagg at the same time. So either its the cvonversion to avi that causes the lagg, or mmreader was too slow and read those 4 frames as all the same. The result is when I save the selected frames as jpegs those frames are still the same. Since this is a project about motion tracking, this is quite bad. Any help or advise would be greatly appreciated.
This is the part of the code where the movie file is loaded with mmreader:
global hfig_extract
global extract_dat
global hfig_main
global setting
% open file browser to select AVI file, but only if no name has been set
% for the current_camera. If user cancels the uigetfile, the name is
% changed to 0, so also check that.
if isequal(extract_dat.file(setting.current_camera).name,'noname.avi') && isequal(extract_dat.file(setting.current_camera).pathname,'nodir')
[extract_dat.file(setting.current_camera).name, extract_dat.file(setting.current_camera).pathname] = uigetfile('*.*');
if isequal(extract_dat.file(setting.current_camera).name,0) && isequal(extract_dat.file(setting.current_camera).pathname,0)
extract_dat.file(setting.current_camera).name = 'noname.avi';
extract_dat.file(setting.current_camera).pathname = 'nodir';
end
else
end
extract_dat.file(setting.current_camera).Name = [extract_dat.file(setting.current_camera).pathname extract_dat.file(setting.current_camera).name];
addpath(extract_dat.file(setting.current_camera).pathname);
% check if OK button pressed
if ~isequal(extract_dat.file(setting.current_camera).name,'noname.avi') && ~isequal(extract_dat.file(setting.current_camera).pathname,'nodir')
[pathstr,name,ext,versn] = fileparts([extract_dat.file(setting.current_camera).pathname extract_dat.file(setting.current_camera).name]);
ext_size = size(ext');
if ext_size ~= 4
errordlg('Invalid Image File Extension');
else
% read movie
extract_dat.file(setting.current_camera).movie = mmreader(extract_dat.file(setting.current_camera).name);
% extract_dat.lastFrame = read(extract_dat.movie, inf);
extract_dat.file(setting.current_camera).numFrames = extract_dat.file(setting.current_camera).movie.NumberOfFrames;
-------------------------------------------------------------------------------------------
And these are the codes for the previous and next frame
% --- Executes on button press in pushbutton_reverse.
function pushbutton_reverse_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_reverse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global hfig_extract
global extract_dat
global setting
if extract_dat.file(setting.current_camera).current_frame > 1
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).current_frame - 1;
else
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).numFrames;
end
extract_dat.file(setting.current_camera).frame = read(extract_dat.file(setting.current_camera).movie,extract_dat.file(setting.current_camera).current_frame);
extract_dat.file(setting.current_camera).show = imshow(extract_dat.file(setting.current_camera).frame,'Parent',hfig_extract.axes_preview);
set(hfig_extract.listbox_names,'Value',extract_dat.file(setting.current_camera).current_frame);
set(hfig_extract.text13,'String',num2str(extract_dat.file(setting.current_camera).current_frame));
% --- Executes on button press in pushbutton_forward.
function pushbutton_forward_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_forward (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global hfig_extract
global extract_dat
global setting
if extract_dat.file(setting.current_camera).current_frame < extract_dat.file(setting.current_camera).numFrames
extract_dat.file(setting.current_camera).current_frame = extract_dat.file(setting.current_camera).current_frame + 1;
else
extract_dat.file(setting.current_camera).current_frame = 1;
end
extract_dat.file(setting.current_camera).frame = read(extract_dat.file(setting.current_camera).movie,extract_dat.file(setting.current_camera).current_frame);
extract_dat.file(setting.current_camera).show = imshow(extract_dat.file(setting.current_camera).frame,'Parent',hfig_extract.axes_preview);
set(hfig_extract.listbox_names,'Value',extract_dat.file(setting.current_camera).current_frame);
set(hfig_extract.text13,'String',num2str(extract_dat.file(setting.current_camera).current_frame));