Matlab script for automatically cut/crop images
This code was created two years ago,It can be used to cut the image automatically with a batch script and the size of the frame can be manually definied as you wish.
%% 从new_name文件夹下读取图片; currentFolder=pwd; str=dir([currentFolder,'\*bmp']); n=length(str); xn_new=str(n).name; %% 手动获取裁剪范围,并且返回参数 one_xn=imread(xn_new); [I2,rect]=imcrop(one_xn); title('Capture success,Please close the figure!'); % 延迟2秒后自动关闭 pause(2); close all; fprintf('Successful capture the position!\n'); fprintf('Wait one minute ,Cropping!\n'); %% 开始裁剪并保存到新的文件夹下面 cut_folder=[currentFolder,'\new_cut']; mkdir(cut_folder); %% 开始批量操作 for i=1:n temp_name=str(i).name; temp_bmp=imread(temp_name); new_cut_bmp=imcrop(temp_bmp,rect); %% 生成文件名 a=sprintf('%04d',i);%类似0001命名的文件 xa=num2str(a);%转为字符串 xb=char('.bmp'); xs='\new_cut\'; xc=strcat(currentFolder,xs,xa,xb);%001.bmp %% 保存 imwrite(new_cut_bmp,xc,'bmp') % imwrite(new_cut_bm,fullfile(writedir,[imnames(imidx).name(1:end-3),writetype])); end fprintf('Successful output the crop picture!\n');
For the OS Platform of Linux , 因天有不测风云,注释被吃,其实就是把路径符号“\”换成“/” 即可~:
%% ??new_name????????????????; currentFolder=pwd; str=dir([currentFolder,'/*png']); n=length(str); xn_new=str(n).name; %% ?????????????????????????????? one_xn=imread(xn_new); [I2,rect]=imcrop(one_xn); title('Capture success,Please close the figure!'); % ????2???????????? pause(2); close all; fprintf('Successful capture the position!\n'); fprintf('Wait one minute ,Cropping!\n'); %% ?????????????????????????????? cut_folder=[currentFolder,'/new_cut']; mkdir(cut_folder); %% ???????????? for i=1:n temp_name=str(i).name; temp_bmp=imread(temp_name); new_cut_bmp=imcrop(temp_bmp,rect); %% ?????????? a=sprintf('%04d',i);%????0001?????????? xa=num2str(a);%?????????? xb=char('.bmp'); xs='/new_cut/'; xc=strcat(currentFolder,xs,xa,xb);%001.bmp %% ???? imwrite(new_cut_bmp,xc,'bmp') % imwrite(new_cut_bm,fullfile(writedir,[imnames(imidx).name(1:end-3),writetype])); end fprintf('Successful output the crop picture!\n');%% ??new_name????????????????; currentFolder=pwd; str=dir([currentFolder,'/*png']); n=length(str); xn_new=str(n).name; %% ?????????????????????????????? one_xn=imread(xn_new); [I2,rect]=imcrop(one_xn); title('Capture success,Please close the figure!'); % ????2???????????? pause(2); close all; fprintf('Successful capture the position!\n'); fprintf('Wait one minute ,Cropping!\n'); %% ?????????????????????????????? cut_folder=[currentFolder,'/new_cut']; mkdir(cut_folder); %% ???????????? for i=1:n temp_name=str(i).name; temp_bmp=imread(temp_name); new_cut_bmp=imcrop(temp_bmp,rect); %% ?????????? a=sprintf('%04d',i);%????0001?????????? xa=num2str(a);%?????????? xb=char('.bmp'); xs='/new_cut/'; xc=strcat(currentFolder,xs,xa,xb);%001.bmp %% ???? imwrite(new_cut_bmp,xc,'bmp') % imwrite(new_cut_bm,fullfile(writedir,[imnames(imidx).name(1:end-3),writetype])); end fprintf('Successful output the crop picture!\n');
另外附一个自动重命名的脚本,可以批量自动重命名,有时候会用到
%% 获取当前路径 currentFolder=pwd; str=dir([currentFolder,'\*bmp']); %str=dir('D:\matlab_new\picture\*bmp'); n=length(str); %% 创建新命名的文件夹 newfolder=[currentFolder,'\new_name']; mkdir(newfolder); %% 批量循环开始 for i=1:n x1=str(i).name; a=sprintf('%04d',i);%类似0001命名的文件 x3=num2str(a);%转为字符串 x4=char('.bmp'); x5=strcat(x3,x4);%001.bmp copyfile([currentFolder,'\',x1],[newfolder,'\',x5]);%复制到新的 % movefile(['D:\matlab_new\picture\' x1],['D:\matlab_new\picture\' %x5]);%直接替换 end