MATLAB 批量处理图片

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier; color: #25992d }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier; min-height: 12.0px }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier; color: #b245f3 }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier; color: #0433ff }
span.s1 { color: #0433ff }
span.s2 { color: #b245f3 }
span.s3 { color: #000000 }
span.s4 { color: #25992d }

function resizephotos(directory, wh, isrecursive, isoverwrite, savetopath, supportFormat)
% resizephotos: resize a batch of photos
%
% resizephotos -dir max_width_and_height, will let you choose a directory,
% and resize all the photos in the directory. When max_width_and_height is
% omitted, it uses 1600 by default(1600px is enough for most monitors).
%
% resizephotos -files max_width_and_height, will let you choose some files,
% and resize all the files in the directory.
%
% You might be asked for whether to recursively resize all the photos in
% subdirectories.
%
% and You might be asked for whether to overwrite the original files. If
% you choose ‘No‘, the program will save the resized images in new
% directories or files with prefix ‘resize-‘.
%
% resizephotos(file_or_directory, max_width_and_height, is_recursive, ...
% is_overwrite, path_to_save): you can use the function by specifying
% the path of the directories and files. When using this method, you can
% specific the max width and height of the resized photos. When path_to_save
% is omitted, the path to save the resized photos will generated automatically
% depends on is_overwrite parameter.
%
%
% See also imresize
%
% Copyright 2010, zhiqiang.org
% author: [email protected], url:
% http://zhiqiang.org/blog/it/batch-resize-images-using-matlab.html
%   $Revision: 1.1.6.28.2.1 $  $Date: 2009/01/27 04:47:43 $

%% cope with the input parameters
% if the directory is not set, we open a file select dialog for use to
% select the files or path to deal with
if nargin == 0 || (nargin >= 1 && ischar(directory) && strcmp(directory, ‘-dir‘))
    directory = uigetdir;
    % let user to choose whether recursively resize all the subdirectories
    tmp = questdlg(‘Did you want to recursively resize all the subdirectories?‘);
    if strcmp(‘Yes‘, tmp)
        isrecursive = true;
    else strcmp(‘No‘, tmp)
        isrecursive = false;
    end
    isoverwrite = askforoverwrite();
elseif (nargin >= 1 && ischar(directory) && strcmp(directory, ‘-files‘))
    [p, f] = uigetfile({‘*.*‘, ‘All files‘}, ‘MultiSelect‘, ‘on‘);
    if isnumeric(p) && ~p
        return;
    end
    directory = cell(size(p));
    if iscell(p)
        for i = 1:numel(p)
            directory{i} = [f, p{i}];
        end
    else
        directory=[f p];
    end
    isoverwrite = askforoverwrite();
end

% set other parameters
if ~exist(‘wh‘, ‘var‘), wh = 1600; elseif ischar(wh), wh = eval_r(wh); end %????eval????_r
if ~exist(‘isrecursive‘, ‘var‘), isrecursive = false; end
if ~exist(‘isoverwrite‘, ‘var‘), isoverwrite = false; end
if ~exist(‘savetopath‘, ‘var‘), savetopath = []; end
if nargin <= 5 || isempty(supportFormat), supportFormat = ‘*‘; end

%% mult-files
% if directory is a cell, it indicate multi-files or multi-directories,
% we resize them one by one

if iscell(directory)
    for i = 1:numel(directory)
        resizephotos(directory{i}, wh, isrecursive, isoverwrite, savetopath, supportFormat);
    end
    return;
end

%% if everything are OK

if exist(directory, ‘file‘) &&  ~isdir(directory) % for a file,
    if nargin <= 4 || isempty(savetopath)
        if isoverwrite
            savetopath = directory;
        else
            [pd, fd] = lastdirectory(directory);
            savetopath = [pd, ‘resize-‘, fd];
        end
    end
    resizesinglephoto(directory, wh, savetopath);
elseif isdir(directory) % if directory is a real directory
    % the last char of directory should be a ‘\‘
    if directory(end) ~= ‘\‘
        directory = [directory, ‘\‘];
    end
    % generate the saved directory
    if nargin <= 4 || isempty(savetopath) % when at the first recursive and savetopath is not set
        % we need to generate the saved directory
        if isoverwrite
            savetopath = directory;
        elseif ~isoverwrite
            [savetopath, lastd] = lastdirectory(directory);
            savetopath = [savetopath, ‘resize-‘, lastd, ‘\‘];
        end
        % when the savetopath do not exist, we create one
        if ~isdir(savetopath)
            mkdir(savetopath);  %????????????????
        end
    end

    % now we resize all photos in current directory, and recursive resize
    % all the directories if needed
    allfiles = dir(directory);  %????????????????(????????????)??????????????????

    % before we generate now image file, we first make sure the savetopath
    % exists, otherwise generate it.
    if ~isdir(savetopath)
        savetopath=[savetopath(1:end-1) ‘resize‘ ‘\‘];
        mkdir(savetopath);  %????????????????????????????????resize
    end
    for i = 1:numel(allfiles)
        cur = allfiles(i).name;
        % ignore the path ‘.‘ and ‘..‘
        if numel(cur) > 2 || ~min(cur == ‘.‘)
            if allfiles(i).isdir && isrecursive
                resizephotos([directory cur ‘\‘], wh, isrecursive, isoverwrite, ...
                    [savetopath cur ‘\‘], supportFormat);
            elseif ~allfiles(i).isdir
                resizesinglephoto([directory cur], wh, [savetopath cur]);
            end
        end
    end
end

%% resize single photo, save it to savetopath
function resizesinglephoto(photo, wh, savetopath)

try
    I = imread(photo);
catch ME %#ok<NASGU>
    % disp(ME.message);
    disp([‘!!! ‘ photo ‘ is not recognized as an image file, and it‘‘s ignored‘]);
    return;
end

% [w, h] is the width and height of original graph
w = size(I, 1);
h = size(I, 2);

% ????resize???????? [maxw, maxh]
if numel(wh) == 1
    if w > h
        maxw = wh;
        maxh = wh*h/w;
    else
        maxh = wh;
        maxw = wh*w/h;
    end
else
    maxw = wh(1);
    maxh = wh(2);
    if (w-h)*(maxw-maxh) < 0
        [maxw, maxh] = deal(h, w);
    end

    if w/h < maxw/maxh
        maxw = maxh*w/h;
    else
        maxh = maxw*h/w;
    end
end

if maxw < w
    I = imresize(I, [maxw, maxh]);   %????????????????????????????????????????????????????
end
imwrite(I, savetopath);
disp([photo ‘ is resized, and is saved to ‘ savetopath]);

%% lastdirectory
function [pd, ld] = lastdirectory(d)
% return the last directory, for example, lastdirectory(‘\abc\edf\‘) =
% [‘\abc\‘,‘edf‘]. When d is a file, return the path and the file name, i.e
% lastdirectory(‘abc\def\x.jpg‘) = [‘abc\def\‘, ‘x.jpg‘]

% remove the last if isdir(d) && d(end) == ‘\‘
    d = d(1:end-1);
end

% if there is a if max(d==‘\‘)
    ld = d(find(d==‘\‘, 1, ‘last‘)+1:end);
else
    ld = d;
end

pd = d(1:end-numel(ld));

%% ask for whether to overwrite original files
function isoverwrite = askforoverwrite()
tmp = questdlg([‘Did you want to overwrite the original files? ‘ ...
        ‘if you choose No, we will add a ‘‘resize-‘‘ prefix to your ‘ ...
        ‘files or directories.‘]);
if strcmp(‘Yes‘, tmp)
    isoverwrite = true;
elseif strcmp(‘No‘, tmp)
    isoverwrite = false;
end

  

时间: 2024-10-08 23:36:49

MATLAB 批量处理图片的相关文章

ps批量处理图片

刚刚有朋友问,ps咋做批量动作呢,其实特别简单,基本一劳永逸,用尺寸做个例子,大家看看就知道了. ps批量处理图片

使用matlab批量处理图像后在指定文件夹存储

使用matlab批量处理图像后在指定文件夹存储 clear;clc;close all; Files=dir('D:\文件及下载相关\文档\MATLAB\postgraduate\Kodak\*.jpg'); N=length(Files); Names={}; Images={}; for k=1:N Names{k}=Files(k).name; Images{k}=imread(['D:\文件及下载相关\文档\MATLAB\postgraduate\Kodak\' Names{k}]);

MATLAB批量修改图片名称

申明:转载请注明出处. 设在“D:\UserDesktop\pic\”目录下有很多张格式为jpg照片,命名不规则,如图. 现在用MATLAB批量修改所有图片的命名格式,改为1.jpg,2.jpg,.... 1 path = 'D:\UserDesktop\pic\'; 2 D = dir([path '*.jpg']);//图片路径 3 for i = 1:length(D) 4 a = imread([path D(i).name]);//批量读取图片名称 5 system(['ren 'a'

Photoshop批量处理图片技巧

我所了解的几个少儿编程软件,其本身的图像处理功能非常基础(这个可以理解),但是实际编程中往往需要制作相对漂亮的卡通形象,理由自然不必说.因此,第三方图像编辑工具,例如Photoshop,的基本应用就是很自然的要求了.本文介绍图像处理过程中常用的两个批量处理图像的技巧. 当然,正规的游戏软件开发中,美工与编程往往是多人合作完成的,一个既会美工又会编程的人其能量不可想像! 一.多张图片快速制作成gif动画 1.将要制作成gif动态图的图片改成同样大小的尺寸,保存在同一个文件夹里,并按想制作成的动作顺

使用convert来批量处理图片

这是个神奇的工具,居然使用命令行就可以这么方便的处理图片.功能有待挖掘. 这个是把图片批量进行 resize 的脚本. #!/bin/sh counter=1 root=mypict resolution=400x300 for i in `ls -1 $1/*.jpg`; do echo "Now working on $i" convert -resize $resolution $i ${root}_${counter}.jpg counter=`expr $counter +

matlab批量灰色预测

没事玩了一下matlab 发现现在网上的代码都是一组数据预测 所以我就写个批量数据的预测 顺便学习下matlab ----------------------------------我是快乐的分割线------------------------------------ 灰色预测的主要思想是: 1.给定一组数据 2.进行累加,即 X(1)1=x(0)1 X(1)2=x(0)1+x(0)2 X(1)3=x(0)1+x(0)2+x(0)3 … 3.最终目的是为了构造预测方程: 其中: 而为了求得上式

matlab 批量改变图片大小 imresize 任意改变

ObjDir = 'F:\STUDY\CamVid\trainannot\';%将被改变的图像地址,称为目标地址 OtpDir = 'F:\CamVid\trainannot\';%输出图像地址,称为输出地址 for i = 1:1:1340%我的图像标号是00000001到00001340 bgFile = [ObjDir,num2str(i,'%08d'),'.png'];%这句话读取目标地址里面的格式为png的图片 %num2str是先把数字i转换成string然后补零直到八位 %举个例子

matlab批量读取文件夹里面的文件名,并且调整图片大小,再按照原名称输出

举个例子,我要批量修改某文件夹里面的图片大小,我不想用顺序命名,之后我还想原名输出 fileFolder=fullfile('E:\caffe\SegNet_ip\CamVid\test');%读取图片路径 dirOutput=dir(fullfile(fileFolder,'*.jpg'));%读取文件夹里面文件 OtpDir = 'E:\test';%输出路径 fileNames = {dirOutput.name};%获得文件名称,存成元祖,可以自己看下fileName for i = 1

Photoshop 批量处理图片

任何你想重复进行的操作都可以通过创建 Photoshop 批处理程序来完成.例如,你想批量改变图片的大小,就可以通过以下操作来实现. 1.打开任意一张图片,在动作面板中,点击新建按钮 2.在新建动作对话框中更改动作的名称,然后点击记录 3.接下来就是进行你想批量处理的操作,这里就是改变图片大小 4.之后点击动作面板中的停止按钮 5.接着我们点击菜单栏上面的 文件-自动-创建快捷批处理 6.在打开的对话框中点击选择按钮来设置批处理文件存放的位置,设置动作为刚才建立的那个动作 7.在文件夹中找到我们