MATLAB: 图像批量剪切
2016-10-27 19:33
246 查看
MATLAB图像批量剪切
Github: loper-eswai/MagicLab问题描述
源文件夹下有若干子文件夹,包含不同类别的图片文件。现要求将每个图片剪切成相同大小,保存在目标文件夹。解决方案
参数设置:- src_dir: 源文件夹 [字符串]
- dst_dir: 目标文件夹 [字符串]
- format: 图片文件格式 [字符串]
- spacing: 剪切间隔,即小图片的大小为spacing × spacing
function ImageCrop(src_dir,dst_dir,format,spacing) subfolders=dir(src_dir); for ii=1:length(subfolders) subname=subfolders(ii).name; % ignore current dir and father dir if ~strcmp(subname,'.')&&~strcmp(subname,'..') frames=dir(fullfile(src_dir,subname,['*.',format])); imgnum=length(frames); dstpath=fullfile(dst_dir,subname); if ~isdir(dstpath) mkdir(dstpath); end for jj=1:imgnum imgpath=fullfile(src_dir,subname,frames(jj).name); I=imread(imgpath); row=floor(size(I,1)/spacing); col=floor(size(I,2)/spacing); % Start Croping for rr=1:row for cc=1:col rect=[(cc-1)*spacing+1,(rr-1)*spacing+1,spacing-1,spacing-1]; newI=imcrop(I,rect); newname=[frames(jj).name,'_',num2str((rr-1)*col+cc),['.',format]]; newpath=fullfile(dstpath,newname); imwrite(newI,newpath); end end end end % end of if end end
p.s. 如果和您的需求有差异请自行修改哦~
相关文章推荐
- Matlab批量修改图像获得指定区域
- 如何使用MATLAB对图像进行批量的修改名称的处理
- matlab中批量读取图像序列并进行简单目标跟踪
- matlab 图像批量修改图像大小
- matlab 图像批量修改图像大小
- matlab 图像剪切
- matlab 图像批量修改图像大小
- 【3.10】matlab中批量读取图像
- Matlab关于批量读取和存储图像这类问题的经验分享
- matlab 对一个文件夹里的所有图像进行批量旋转90度并保存
- 用matlab对图像进行批量旋转
- matlab 之批量保存图像,再论 imwrite print saveas
- matlab 批量保存图像的方法
- matlab对图像进行批量裁剪并保存到特定文件夹
- Matlab图像处理常用语句(1)--批量读取文件夹内图像
- Matlab GUI 图像矩形剪切,并存储、发布
- MATLAB读取文件夹中的批量图像
- Matlab图像剪切,超出图像区域指定颜色填充
- Matlab 对图像的读写,显示,保存,局部图像剪切,替换的操作
- 二、Matlab图像的读取和存储