Making a RGB 3D array from multiple 2D colored slices

17 views (last 30 days)
Hello, I have multiple CT Scan slices that I want to add together into a 3D array for processing. I have 2 options now, I can convert the slices to RGB beforehand using threshholding and combine them like this:
if slice == 1
array3d = thisSlice
array3d = cat(3, array3d, thisSlice);
save ('3D\3d_array.mat', 'array3d' );
Now, the problem with that is that i don't get an RGB array in implay, it takes every color as it's own slice, so when i add 7, i get 21 slices in implay.
The other option is that i combine them using the code above while still in greyscale, and then convert the stack ionto RGB.
But using this code on the stack:
thresh = multithresh(original,2);
seg_I = imquantize(original,thresh);
RGB = label2rgb(seg_I);
does not work, because label2rgb does only take 2D input. I tried label2rgb3D, but that only puts out a 4D double instead of a matrix that I can work with.
For example, i want to blur it using imfilter and i cant get the 4D-double to work with implay or volumeviewer.

Accepted Answer

Superficial on 20 Nov 2020
The problem you have is that when you have a greyscale image, each pixel has one intensity value. When you want RGB, you need a value for each of red, green and blue. So an image that in greyscale is 256x256x1 becomes 256x256x3. Or a stack of 10 slices which would be 256x256x10 needs to become 256x256x3x10.
I'm not intimately familiar with the functions you mention but whenever I've thresholded medical images, I do it 'manually'. Something like this:
input=imread('cameraman.tif'); % load example image to be your 'slice'
segmented=zeros(size(N)); % Initiates segmented copy of slice
segmented(input>128)=1; % if the input > 128, marks as 1
segmented(input>168)=2; % Second threshold level
That code should be translatable to 3D arrays too (except imagesc won't display them).
If you must use label2rgb then it would be fairly simple to write a for loop to iterate through the slices one at a time:
newimage=flipud(segmented); % make another slice as an example
img_stack(:,:,2)=newimage; % Stacks the slices
img_stackRGB=zeros(256, 256, 3, 2); % the 'RGB' stack is the same size as the original but adds an extra dimension for each RGB value
for i=1:size(img_stack,3)
Caveat: I'm not familiar with label2rgb though and it's possible it would treat each slice differently. Looks OK at first glance.
implay appears to like a 4D array input in the format 256 x 256 x 3 x nSlices.

More Answers (0)


Find more on Graphics Object Programming in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!