20060606-Batch processing

本文介绍了一种使用MATLAB进行图像批量处理的方法,包括获取文件名列表、读取图像数据、处理图像、构造输出文件名及保存处理后的图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://blogs.mathworks.com/steve/2006/06/06/batch-processing/

Step 1: Get a list of filenames

If you use the dir function with an output argument, you get back a structure array containing the filenames, as well as other information about each file.

Let's say I want to process all files ending in .jpg:

files = dir('*.jpg')
Let's look at the details of the  files struct array for a couple of these files.
files(1)
files(end)

Step 2: Determine the processing steps to follow for each file

There are four basic steps to follow for each file:

  1. Read in the data from the file.
  2. Process the data.
  3. Construct the output filename.
  4. Write out the processed data.

Here's what my read and processing steps looked like:

rgb = imread('IMG_0175.jpg');  % or rgb = imread(files(1).name);
rgb = rgb(1:1800, 520:2000, :);
rgb = imresize(rgb, 0.2, 'bicubic');
You have many options to consider for constructing the output filename. In my case, I wanted to use the same name but in a subfolder:
output_name = ['cropped\' files(1).name] % Use fullfile instead if you want
                                         % multiplatform portability
You might use something like this if you want to change image formats.
input_name = files(1).name
[path, name, extension] = fileparts(input_name)
output_name = fullfile(path, [name '.tif'])
Step 3: Put everything together in a for loop

Here's my complete batch processing loop:

files = dir('*.JPG');
for k = 1:numel(files)
   rgb = imread(files(k).name);
   rgb = rgb(1:1800, 520:2000, :);
   rgb = imresize(rgb, 0.2, 'bicubic');
   imwrite(rgb, ['cropped\' files(k).name]);
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值