【发布时间】:2013-09-20 19:33:34
【问题描述】:
我在名为 1.jpg、2.jpg、3.jpg 等的目录中有很多图像。我一一阅读。我做了一些操作,然后保存它们。
我想自动化这个操作。我可以读取图像名称。然后在生成输出文件时,我从输入文件名中提取 image_name,添加我需要的扩展名,添加我要保存的文件类型,然后通过打印命令保存图像。
%//Read the image
imagefiles = dir('*.bmp');
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
images{ii} = currentimage;
Img=currentimage;
%//Do some operation on the image
%//Save the image file
h=figure;
%//Display the figure to be saved
token = strtok(currentfilename, '.');
str1 = strcat(token,'_op');
print(h,'-djpeg',str1);
end
这个程序运行良好,但后来我发现这个命令可以绘制漂亮的图形。 export_fig
export_fig 采用以下形式的基本命令:
export_fig file_name.file_type
如何在 export_fig 命令中自动替换存储为 str1 的输出文件名代替 file_name 占位符。
注意:请注意 export_fig 文档中的这一点(用于变量文件名)
for a = 1:5
plot(rand(5, 2));
export_fig(sprintf('plot%d.png', a));
end
我不想要这个解决方案。请理解我的查询,即有数千个 MATLAB 函数需要输入数据,如export_fig 基本语句中给出的那样。关于变量文件名的特殊情况已经在 export_fig 函数中构建。
我想知道如果它没有构建,那我怎么能使用自动生成的变量文件名?我的查询不是专门针对 export_fig,而是关于如果输入不能是字符串,我可以提供变量文件名的基本方式?
如果您无法理解该问题,请询问我。
【问题讨论】:
-
我不太明白.. 如果你想要
export_fig而不是print,你可以简单地将print(h,'-djpeg',str1)替换为export_fig(str1),str1 现在是strcat(token,'_op.jpeg')。或许值得注意的是,在 MATLAB 中,类似以下的函数调用:function_name x与function_name('x')相同。 -
不,你不明白我的问题。假设有一个名为 my_function 的函数,其基本语句的形式为 my_function file_name.file_type。如果假设 file_name 在循环内变化,考虑到我的 file_name 在循环的每次迭代中变化,我如何在循环内调用 my_function ?你现在明白了吗?请告诉我,否则我将编辑我的问题。
标签: matlab image-processing printing save-as