Matlab批量预处理图片(1)

来源:互联网 发布:分析软件测试的复杂性 编辑:程序博客网 时间:2024/06/07 05:28

本Matlab批量预处理图片程序包括:

1、批量对图片重命名;

2、将多个文件夹下面的图片,拷贝到一个文件夹下;

3、给定图片及其中人脸位置,框出人脸

[plain] view plain copy
  1. <span style="font-size:24px;">%功能描述:批量对图片重命名  
  2. %路径格式为,E:\image\(maindir路径下要是子文件夹,不可直接是图片)    
  3.   
  4. clc;    
  5. clear;    
  6.     
  7. maindir='H:\基于CNN的图像分类\绘画图像\敦煌\00000001\';    
  8. name_long=5; %图片名字的长度,如000123.jpg为6,最多9位,可修改    
  9. num_begin=2000; %图像命名开始的数字如000123.jpg开始的话就是123    
  10.     
  11. subdir = dir(maindir);  %dir:列出指定目录下所有子文件夹和文件,不包含下级目录  
  12. n=1;    
  13.     
  14. for i = 1:length(subdir)    
  15.   if ~strcmp(subdir(i).name ,'.') && ~strcmp(subdir(i).name,'..')  %strcmp:比较字符串str1与str2,若完全相等则返回1,不相等返回0  
  16.      subsubdir = dir(strcat(maindir,subdir(i).name));  %strcat:将字符串str1与str2连接,dir  
  17.     for j=1:length(subsubdir)    
  18.          if ~strcmp(subsubdir(j).name ,'.') && ~strcmp(subsubdir(j).name,'..')    
  19.             img=imread([maindir,subdir(i).name,'\',subsubdir(j).name]);  %读取图片  
  20.             imshow(img);    
  21.             str=num2str(num_begin,'%09d');  %将数值转化为字符串  
  22.             newname=strcat(str,'.jpg');    
  23.             newname=newname(end-(name_long+3):end);  %取newname从第end-(name_long+3)个,到最后一个  
  24.             system(['rename ' [maindir,subdir(i).name,'\',subsubdir(j).name] ' ' newname]); %system('rename  filename newfilename'); 这是调用格式  
  25.             num_begin=num_begin+1;    
  26.             fprintf('当前处理文件夹%s',subdir(i).name);    
  27.             fprintf('已经处理%d张图片\n',n);    
  28.             n=n+1;    
  29.             pause(0.1);%可以将暂停去掉    
  30.          end    
  31.     end    
  32.   end    
  33. end  </span>  

[plain] view plain copy
  1. <span style="font-size:24px;">%功能描述:将多个文件夹下面的图片,拷贝到一个文件夹下。  
  2. %train.txt文件包含多个子文件图片的相对地址,如:0--Parade\0_Parade_marchingband_1_849.jpg  
  3.   
  4. clc;  
  5. clear;  
  6. %注意修改下面四个变量  
  7. imgpath='WIDER_train\images\';%图像存放文件夹  
  8. txtpath='WIDER_train\train.txt';%txt文件  
  9. b=0;  
  10. fidin=fopen(txtpath,'r');  %以只读方式打开文件txtpath  
  11. lastname='begin';  
  12.   
  13. while ~feof(fidin)  %文件指针到达文件末尾时 该表达式值为“假”;否则为“真”  
  14.      tline=fgetl(fidin);  %fget1:从文件中读取一行数据  
  15.      str = regexp(tline, ' ','split');  %分割,str:待分割的字符串,' ':分隔符的字符,S:分割结果  
  16.      filepath=[imgpath,str{1}];  %filepath:拼接的文件名    
  17.      a=0;  
  18.       
  19.     if strcmp(str{1},lastname)%如果文件名相等,只需增加object  
  20.         a=a+1;       
  21.     else %如果文件名不等,则需要新建xml  
  22.         copyfile(filepath, 'WIDER_train_img');  %拷贝文件到JPEGImages_val文件夹中     
  23.         b=b+1;  
  24.     end  
  25.     lastname=str{1}; %将文件名赋值给lastname       
  26.     fprintf('已处理%d\n',b);  
  27. end  
  28. fclose(fidin); %关闭文件  
  29. </span>  

[plain] view plain copy
  1. <span style="font-size:24px;">%功能描述:给定图片及其中人脸位置,框出人脸  
  2. %train.txt:0--Parade\0_Parade_Parade_0_904.jpg 360.918 97.9235 262.995 338.536   
  3.   
  4. clc;  
  5. clear;  
  6. imgpath='WIDER_train\images\';%图像存放文件夹  
  7. txtpath='WIDER_train\train.txt';%txt文件  
  8. fidin=fopen(txtpath,'r');  
  9.   
  10. while ~feof(fidin)  %文件指针到达文件末尾时 该表达式值为“假”;否则为“真”  
  11.      tline=fgetl(fidin);  %fget1:从文件中读取一行数据  
  12.      str = regexp(tline, ' ','split');  %分割,str:待分割的字符串,' ':分隔符的字符,S:分割结果  
  13.      filepath=[imgpath,str{1}];  %filepath:拼接的文件名  
  14.      img=imread(filepath);  %读取图片  
  15.      imshow(img);  
  16.      %rectangle('Position',[x,y,w,h]),LineWidth:线条粗细4,EdgeColor:边框颜色是红色  
  17.      rectangle('Position',[str2double(str{2}),str2double(str{3}),str2double(str{4}),str2double(str{5})],'LineWidth',4,'EdgeColor','r');  
  18.      pause(0.1);   %pause(n):程序暂停n秒后继续  
  19. end  
  20. fclose(fidin);</span>  
0 0
原创粉丝点击