Convert batch images and save them as mat files

Matlab converts batch images into struct structures and saves them as mat files for neural network training.

Look directly at the code:

%load('../mask/mask_20.mat')  %导入相关路径
folder_path = 'BSDS300\images\train\' %图片所在文件夹位置,末尾'\'别丢了。

file_list = dir([folder_path '*.jpg']);  %根据图片类型(jpg/png)修改
for i = 1:length(file_list) 
    % 读取图片 
    img = imread([folder_path file_list(i).name]);
    
    %------图片处理操作,根据需要相应增改---------------
    img = imresize(img,[256,256]);  %裁剪成指定大小(256)  
    %img = rgb2gray(img);            %彩色图转换成灰度图
    %img= double(img);               %更改成double类型
    %-------------------------------------------------
	
	
    train = (double(fft2(img)));  %训练样本处理。(这里对图片做傅里叶变换,作为训练集)
    label = double(img)/255;      %标签。(这里将原始图片作为标签)
    
    data = struct('train', train, 'label', label);  %将训练样本和标签放到名为【data】的结构体中。
    save([strcat(num2str(i),'.mat')], 'data');      %保存为mat文件放入当前文件夹。
   
end

Final effect:
insert image description here
insert image description here

Sometimes it is necessary to realize that data contains a data field, and then store the train and label in the data field, you can use the above paragraph:

data = struct('train', train, 'label', label);

Just replace it with the following.

data.data = struct('label', label,'train', train );

Guess you like

Origin blog.csdn.net/m0_46366547/article/details/129786168