📄 util_img2mat.m
字号:
function util_img2mat (img_dir, target_dir, varargin)% FUNCTION util_img2mat (img_dir, target_dir, varargin)%% Convert (and resize) the images into mat files, with the naming% convention of 'r_i00x.mat', so that main_model program can% process them. The extra arguements are:% varargin{1} = stimulus size (eg. [256 128])% default: the first image size.% varargin{2} = image format (eg. 'pgm')% default: 'pgm'.% For example, running% util_img2mat('Raw','Test',[256 256],'jpg');% will take all jpg images in Raw/*.jpg, and resize them in% 256x256, and save in Test/r_i00x.mat formats. % If the images need to be normalized (mean subtraction, etc),% uncomment the commands within the code.%% 05/25/06: Created.% 07/17/06: Added normalization part (commented).if ~exist(img_dir,'dir') error (['No image directory named: ' img_dir]);else % Get extra arguements: image format and image size. if length (varargin) > 1 img_format = varargin{2}; else img_format = 'pgm'; end D = dir([img_dir '/*' img_format]); if length (varargin) > 0 stim_size = varargin{1}; else stim_size = size(imread([img_dir '/' D(1).name])); end % is everything okay with the target directory? if ~exist(target_dir, 'dir') mkdir(target_dir); elseif length(dir([target_dir '/*.mat'])) > 0 error('Target directory not empty'); end fid = fopen([target_dir '/file_list.txt'], 'w'); for i = 1:length(D) stim_fn = [img_dir '/' D(i).name]; disp(stim_fn); % We write file_list.txt in the target directory. fprintf(fid, '%s\n', stim_fn); stim_img = imread(stim_fn); stim_img = double(stim_img)/255; stim_img = imresize(stim_img, stim_size); % Make the stimulus mean-zero and [0 1]. %stim_img = (stim_img-mean(mean(stim_img))); %max_val = max(max(stim_img)); %min_val = min(min(stim_img)); %stim_img = (stim_img-min_val)/(max_val-min_val); r = stim_img; % build file name max_num_zero = length(num2str(length(D))); save_file_name = repmat('0', [1 max_num_zero-length(num2str(i))]); save_file_name = ['r_i' save_file_name num2str(i)]; save_file_name = [target_dir '/' save_file_name]; save(save_file_name, 'r'); end %save ([target_dir '/stim_size.mat'], 'stim_size'); fclose(fid);end% Now examine all the files.%util_view_img_files(target_dir);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -