stacking rgb images into 3d or 4d variable

I have mri frames that are in rgb and I want stack them in a 4d variable ( height x width x 3 channels x number of frames) so that I can view them as a montage.
How can i do this?

 Accepted Answer

Adam Danz
Adam Danz on 26 Sep 2019
Edited: Adam Danz on 26 Sep 2019
If the height and width are the same for all images, use cat().
a = ones(5,6,3)+0;
b = ones(5,6,3)+1;
c = ones(5,6,3)+2;
stacked = cat(4,a,b,c);
% ^ concatenate along 4th dim
If the image sizes are not identical, you can pad them smaller ones to match the size of the larged one by using paddarray() and then use cat().

10 Comments

thank you for your reply. I have 33 frames which were loaded and stored in a 1x33 cell (baseFile86611) , each frame is 128 x 128 x 3. so could you explain what a and b would mean in this context.
file86611 = dir(fullfile(img_Folder86611 , '*.dcm'));
for P = 1:length(file86611)
baseFile86611 = {file86611.name};
end
This fake data fits your description
%Fake data
data = arrayfun(@(x)rand(128,128,3),1:33,'UniformOutput',false);
To concatenate it,
array = cat(4,data{:});
Check size of array
size(array)
% ans =
% 128 128 3 33
thank you it worked! It created a 4d variable, however when I try to visualize the 4d variable as a montage
array = uint8(array);
montage86611 = imtile(array, []);
set(gca, 'clim');
figure; imshow(montage86611);
it does not display the images correctly. what am i doing wrong?
After you concatenate using cat(), what is the size of "array" output?
size(array)
Also, just curious, what's the purpose of the 8-bit unsigned conversion (unit8()) ?
If you attach a mat file with "array" variable just after you concatenate, I can take a look at it.
"the size of array is 1 x 10 x 1 x 33 "
Well, that tells us something's wrong.
The code you attached starts with this:
file86611 = dir(fullfile(img_Folder86611 , '*.dcm'));
for P = 1:length(file86611)
baseFile86611 = {file86611.name};
end
There are 2 problems with the for-loop.
  1. On each iteration of your loop, the variable "baseFile86611" is being overwritten...
  2. ...but that doesn't matter because you're writing the same values to it each time.
In other words, that for-loop does the same thing over and over again: it assigns a cell array of filenames to the variable.
Then you're reading in the images but you never use the image variable! Instead, you're concatenating the file names.
array = cat(4, baseFile86611{:}); %stack the images into 4-D array
% ^^^^^^^^^^^^^^
% This is your list of filenames
What's the purpose of this line? The variable "array" is being overwritten on the next line.
array = zeros(info86611.Rows, info86611.Columns, 3, images86611 , class86611);
There are lots of things wrong with this code but the good news is that it's a very short code so it shouldn't take you look to study it. I suggest you clear your workspace and execute each line of code - one line at a time - and understand what is going on in each line of your code.
  • What do the variable values look like?
  • Are the outputs to each function what you expect?
  • Where does the code start to go wrong?
Ah I see what you're saying. I forgot to add the code where i used dicomread to read in one image so that i could find the dimensions of the frame (128 x 128 x 3)
info86611 = dicominfo(fullfile(img_Folder86611, baseFile86611{1})); %examine information from metadata
slice86611 = dicomread(fullfile(img_Folder86611 , baseFile86611{1}))
Okay so how do I create a cell array containing all the images?
Adam Danz
Adam Danz on 27 Sep 2019
Edited: Adam Danz on 30 Sep 2019
From your quesiton,
I have mri frames that are in rgb and I want stack them in a 4d variable ( height x width x 3 channels x number of frames) so that I can view them as a montage.
I assumed that you had already read in the images somehwere. First you'll need to read in the dcm images using dicomread. Below is a link to the documentation for dicomread and it contains examples of how to plot a montage. Those examples are different from my approach since I didn't know you were using this type of image file.
Give that a try and if you get stuck, share your updated code and describe where you're stuck.
could you help me with this error I'm getting with trying to fuzzy cluster nonzero pixels in my image. mage in which I have isolated the tumor region using thresholding (img 1) . Now im trying to use fuzzy clustering to cluster the pixels in just the tumor ROI but not the black background (basically ignore any pixels = 0 in clustering).When I try to segment the clusters into images it s not outputting any clustered images, because its throwing an error that "to reshape the number of elements must not change."
fuzzyT2 = mriAdjust(:,:,15);
[y, x] = find(fuzzyT2)
idx = fuzzyT2 > 0 %creates a mask for true value for all nonzero pixels
z = fuzzyT2(idx);
%% fuzzy c-means
G = 4
z = double(z)
[cluster_idx, cluster_center, obj] = fcm(z, G)
% reshape into four 2-D array
cluster1 = cluster_center(1,:);
cluster2 = cluster_center(2,:);
cluster3 = cluster_center(3,:);
cluster4 = cluster_center(4,:)
imIDX1 = reshape(cluster1, size(idx));
imIDX2 = reshape(cluster2, size(idx));
imIDX3 = reshape(cluster3, size(idx));
imIDX4 = reshape(cluster4, size(idx));
subplot(2,2,1), imshow(img_1), title('cluster 1')
subplot(2,2,2), imshow(img_2), title('cluster 2')
subplot(2,2,3), imshow(img_3), title('cluster 3')
subplot(2,2,4), imshow(img_4), title('cluster 4')
I see you've asked similar questions since adding the question above. If you still need help, please create a new question in the forum and send me the link to your new question or add the link as a comment below.
Thank you for your response. Yes here is the link to the question I put previously on the forum https://www.mathworks.com/matlabcentral/answers/489011-fuzzy-clustering-only-on-pixels-zero
I've re put my question on the forum, showing my thought process of trying to work through this problem, but Im still getting the same reshaping error

Sign in to comment.

More Answers (1)

the array was in double so I thought to turn it in to uint8 class which was the original format of the frames so that i could visualize it.
the size of array is 1 x 10 x 1 x 33 which is strange.
and array is stored as a 4-d uint8.
where as the dimensions of each frame is 128 x 128 x 3, so i'm guessing that when it is concatenating it is cutting down the image dimensions.

Community Treasure Hunt

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

Start Hunting!