Matlab批量预处理图片

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

1、批量对图片重命名;


<span style="font-size:18px;"><span style="font-size:24px;">%功能描述:批量对图片重命名
%路径格式为,G:\faces\(maindir路径下要是子文件夹,不可直接是图片)  

close all; clear; clc;
maindir='G:\faces\';    
name_long=4; %图片名字的长度,如000123.jpg为6,最多9位,可修改    
num_begin=123; %图像命名开始的数字如0123.jpg开始的话就是123    
    
subdir = dir(maindir);  %dir:列出指定目录下所有子文件夹和文件,不包含下级目录  
n=1;    
    
for i = 1:length(subdir)    
  if ~strcmp(subdir(i).name ,'.') && ~strcmp(subdir(i).name,'..')  %strcmp:比较字符串str1与str2,若完全相等则返回1,不相等返回0  
     subsubdir = dir(strcat(maindir,subdir(i).name));  %strcat:将字符串str1与str2连接,dir  
    for j=1:length(subsubdir)    
         if ~strcmp(subsubdir(j).name ,'.') && ~strcmp(subsubdir(j).name,'..')    
            img=imread([maindir,subdir(i).name,'\',subsubdir(j).name]);  %读取图片  
           % imshow(img);    
            str=num2str(num_begin,'%09d');  %将数值转化为字符串  
            newname=strcat(str,'.jpg');  
            newname=newname(end-(name_long+3):end);  %取newname从第end-(name_long+3)个,到最后一个  
            system(['rename ' [maindir,subdir(i).name,'\',subsubdir(j).name] ' ' newname]); %system('rename  filename newfilename'); 这是调用格式  
            num_begin=num_begin+1;    
            fprintf('当前处理文件夹%s',subdir(i).name);    
            fprintf('已经处理%d张图片\n',n);    
            n=n+1;    
           % pause(0.1);%可以将暂停去掉    
         end    
    end    
  end    
end 
 </span></span>

2、给定图片及其中人脸位置,框出人脸或行人

<span style="font-size:18px;"><span style="font-size:24px;">%功能描述:给定图片及其中人脸位置,框出人脸
%train.txt:0--Parade\0_Parade_Parade_0_904.jpg 360.918 97.9235 262.995 338.536 

clc;
clear;
imgpath='WIDER_train\images\';%图像存放文件夹
txtpath='WIDER_train\train.txt';%txt文件
fidin=fopen(txtpath,'r');

while ~feof(fidin)  %文件指针到达文件末尾时 该表达式值为“假”;否则为“真”
     tline=fgetl(fidin);  %fget1:从文件中读取一行数据
     str = regexp(tline, ' ','split');  %分割,str:待分割的字符串,' ':分隔符的字符,S:分割结果
     filepath=[imgpath,str{1}];  %filepath:拼接的文件名
     img=imread(filepath);  %读取图片
     imshow(img);
	 %rectangle('Position',[x,y,w,h]),LineWidth:线条粗细4,EdgeColor:边框颜色是红色
     rectangle('Position',[str2double(str{2}),str2double(str{3}),str2double(str{4}),str2double(str{5})],'LineWidth',4,'EdgeColor','r');
     pause(0.1);   %pause(n):程序暂停n秒后继续
end
fclose(fidin);</span></span>

3.同一幅图像上同时框出所有目标(对2的改进,感谢张大神的指导,temp变量保存上一行的图像标号)

clc;
clear;
imgpath='E:\。。\。。\。。\。。\。。\';%图像存放文件夹
txtpath='D:\dd\detections\。。.txt';%txt文件
fidin=fopen(txtpath,'r');

temp =-1;
while ~feof(fidin)  %文件指针到达文件末尾时 该表达式值为“假”;否则为“真”
    tline=fgetl(fidin);  %fget1:从文件中读取一行数据
    str = regexp(tline, ',','split');  %分割,str:待分割的字符串,' ':分隔符的字符,S:分割结果
    if( str2double(str{6})>0.5)
        t=str2double(str{1})-1;
        %filepath=sprintf('tu\\ff\\%06d.png',t);
        filepath=sprintf('E:\\。。\\。。\\data\\testing\\。。\\%06d.png',t);
        %filepath=[imgpath,str{1},'.png'];  %filepath:拼接的文件名
        if temp ~= t
            img=imread(filepath);  %读取图片
            imshow(img);
        end
        temp =t;
        %rectangle('Position',[x,y,w,h]),LineWidth:线条粗细4,EdgeColor:边框颜色是红色
        rectangle('Position',[str2double(str{2}),str2double(str{3}),str2double(str{4}),str2double(str{5})],'LineWidth',4,'EdgeColor','r');
        title(t);
        pause(0.8);   %pause(n):程序暂停n秒后继续
    end
end
fclose(fidin);





  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值