请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 8191|回复: 2

训练样本切分小图

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2019-5-16 22:40:33 | 显示全部楼层 |阅读模式
训练样本切分小图

(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文件夹】;


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
 楼主| 发表于 2019-5-16 22:44:42 | 显示全部楼层
第一:切分图像:
  1. clc,clear, close all
  2. warning off
  3. FilePath='../data/train';
  4. fileList = dir(FilePath);
  5. savePath='../data/train2';
  6. delete([savePath, '/', '*.**']);
  7. for i=1:length(fileList)
  8.     if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
  9.         continue;
  10.     else
  11.         fileList(i).name;
  12.         img = imread([FilePath,'/',fileList(i).name]);
  13.         % Crop Parts
  14.         CropImagesPartsFun(img, fileList(i).name, 256, 256, 150, savePath);
  15.     end
  16. end
  17. disp('切分完成!!!');
复制代码


相应地的切分函数为:
  1. function CropImagesPartsFun(img, imageName, newW, newH, OverlapRate, savePath)
  2. %         imageName=fileList(i).name;
  3. %         newW=256;
  4. %         newH=256;
  5. %         OverlapRate=200;
  6. [w,h,d]=size(img);
  7. img1 = zeros(newW,newH,d);
  8. k=0;
  9. for i=1:OverlapRate:w-1
  10.     for j=1:OverlapRate:h-1
  11.         iW=i+newW-1;
  12.         jH=j+newH-1;
  13.         if(iW>w)
  14.             iW=w;
  15.         end
  16.         if(jH>h)
  17.             jH=h;
  18.         end
  19.         img1 = img(iW-newW+1:iW,jH-newH+1:jH,d);
  20.         k=k+1;
  21.         % .png图像
  22.         if(length(imageName)>=8)
  23.             if(isequal(imageName(end-7:end),'mask.png'))
  24.                 imageNewName = [imageName(1:end-8), num2str(k) , '_mask.png'];
  25.                 imwrite(img1, [savePath,'/',imageNewName])
  26.             elseif(isequal(imageName(end-3:end),'.png'))
  27.                 imageNewName = [imageName(1:end-4), num2str(k) , '.png'];
  28.                 imwrite(img1, [savePath,'/',imageNewName])
  29.             end
  30.         elseif(length(imageName)>4 && length(imageName)<8)
  31.             imageNewName = [imageName(1:end-4),'_', num2str(k) , '.png'];
  32.             imwrite(img1, [savePath,'/',imageNewName])
  33.         end
  34.         
  35.     end
  36. end
复制代码


回复 支持 反对

使用道具 举报

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
 楼主| 发表于 2019-5-16 22:45:47 | 显示全部楼层
第二:将切分的小图合并为原始大图:
主函数如下:
  1. clc,clear, close all
  2. warning off
  3. FilePath='../data/train2';
  4. fileList = dir(FilePath);
  5. savePath='../data/train3';
  6. delete([savePath, '/', '*.**']);
  7. % 提取唯一出现的名称ID
  8. fileList2=[];
  9. for i=1:length(fileList)
  10.     if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1
  11.         continue;
  12.     else
  13.         imageName=fileList(i).name;
  14.         if(isequal(imageName(end-5:end),'_1.png'))
  15.             fileList2 = [fileList2; imageName(1:end-6)];
  16.         end
  17.     end
  18. end
  19. % 根据图像ID名称,结合切分的小图,进行大图合图
  20. for i=1:length(fileList2)
  21.     imageName=fileList2(i,:);
  22.     imagePathName = [FilePath, '/', imageName];
  23.     % Merge Parts
  24.     newW=256;          % 小图宽
  25.     newH=256;          % 小图高
  26.     OverlapRate=150;   % 大图切分小图时,重叠大小
  27.     w=512;             % 大图宽
  28.     h=512;             % 大图高
  29.     MergeImagesPartsFun( FilePath, imageName, newW, newH, OverlapRate, w, h, savePath);
  30. end
  31. disp('合图完成!!!');
复制代码


MergeImagesPartsFun函数如下:
  1. function MergeImagesPartsFun( FilePath, imageName, newW,newH, OverlapRate, w,h, savePath)
  2. %         imageName=fileList(i).name; 大图唯一ID名称
  3. %         newW=256; 小图宽
  4. %         newH=256; 小图高
  5. %         OverlapRate=200; 大图切分小图所用的重叠大小
  6. % knums:  小图数量
  7. % w: 大图宽
  8. % h: 大图高
  9. k=0;
  10. for i=1:OverlapRate:w-1
  11.     for j=1:OverlapRate:h-1
  12.         iW=i+newW-1;
  13.         jH=j+newH-1;
  14.         if(iW>w)
  15.             iW=w;
  16.         end
  17.         if(jH>h)
  18.             jH=h;
  19.         end
  20.         % .png图像
  21.         k=k+1;
  22.         imageNewName = [FilePath, '/', imageName,'_', num2str(k) , '.png'];
  23.         img1 = imread( imageNewName );
  24.         img(iW-newW+1:iW,jH-newH+1:jH, :) = img1;
  25.     end
  26. end
  27. % 保存图像
  28. imwrite(img, [savePath,'/', [imageName, '.png']])
  29. clear im1 img
复制代码




回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-29 00:20 , Processed in 0.209836 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表