How do you catalogue/index a matrix to be concatenated into a 3D matrix?

4 次查看(过去 30 天)
I want to stitch together a series of images (2D matrices) into a 3D image (3D matrix). Preferably, this would be done within a for loop, however I don't understand how to catalogue (or is the term "index?") each matrix. And I don't know how to concatenate the catalogued/indexed images within the for loop.
% n = specified number of images;
% do you need an example image to answer this question?
for m = 1:n
figname = strcat('figure', m, '.png');
IM = imread(figname); % how do I catalogue/index here?
end
IM3d = cat(3, ...) % how do I make the 3D matrix here?
  1 个评论
Stephen23
Stephen23 2018-9-13
编辑:Stephen23 2018-9-13
"I want to stitch together a series of images (2D matrices) ..."
Are the images really 2D? Most color and grayscale image are stored as RGB, which are 3D arrays:

请先登录,再进行评论。

回答(2 个)

Greg
Greg 2018-9-13
编辑:Greg 2018-9-13
You actually had a pretty good start. The only piece of your attempt that wasn't going to work was that you weren't concatenating inside the loop. Since each iteration loaded a new filename and overwrote IM, you only ended up with 1 frame outside the loop.
You could have added IM = []; before the loop then IM = cat(3,IM,imread(…)); inside the loop, but this leads to performance issues. Also, it requires all images to be exactly the same size in pixels.
The following code pre-allocates, and allows images to be different sizes.
filenames = strcat('figure', num2str(1:n)', '.png');
info = cellfun(@imfinfo,filenames);
widths = [info.Width]';
heights = [info.Height]';
IM3d = NaN(max(heights),max(widths),n);
for m = 1:n
w = widths(m);
h = heights(m);
IM3d(1:h,1:w,m) = imread(filenames{m});
end
"Catalogue" isn't a MATLAB term (at least not in any context related to this question). Your approach doesn't technically use "indexing," but that is an accurate term. "Concatenate" is the appropriate word. My commands inside the for loop use indexing.
P.S. I can't launch MATLAB to make sure I don't have any silly syntax errors, but the above approach is at least pretty close. If it doesn't work for you, let me know and I'll update.

Stephen23
Stephen23 2018-9-13
编辑:Stephen23 2018-9-13
I would recommend following the examples in the MATLAB documentation:
In your case you can simply do this:
N = ... number of images
C = cell(1,N); % preallocate cell array.
for k = 1:N
figname = sprintf('figure%d.png',k);
C{k} = imread(figname); % read one image.
end
IM3d = cat(3,C{:}) % concatenate all images.
If the images are actually 3D arrays, then you just need to trivially change to cat(4,...) to concatenate along the fourth dimension. Note that the correct term is "indexing":
Cell arrays (e.g. the variable C) have two different kinds of indexing:
The introductory tutorials are the best place to learn basic MATLAB concepts, like what indexing is:

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by