Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何告诉MATLAB在同一目录中打开和保存特定文件

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

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

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

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

现在..

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

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

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

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



回答:

听起来您想将目录中名称与某种格式匹配的所有文件都获取,然后自动处理它们。您可以使用DIR函数来获取当前目录中文件名的列表,然后使用REGEXP函数来查找与特定模式匹配的文件名。这是一个例子:

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中具有与所需的命名模式匹配的文件单元格数组之后,您可以简单地循环遍历文件并执行处理。例如,您的代码可能如下所示:

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 上面的示例使用了NUMELSTRTOKNUM2STRIMREADIMWRITE函数



更多&回答...
poster 当前离线   回复时引用此帖
 

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 23:15


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.