我正在尝试从一个文件夹中读取图像,并将新图像保存在不同名称的不同文件夹中。我需要用不同的名称来命名它们,因为它们是用不同的affine2d值生成的。例如,a1文件夹中的图像将是aa1、aa2、aa3、aa4、aa5……,a2文件夹中的图片将是bb1、bb2、bb3……有办法做到这一点吗?
''
srcFiles = dir('C:UsersmstfyDesktopMatlabalexTrain1Person1*.jpg');
for i = 1 : length(srcFiles)
filename = strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1',srcFiles(i).name);
im = imread(filename);
aform = affine2d([1 0 0; .1 1 0; 0 0 1])
bform = affine2d([1 0 0; .2 1 0; 0 0 1])
cform = affine2d([1 0 0; .3 1 0; 0 0 1])
dform = affine2d([1 0 0; .4 1 0; 0 0 1])
eform = affine2d([1 0 0; .5 1 0; 0 0 1])
fform = affine2d([1 0 0; .6 1 0; 0 0 1])
gform = affine2d([1 0 0; .7 1 0; 0 0 1])
hform = affine2d([1 0 0; .8 1 0; 0 0 1])
jform = affine2d([1 0 0; .9 1 0; 0 0 1])
a = imwarp(im,aform);
b = imwarp(im,bform);
c = imwarp(im,cform);
d = imwarp(im,dform);
e = imwarp(im,eform);
f = imwarp(im,fform);
g = imwarp(im,gform);
h = imwarp(im,hform);
j = imwarp(im,jform);
newfilename=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a1',srcFiles(i).name);
newfilename1=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a2',srcFiles(i).name);
newfilename2=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a3',srcFiles(i).name);
newfilename3=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a4',srcFiles(i).name);
newfilename4=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a5',srcFiles(i).name);
newfilename5=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a6',srcFiles(i).name);
newfilename6=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a7',srcFiles(i).name);
newfilename7=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a8',srcFiles(i).name);
newfilename8=strcat('C:UsersmstfyDesktopMatlabalexTrain1Person1a9',srcFiles(i).name);
imwrite(a,newfilename,'jpg');
imwrite(b,newfilename1,'jpg');
imwrite(c,newfilename2,'jpg');
imwrite(d,newfilename3,'jpg');
imwrite(e,newfilename4,'jpg');
imwrite(f,newfilename5,'jpg');
imwrite(g,newfilename6,'jpg');
imwrite(h,newfilename7,'jpg');
imwrite(j,newfilename8,'jpg');
end
''
我不太确定你想要什么,但我觉得,如果你更多地考虑外观和更复杂的结构,你的代码可能会更自动化(尽管我现在只使用了3D矩阵;(
% build a 3D matrix to store all transformation matrices
Mat = cat(3,[1 0 0; .1 1 0; 0 0 1],...
[1 0 0; .2 1 0; 0 0 1],...
[1 0 0; .3 1 0; 0 0 1],...
[1 0 0; .4 1 0; 0 0 1],...
[1 0 0; .5 1 0; 0 0 1],...
[1 0 0; .6 1 0; 0 0 1],...
[1 0 0; .7 1 0; 0 0 1],...
[1 0 0; .8 1 0; 0 0 1],...
[1 0 0; .9 1 0; 0 0 1]);
srcFiles = dir('C:UsersmstfyDesktopMatlabalexTrain1Person1*.jpg');
for i = 1:length(srcFiles)
% load file (use fullfile() + the path from the list to make this
% generic for every platform)
path2img = fullfile(srcFiles(i).folder,srcFiles(i).name);
% read image
img = imread(path2img);
for j = 1:size(Mat,3)
% transform image
form = affine2d(Mat(:,:,j));
img_trf = imwarp(img,form);
%% save image
% folder name
fldrNm = num2str(j,'a%d');
path2fldr = fullfile(srcFiles(i).folder,fldrNm);
% create new folder if it does not exist yet
if i == 1 && exist(path2fldr,'dir') ~=7
mkdir(path2fldr)
end
path2save = fullfile(path2fldrsrcFiles(i).name);
% store new image
imwrite(img_trf,path2save,'jpg');
end
end
事实上,如果你的转换矩阵遵循这种模式,你甚至可以自动生成它:
% allocate memory
NumMat = 100;
Mat = NaN(3,3,NumMat);
for i = 1:NumMat
Mat(:,:,i) = [1 0 0; i/10 1 0; 0 0 1];
end
如果你想节省最短的时间,你可以预先保存到affine2d
,但这需要将其保存到结构数组或单元数组,我不知道你是否已经做到了这一点;(
听起来是使用循环的完美场所。
rootdir='C:UsersmstfyDesktopMatlabalexTrain1Person1';
srcFiles = dir([rootdir filesep '*.jpg']);
for i = 1 : length(srcFiles)
filename = [rootdir filesep srcFiles(i).name];
im = imread(filename);
for j=1:9
xform = affine2d([1 0 0; .1*j 1 0; 0 0 1]);
newim = imwarp(im,xform);
newfilename= [rootdir filesep sprintf('a%d',j) filesep srcFiles(i).name];
imwrite(newim,newfilename,'jpg');
end
end