如何告诉MATLAB在同一目录中打开和保存特定文件 [英] How to tell MATLAB to open and save specific files in the same directory

查看:112
本文介绍了如何告诉MATLAB在同一目录中打开和保存特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在目录中的大量图像上运行图像处理算法.

I have to run an image processing algorithm on numerous images in a directory.

图像另存为name_typeX.tif,因此给定名称有X种不同类型的图像.

An image is saved as name_typeX.tif, so there are X different type of images for a given name.

图像处理算法获取输入图像并输出图像结果.

The image processing algorithm takes an input image and outputs an image result.

我需要将此结果另存为name_typeX_number.tif,其中number也是给定图像的算法输出.

I need to save this result as name_typeX_number.tif, where number is also an output from the algorithm for a given image.

现在.

如何告诉MATLAB打开特定的typeX文件?另外请注意,同一目录中还有其他非tif文件.

How do I tell MATLAB to open a specific typeX file? Also note that there are other non-tif files in the same directory.

如何将结果另存为name_typeX_number.tif?

结果必须保存在与输入图像相同的目录中.如何告诉MATLAB不要处理已保存为输入图像的结果?

The results have to be saved in the same directory where the input images are present. How do I tell MATLAB NOT to treat the results that have been saved as an input images?

我必须在服务器上将其作为后台代码运行...因此不允许用户输入.

I have to run this as background code on a server... so no user inputs allowed.

推荐答案

听起来,您想要将目录中名称与特定格式匹配的所有文件都获取,然后自动对其全部进行处理.您可以使用功能 DIR 来完成此操作当前目录中的文件名列表,然后使用功能 REGEXP 查找与特定模式匹配的文件名.这是一个示例:

It sounds like you are wanting to get all files in a directory whose names match a certain format, then process them all automatically. You can do this using the function DIR to get a list of file names in the current directory, then using the function REGEXP to find file names that match a certain pattern. Here's an example:

fileData = dir();             %# Get a structure of data for the files in the
                              %#   current directory
fileNames = {fileData.name};  %# Put the file names in a cell array
index = regexp(fileNames,...                 %# Match a file name if it begins
               '^[A-Za-z]+_type\d+\.tif$');  %#   with at least one letter,
                                             %#   followed by `_type`, followed
                                             %#   by at least one number, and
                                             %#   ending with '.tif'
inFiles = fileNames(~cellfun(@isempty,index));  %# Get the names of the matching
                                                %#   files in a cell array

一旦您在inFiles中具有与所需的命名模式匹配的文件单元格阵列,就可以简单地循环遍历文件并执行处理.例如,您的代码可能如下所示:

Once you have a cell array of files in inFiles that matches the naming pattern you want, you can simply loop over the files and perform your processing. For example, your code might look like this:

nFiles = numel(inFiles);    %# Get the number of input files
for iFile = 1:nFiles        %# Loop over the input files
  inFile = inFiles{iFile};  %# Get the current input file
  inImg = imread(inFile);   %# Load the image data
  [outImg,someNumber] = process_your_image(inImg);  %# Process the image data
  outFile = [strtok(inFile,'.') ...   %# Remove the '.tif' from the input file,
             '_' ...                  %#   append an underscore,
             num2str(someNumber) ...  %#   append the number as a string, and
             '.tif'];                 %#   add the `.tif` again
  imwrite(outImg,outFile);  %# Write the new image data to a file
end

以上示例使用了 NUMEL STRTOK

The above example uses the functions NUMEL, STRTOK, NUM2STR, IMREAD, and IMWRITE.

这篇关于如何告诉MATLAB在同一目录中打开和保存特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆