|
楼主 |
发表于 2019-5-16 22:44:42
|
显示全部楼层
第一:切分图像:
- clc,clear, close all
- warning off
- FilePath='../data/train';
- fileList = dir(FilePath);
- savePath='../data/train2';
- delete([savePath, '/', '*.**']);
- for i=1:length(fileList)
- if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
- continue;
- else
- fileList(i).name;
- img = imread([FilePath,'/',fileList(i).name]);
- % 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;
- [w,h,d]=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 = [imageName(1:end-8), num2str(k) , '_mask.png'];
- imwrite(img1, [savePath,'/',imageNewName])
- elseif(isequal(imageName(end-3:end),'.png'))
- imageNewName = [imageName(1:end-4), num2str(k) , '.png'];
- imwrite(img1, [savePath,'/',imageNewName])
- end
- elseif(length(imageName)>4 && length(imageName)<8)
- imageNewName = [imageName(1:end-4),'_', num2str(k) , '.png'];
- imwrite(img1, [savePath,'/',imageNewName])
- end
-
- end
- end
复制代码
|
|