如何将这些图像放在一起? [英] How to put these images together?

查看:127
本文介绍了如何将这些图像放在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 200 时间点。对于每个时间点,都有一个图像,其大小为 40 * 40 double ,对应于此时间点。例如,图像1 对应于时间点1 ; 图片k 对应时间点k k = 1,2,..., 200 )。

I have 200 time points. For each time point, there is an image, the size of which is 40*40 double, corresponds to this time point. For example, image 1 corresponds to time point 1; image k corresponds to time point k (k = 1,2,...,200).

时间点 T = 1:200 ,名为 Image_T 的图像,因此 Image_1 Image_2

The time points are T = 1:200, with the images named as Image_T, thus Image_1, Image_2 etc.

我想将所有这些 200 图像放在一起。最终尺寸为 40 * 40 * 200 double。最终图像看起来像fMRI图像( fmri_szX = 40 fmri_szY = 40 fmri_szT = 200 )。怎么实现呢?
谢谢!

I want to put all these 200 images together. The final size is 40*40*200 double. The final image looks like fMRI image (fmri_szX = 40, fmri_szY = 40 and fmri_szT = 200). How to achieve that? Thanks!

推荐答案

动态变量



请注意虽然这是可能的,但它被认为是糟糕的编程(例如参见此处,或 Loren的这个博客甚至还有他们的文档告诉你不要这样做)。将图像直接加载到3D数组或单元格结构中会更好,从而避免使用动态变量名称。我刚刚发布了这个完整性;如果您碰巧必须使用此解决方案,则应立即更改为(cell-)数组。

Dynamic variables

Note that whilst this is possible, it's considered to be bad programming (see for instance here, or this blog by Loren and even the Mathworks in their documentation tell you not to do this). It would be much better to load your images directly into either a 3D array or a cell structure, avoiding dynamic variable names. I just posted this for completeness; if you ever happen to have to use this solution, you should change to a (cell-) array immediately.

链接文章的要点为什么 eval 是个坏主意,是MATLAB无法再预测操作结果会是什么。例如, A = 3 *(2:4)被MATLAB识别为输出双数组。如果你 eval 东西,MATLAB就不能再这样了。 MATLAB是一种解释型语言,即读取每行代码然后运行,而不事先编译整个代码。这意味着每次MATLAB遇到 eval 时,它必须停止,计算表达式,然后检查输出,存储并继续。 MATLAB(JIT / MAGMA等)使用的大多数速度引擎在没有预测语句结果的情况下无法工作,因此会在 eval 评估期间关闭,呈现你的代码非常慢。

The gist of the linked articles as to why eval is such a bad idea, is that MATLAB can no longer predict what the outcome of the operation will be. For instance A=3*(2:4) is recognised by MATLAB to output a double-array. If you eval stuff, MATLAB can no longer do this. MATLAB is an interpreted language, i.e. each line of code is read then ran, without compiling the entire code beforehand. This means that each time MATLAB encounters eval, it has to stop, evaluate the expression, then check the output, store that, and continue. Most of the speed-engines employed by MATLAB (JIT/MAGMA etc) can't work without predicting the outcome of statements, and will therefore shut down during the eval evaluation, rendering your code very slow.

此外,使用 eval 还有一个安全方面。请考虑以下事项:

Also there's a security aspect to the usage of eval. Consider the following:

var1=1;
var2=2;
var3=3;
varnames={'var1','var2; disp(''GOTCHA''); %', 'var3'};

accumvar=[];
for k=1:numel(varnames)
   vname=varnames{k};
   disp(['Reading from variable named ' vname]); eval(['accumvar(end+1)=' vname ';']);
end

现在 accumvar 将包含所需的变量名称。但是如果你没有设置 accumvar 作为输出,你也可以不使用 disp ,但是 eval('rm -rf~ / *')这会格式化整个磁盘,甚至不会告诉你这样做。

Now accumvar will contain the desired variable names. But if you don't set accumvar as output, you might as well not use a disp, but e.g. eval('rm -rf ~/*') which would format your entire disk without even telling you it's doing so.

循环方式

for ii = 200:-1:1
    str = sprintf('Image_%d',ii);
    A(:,:,ii) = eval(str);
end

这会创建您的矩阵。请注意,我让 for 循环向后运行,以便以最大的大小初始化 A

This creates your matrix. Note that I let the for loop run backwards, so as to initialise A in its largest size.

半矢量化方法

str=strsplit(sprintf('image_%d ',1:200),' '); % Create all your names
str(end)=[]; % Delete the last entry (empty)
%Problem: eval cannot handle cells, loop anyway:
for ii = 200:-1:1
    A(:,:,ii)=eval(str{ii});
end

eval 不支持数组,所以你不能直接插入cellarray str 。像 cellfun 这样的函数也不支持 eval 所以你必须循环。

eval does not support arrays, so you cannot directly plug the cellarray strin. Functions like cellfun also do not support eval so you have to loop.

尽管有类似的标题,但这意味着你的文件名是结构化的,所以在文件中浏览器,而不是MATLAB。我在这里假设.jpg文件,但您可以添加每个支持的图像扩展名。此外,请务必在单个文件夹中包含所有图像,并且不得使用该扩展名添加其他图像,或者您必须修改 dir()调用只包括所需的图像。

Despite having a similar title as above, this implies having your file names structured, so in the file browser, and not MATLAB. I'm assuming .jpg files here, but you can add every supported image extension. Also, be sure to have all images in a single folder and no additional images with that extension, or you have to modify the dir() call to include only the desired images.

filenames = dir('*.jpg');
for ii = length(filenames):-1:1
    A(:,:,:,ii) = imread(filenames{ii});
end

图像通常读作 m * n * 3 文件,其中 m * n 是您的图像大小(以像素为单位), 3 源于他们被 imread 读成RGB的事实。因此 A 现在是一个4D矩阵,结构为 m * n * 3 * T ,其中最后一个索引对应于图像的时间,前三个是RGB格式的图像。

Images are usually read as m*n*3 files, where m*n is your image size in pixels and the 3 stems from the fact that they're read as RGB by imread. Therefore A is now a 4D matrix, structured as m*n*3*T, where the last index corresponds to the time of the image, and the first three are your image in RGB format.

由于您没有指定如何获得 40 * 40 加倍,我已离开4D矩阵。可能是你读了它们然后切换到使用 uint16 解释RGB,这是一个单一的数字,这将导致 m * n * 1 * T 变量,您可以通过调用 A = squeeze(A(:,:,1,:));

Since you do not specify how you obtain your 40*40 double, I have left the 4D matrix. Could be you read them and then switch to using a uint16 interpretation of RGB, which is a single number, which would result in a m*n*1*T variable, which you can reduce to a 3D variable by calling A = squeeze(A(:,:,1,:));

这篇关于如何将这些图像放在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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