![]() |
如何告诉MATLAB在同一目录中打开和保存特定文件
我必须在目录中的大量图像上运行图像处理算法。
图像另存为name_typeX.tif ,因此给定名称有X种不同类型的图像。 图像处理算法获取输入图像并输出图像结果。 我需要将此结果另存为name_typeX_number.tif ,其中number也是给定图像的算法输出。 现在.. 如何告诉MATLAB打开特定的typeX文件?另请注意,同一目录中还有其他非tif文件。 如何将结果另存为name_typeX_number.tif ? 结果必须保存在存在输入图像的相同目录中。如何告诉MATLAB不要处理已保存为输入图像的结果? 我必须在服务器上将其作为后台代码运行...因此不允许用户输入。 [B]回答:[/B] 听起来您想将目录中名称与某种格式匹配的所有文件都获取,然后自动处理它们。您可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dir.html"]DIR[/URL]函数来获取当前目录中文件名的列表,然后使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/regexpi.html"]REGEXP[/URL]函数来查找与特定模式匹配的文件名。这是一个例子: 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 上面的示例使用了[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/numel.html"]NUMEL[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strtok.html"]STRTOK[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.html"]NUM2STR[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imread.html"]IMREAD[/URL]和[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imwrite.html"]IMWRITE函数[/URL] 。 [url=https://stackoverflow.com/questions/3048453]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 15:13。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.