简体   繁体   中英

Matlab: Create 3D cube RGB and show it

I'll try to be precise and short.

I have a volume (128x128x128) and a mask (same size with [0|1|2] values)

I want to make the 3D volume matrix a 3D image with RGB, and store in each channel (red,green,blue) the points marked in the mask.

This is to use a 2D representation by taking a slice of that 3D cube, and not compute it over and over to make things way more faster (very important in my project), so actually, the 3D volume + rgb would be like a store for 128 2D images.

The question is, what steps and how do I have to make all this: - Create a volume 128x128x128x3 ? - Define a new colormap (original is gray) ? - Join each channel ? - How do I use imagesc/whatever to show one slice of that cube with the points in the color as marked in the mask (ex: imageRGB(:,:,64)) ?

That's just my guess, but I don't even know how to do it properly...I'm a bit lost, I hope you can help me, this is a piece of code that may be wrong but may help you out

% Create the matrix 4D
ovImg = zeros(size(volImg,1),size(volImg,2),size(volImg,3),3);    % 128x128x128x3
% Store in each channel the points marked as groups
ovImg(:,:,:,1) = volImg .* (mask==1);
ovImg(:,:,:,2) = volImg .* (mask==2);
ovImg(:,:,:,3) = volImg .* (mask==3);

many many thanks!!

UPDATE:

I'm having some trouble with transparency and the colormap, this is what I did.

% Create the matrix 4D
ovImg = zeros(size(volImg,1),size(volImg,2),size(volImg,3),3);
% Store in each channel the points marked as groups
ovImg(:,:,:,1) = imaNorm.*(mask==1);
ovImg(:,:,:,2) = imaNorm.*(mask==2);
ovImg(:,:,:,3) = imaNorm.*(mask==3);

[X,Y,Z] = meshgrid(1:128,1:128,1:128);
imaNorm = volImg - min(volImg(:));   
maxval = max(imaNorm(:));        
ovImg  = imaNorm + mask * maxval;


N= ceil(maxval);
c = [linspace(0,1,N)' zeros(N,2)];
my_colormap = [c(:,[1 2 3]) ; c(:,[3 1 2]) ; c(:,[2 3 1])];

figure;
imshow(squeeze(ovImg(:,:,64)),my_colormap);

figure;
imagesc(squeeze(mask(:,:,64)));

Result (Overlayed image / mask) 叠加和掩码图片 Any ideas? Thanks again, everybody


FINAL UPDATE: With the other approach that Gunther Struyf suggested, I had exactly what I wanted. Thanks mate, I really appreciate it, hope this helps other people too.

You can use imshow with a colormap to 'fake' an RGB image from a grayscale image (which you have). For the scale I'd not multiply it, but add an offset to the value, so each mask is a different range in the colormap.

For plotting a slice of the 3d matrix, you can just index it and then squeeze it to remove the resulting singleton dimension:

Example:

[X,Y,Z]=meshgrid(1:128,1:128,1:128);
volImg =5*sin(X/3)+13*cos(Y/5)+8*sin(Z/10);
volImg=volImg-min(volImg(:));
mask = repmat(floor(linspace(0,3-2*eps,128))',[1 128 128]);

maxval=max(volImg(:));
ovImg=volImg+mask*maxval;
imshow(squeeze(ovImg(:,:,1)),jet(ceil(max(ovImg(:)))));

Unmasked, original image ( imshow(squeeze(volImg(:,:,1)),jet(ceil(maxval))) )

在此输入图像描述

Resulting with mask (code block above):

在此输入图像描述

For different colormaps, see here , or create your own colormap. Eg you're mask has three values, so let's match those with R,G and B:

N = ceil(maxval);
c = [linspace(0,1,N)' zeros(N,2)];
my_colormap = [c(:,[1 2 3]) ; c(:,[3 1 2]) ; c(:,[2 3 1])];
figure
imshow(squeeze(ovImg(:,:,1)),my_colormap);

which gives:

在此输入图像描述

Other approach:

Now I understand your question, I see you got it quite right from the beginning, you only need rescale the variable to a value between 0 and 1, since from imshow :

Color intensity can be specified on the interval 0.0 to 1.0.

which you can do using:

minval=min(volImg(:));
maxval=max(volImg(:));
volImg=(volImg-minval)/(maxval-minval);

next up is your code:

ovImg = zeros([size(volImg),3]);
ovImg(:,:,:,1) = volImg .* (mask==1);
ovImg(:,:,:,2) = volImg .* (mask==2);
ovImg(:,:,:,3) = volImg .* (mask==3);

You just have to plot it now:

imshow(squeeze(ovImg(:,:,64,:)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM