12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- function parse_video_rgb()
- %
- % Parsing video (*.mp4/*.avi) file(s) into image (*.jpg) files
- % Written by Hio-Been Han, 2023-08-23
- %
- %% (1) Get file info & set save directory
- % [file_list, vid_directory] = uigetfile('*.*','MultiSelect','on');
- vid_directory_single = './data_BIDS/stimuli/video/Single/';
- vid_directory_group = './data_BIDS/stimuli/video/Group/';
- if ~isdir('./data_BIDS/stimuli/video_parsed'), mkdir('./data_BIDS/stimuli/video_parsed'); end
- write_directory_single = './data_BIDS/stimuli/video_parsed/Single/';
- write_directory_group = './data_BIDS/stimuli/video_parsed/Group/';
- if ~isdir(write_directory_single), mkdir(write_directory_single); end
- if ~isdir(write_directory_group), mkdir(write_directory_group); end
- file_counter = 1;
- file_list = cell([3,64+8]);
- % Single
- for mouse = 1:8
- for day = 1:2
- for trial = 1:4
- file_list{1,file_counter} = vid_directory_single;
- file_list{2,file_counter} = sprintf('Day%d-Trial%d-Mouse%d.avi',day,trial,mouse);
- file_list{3,file_counter} = write_directory_single;
- file_counter = file_counter + 1;
- end
- end
- end
- % Group
- for day = 1:2
- for trial = 1:4
- file_list{1,file_counter} = vid_directory_group;
- file_list{2,file_counter} = sprintf('Day%d-Trial%d-Group.avi',day,trial);
- file_list{3,file_counter} = write_directory_group;
- file_counter = file_counter + 1;
- end
- end
- %% (2) Parse & save video into JPEG files
- save_format = '.jpg';
- for fileIdx = 1:length(file_list)
-
- % Set save directory
- vid_directory = file_list{1,fileIdx};
- fname = file_list{2,fileIdx};
- img_directory = file_list{3,fileIdx};
- uniqname = fname(1:end-4);
- vidname = [vid_directory fname];
- save_directory = [img_directory uniqname '/'];
- if ~isdir(save_directory), mkdir(save_directory); end
-
- % Read frames
- v = VideoReader( [ vidname ]);
- nFrames = v.Duration * v.FrameRate;
- for frameIdx = 1:7200
- if v.hasFrame
- frame = v.readFrame();
- % Save frame
- imgname_write = [num2str(sprintf('frame-%06d',frameIdx))...
- '-' uniqname save_format];
- imwrite(frame , [ save_directory imgname_write ] )
- end
- end
- end
|