clear clc pathname = uigetdir; name_list=dir(pathname); for i=3:22 name_list(i).num=zeros(20,1); name_list(i).num(i-2)=1; end %pathname = uigetdir; img_name1=importdata('train_30_32.txt'); img_train_num=size(img_name1,1); num=0; for i=1:img_train_num img_name_char=cell2mat(img_name1(i)); label_1(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_1(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); train_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata1(:,num)=temp; end for i=1:img_train_num for j=1:20 if strcmp(name_list(j+2).name,label_1(i).name) label_1(i).num=name_list(j+2).num; end end train_y(:,i)=uint8(label_1(i).num); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %pathname = uigetdir; img_name1=importdata('test_30_32.txt'); img_test_num=size(img_name1,1); num=0; for i=1:img_test_num img_name_char=cell2mat(img_name1(i)); label_2(i).name=extractBefore(cell2mat(img_name1(i)),'_'); imgPath = [pathname,'\',label_2(i).name,'\',cell2mat(img_name1(i))]; temp = imread(imgPath); test_x(:,:,i)=temp; % temp = double(temp(:)); % num=num+1; % imagedata2(:,num)=temp; end for i=1:img_test_num for j=1:20 if strcmp(name_list(j+2).name,label_2(i).name) label_2(i).num=name_list(j+2).num; end end test_y(:,i)=uint8(label_2(i).num); end save('E:\\imgdata_uint8.mat', 'train_x','train_y','test_x','test_y');如何修改程序使得在一个文件下直接读取图片
时间: 2024-02-07 07:02:16 浏览: 84
clc.rar_clc_matlab 中值滤波_中值_中值滤波 S函数_中值滤波 matlab
要在一个目录下直接读取图片,只需要修改 `imgPath` 变量的赋值语句即可。将其改为:
```matlab
imgPath = [pathname,'\',cell2mat(img_name1(i))];
```
这样,`imgPath` 将直接指向指定目录下的图像文件。同时,为了避免与其他文件混淆,建议将训练集和测试集的图像文件放在不同的文件夹中,以便于区分。
阅读全文