ron*_*oni 1 printing matlab image-processing save-as
我有很多图片都在一个名为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
Run Code Online (Sandbox Code Playgroud)
这个程序运行良好,但后来我发现了这个命令来绘制漂亮的图形。 export_fig
export_fig
采用以下形式的基本命令:
export_fig file_name.file_type
Run Code Online (Sandbox Code Playgroud)
如何自动替换存储为 str1 的输出文件名代替 export_fig 命令中的 file_name 占位符。
注意:请注意 export_fig 文档中的这一点(对于变量文件名)
for a = 1:5
plot(rand(5, 2));
export_fig(sprintf('plot%d.png', a));
end
Run Code Online (Sandbox Code Playgroud)
我不想要这个解决方案。请理解我的查询,即有数以千计的 MATLAB 函数需要输入export_fig
基本语句中给出的数据。关于变量文件名的特殊情况已经在 export_fig 函数中构建。
我想知道如果它不是构建的,那么我怎么能使用自动生成的变量文件名?我的查询不是专门关于 export_fig 而是关于如果输入不能是字符串时我可以提供变量文件名的基本方式?
如果您在理解问题时遇到困难,请问我。
语法my_function file_name.file_type
等效于my_function('file_name.file_type')
- 两者之间没有区别。
因此,如果您希望在循环中使用它,您可以使用任何方法来创建文件名,然后调用该函数:
for i=1:N
% construct the filename for this loop - this would be `str1` in your example
file_name = sprintf('picture_%i.jpeg', i);
% or:
file_name = strcat('picture_', num2str(i), '.jpeg');
% call the function with this filename:
my_function(file_name);
end
Run Code Online (Sandbox Code Playgroud)