加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Matlab中实现批量处理图像

(2011-01-09 09:11:21)
标签:

杂谈

分类: Matlab

有时候,我的采集到了很多的图像,但是图像并不是我们想要的,即图像还要经过一系列的转换才是我们要的,于是,你就处理采集到的图像,采集到了100张图像,你需要100次处理,每一次都需要你打开图像、处理和保存图像,显而易见,这种方法很是麻烦,于是,你就想可不可以批量处理啊……哈哈……你对了,可以的……批量转换图像格式为例,具体程序如下:

clc;clear;
pathname=uigetdir(cd,'请选择文件夹');
if pathname==0
    msgbox('您没有正确选择文件夹');
    return;
end

% 可以打开几乎所有的图像类型,哈哈……全了……
filesbmp=ls(strcat(pathname,'\*.bmp'));
filesjpg=ls(strcat(pathname,'\*.jpg'));
filesjpeg=ls(strcat(pathname,'\*.jpeg'));
filesgif=ls(strcat(pathname,'\*.gif'));
filestif=ls(strcat(pathname,'\*.tif'));
filespng=ls(strcat(pathname,'\*.png'));
files=[cellstr(filesbmp);cellstr(filesjpg);...
    cellstr(filesjpeg);cellstr(filesgif);...
    cellstr(filestif);cellstr(filespng)];
len=length(files);
flag=[];

% 开始批量处理图像,转换格式
for ii=1:len
    if strcmp(cell2mat(files(ii)),'')
        continue;
    end
    Filesname{ii}=strcat(pathname,'\',files(ii));
    page{ii}=imread(cell2mat(Filesname{ii}));
    if length(size(page{ii}))==3 %图像为彩色RGB,进行转换
        page1{ii}=rgb2gray(page{ii});
        flag=[flag ii]; %用于存储被处理图像的在数组举证中的索引
    end   
end
% 批量保存转换后的图片
for ii=1:length(flag)
    fname_temp=cell2mat(Filesname{flag(ii)});
    dot=strfind(fname_temp,'.');
    fname_temp=fname_temp(1:dot(end)-1);
    FileName=strcat(fname_temp,'彩色TO灰度.jpg');
    imwrite(page1{flag(ii)},FileName );
end

success……哈哈……

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有