训练样本切分小图
训练样本切分小图(1)这个【train文件夹】,含有000.png, 001.png, 002.png, ……, 999.png;以及对应的标签000_mask.png, 001_mask.png, 002_mask.png, ……, 999_mask.png. 图像大小为512x512;
(2)目的:第一:将文件夹train,将图像依次切分为256x256大小,得到【train2文件夹】;
第二:将得到的256x256大小图像的train2文件夹,合并为512x512大小的图像,得到【train3文件夹】;
第一:切分图像:
clc,clear, close all
warning off
FilePath='../data/train';
fileList = dir(FilePath);
savePath='../data/train2';
delete();
for i=1:length(fileList)
if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
continue;
else
fileList(i).name;
img = imread();
% Crop Parts
CropImagesPartsFun(img, fileList(i).name, 256, 256, 150, savePath);
end
end
disp('切分完成!!!');
相应地的切分函数为:
function CropImagesPartsFun(img, imageName, newW, newH, OverlapRate, savePath)
% imageName=fileList(i).name;
% newW=256;
% newH=256;
% OverlapRate=200;
=size(img);
img1 = zeros(newW,newH,d);
k=0;
for i=1:OverlapRate:w-1
for j=1:OverlapRate:h-1
iW=i+newW-1;
jH=j+newH-1;
if(iW>w)
iW=w;
end
if(jH>h)
jH=h;
end
img1 = img(iW-newW+1:iW,jH-newH+1:jH,d);
k=k+1;
% .png图像
if(length(imageName)>=8)
if(isequal(imageName(end-7:end),'mask.png'))
imageNewName = ;
imwrite(img1, )
elseif(isequal(imageName(end-3:end),'.png'))
imageNewName = ;
imwrite(img1, )
end
elseif(length(imageName)>4 && length(imageName)<8)
imageNewName = ;
imwrite(img1, )
end
end
end
第二:将切分的小图合并为原始大图:
主函数如下:
clc,clear, close all
warning off
FilePath='../data/train2';
fileList = dir(FilePath);
savePath='../data/train3';
delete();
% 提取唯一出现的名称ID
fileList2=[];
for i=1:length(fileList)
if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
continue;
else
imageName=fileList(i).name;
if(isequal(imageName(end-5:end),'_1.png'))
fileList2 = ;
end
end
end
% 根据图像ID名称,结合切分的小图,进行大图合图
for i=1:length(fileList2)
imageName=fileList2(i,:);
imagePathName = ;
% Merge Parts
newW=256; % 小图宽
newH=256; % 小图高
OverlapRate=150; % 大图切分小图时,重叠大小
w=512; % 大图宽
h=512; % 大图高
MergeImagesPartsFun( FilePath, imageName, newW, newH, OverlapRate, w, h, savePath);
end
disp('合图完成!!!');
MergeImagesPartsFun函数如下:
function MergeImagesPartsFun( FilePath, imageName, newW,newH, OverlapRate, w,h, savePath)
% imageName=fileList(i).name; 大图唯一ID名称
% newW=256; 小图宽
% newH=256; 小图高
% OverlapRate=200; 大图切分小图所用的重叠大小
% knums:小图数量
% w: 大图宽
% h: 大图高
k=0;
for i=1:OverlapRate:w-1
for j=1:OverlapRate:h-1
iW=i+newW-1;
jH=j+newH-1;
if(iW>w)
iW=w;
end
if(jH>h)
jH=h;
end
% .png图像
k=k+1;
imageNewName = ;
img1 = imread( imageNewName );
img(iW-newW+1:iW,jH-newH+1:jH, :) = img1;
end
end
% 保存图像
imwrite(img, ])
clear im1 img
页:
[1]