在每次迭代中使用for循环的索引命名图像 [英] Naming Image using index of for loop in each iteration

查看:164
本文介绍了在每次迭代中使用for循环的索引命名图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MATLAB中为我的图像处理项目工作。
我使用for循环来生成某种图像数据(图像的大小变化)与每个循环迭代。我的问题是如何阻止它在下一次迭代覆盖图像。

  Img(i,j)= data 

理想情况下,我希望它有

  Img_1 = data(用于第一次迭代)
Img_2 = data(for 2nd iteration)
Img_3 = data(for 3rd iteration)



等等...



有什么办法可以实现吗? / p>

解决方案

是的,您可以使用具有结构的动态字段名称。我不会建议使用单独的变量名称,因为您的工作空间将变得笨拙。做这样的事情:

  img_struct = struct(); %//创建空结构

for ii = 1:num_iterations
%//对数据进行处理
%...
%...

img_struct。(['Img_'num2str(ii)])= data; %//迭代之后
结束

这将创建一个名为 img_struct 它将包含名为 Img_1 Img_2 等字段。从一个迭代访问特定的数据...说...迭代1,做:

  data = img_struct.Img_1; 

更改 _1






或者,您可以使用 cell arrays ...同样的思路:

pre $ code >%//创建空单元格数组
img_cell = cell(num_iterations,1);

for ii = 1:num_iterations
%//对数据进行处理
...
%...

img_cell {ii} = data; %//迭代之后
结束

单元格数组是每个元素 - 或者他们是非同类阵列。这意味着每个元素可以是任何你想要的。因此,由于您的图像数据在每次迭代时都会变化,所以这样做会非常好。要在任何迭代中访问数据,只需执行:

  data = img_cell {ii}; 

ii是您迭代的索引想要访问。


I am working in MATLAB for my image processing project. I am using a for loop to generate some kind of image data (size of image varies) with each loop iteration. My problem is how do stop it from overwriting the image in next iteration.

Img(i,j)=data

Ideally I would like it to have

Img_1 = data (for 1st iteration)
Img_2 = data (for 2nd iteration)
Img_3 = data (for 3rd iteration)

and so on...

Is there any way, it can be acheived?

解决方案

Yes, you can use dynamic field names with structures. I wouldn't recommend using separate variable names because your workspace will become unwieldy. Do something like this:

img_struct = struct(); %// Create empty structure

for ii = 1 : num_iterations
    %// Do your processing on data
    %...
    %...

    img_struct.(['Img_' num2str(ii)]) = data; %// After iteration
end

This will create a structure called img_struct where it will have fields that are named Img_1, Img_2, etc. To access a particular data from an iteration... say... iteration 1, do:

data = img_struct.Img_1;

Change the _1 to whatever iteration you choose.


Alternatively, you can use cell arrays... same line of thinking:

%// Create empty cell array
img_cell = cell(num_iterations, 1);

for ii = 1 : num_iterations
    %// Do your processing on data
    %...
    %...

    img_cell{ii} = data; %// After iteration
end

Cell arrays are arrays that take on any type per element - or they're non-homogeneous arrays. This means that each element can be whatever you want. As such, because your image data varies in size at each iteration, this will do very nicely. To access data at any iteration, simply do:

data = img_cell{ii};

ii is the index of the iteration you want to access.

这篇关于在每次迭代中使用for循环的索引命名图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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