File Exchange

image thumbnail


version 1.0 (5.27 KB) by

Creates a 4D rgb image from a 3D label image.

1 Download


No License

RGB = LABEL2RGB3D(L) converts a 3D label matrix L, such as returned by WATERSHED, into a color RGB image.

RGB = LABEL2RGB3D(L, MAP) defines the colormap to be used in the RGB image. MAP can either be an n x 3 colormap matrix, a string containing the name of a colormap function (such as 'jet' or 'gray'), or a function handle of a colormap function (such as @jet or @gray).

LABEL2RGB3D evaluates MAP so that there is a different color for each region in L.If MAP is not specified, 'jet' is used as the default.

RGB = LABEL2RGB3D(L, MAP, ZEROCOLOR) defines the RGB color of the elements labeled 0 in the input label matrix L. ZEROCOLOR can either be an RGB triple, or one of the following: 'y' (yellow), 'm', (magenta), 'c' (cyan), 'r'(red), 'g' (green), 'b' (blue), 'w' (white), or 'k'(black). If ZEROCOLOR is not specified, [1 1 1] is used as the default.

RGB = LABEL2RGB3D(L, MAP, ZEROCOLOR, ORDER), controls how colormap colors are assigned to regions in the label matrix. If ORDER is 'noshuffle' (the default), then colormap colors are assigned to the label matrix regions in numerical order. If ORDER is 'shuffle', then colormap colors are pseudorandomly shuffled.

Class Support
The input label matrix L can have any nonsparse numeric class. It must contain finite nonnegative integers. The output of LABEL2RGB3D is ofclass uint8.

MATLAB does not have the capability to visualize 4D data. But the image can be exported to other softwares for visualization.

Comments and Ratings (7)

I would add that the code of Nathan worked for me, thanks ;)

Didn't work for me either (Matlab2013b), even though the description looked perfect... thanks for your work, but might need an update. Best,

Nathan Orloff

function rgbcell = label2rgb3d(lblstack)
%label2rgb3d(lblstack) take a labeled stack and turn it into a truecolor image.
% get all of the unique ind
ind = unique(lblstack);
cmap = jet(numel(ind));
images = stack2cell(lblstack);
rgbcell = cellfun(@(x) im2double(label2rgb(x, cmap, 'k')), images, 'UniformOutput', false);

function images = stack2cell(stack)
%stack2cell(stack) convert a stack of data into a cell array
% Detailed explanation goes here
% See also cell2stack
images = num2cell(zeros(size(stack,3),1));
if numel(size(stack)) == 3 % bw and grayscale images
for ii = 1:size(stack,3)
images{ii} = stack(:,:,ii);
elseif numel(size(stack)) == 4 % truecolor data
for ii = 1:size(stack,4)
images{ii} = stack(:,:,:,ii);

function stack = cell2stack(images)
%cell2stack converts a cell array of images to a stack (or cube of images)
% converts a cell array of images to a stack (or cube of images)
p = numel(images);
dims = size(images{1});
stack = zeros([dims,p]);
if numel(dims) == 2 % bw and grayscale images
for ii = 1:p
stack(:,:,ii) = images{ii};
elseif numel(dims) == 3 % truecolor data
for ii = 1:p
stack(:,:,:,ii) = images{ii};


This should be all you need to do this. While manipulating multidimensional data I found cell2stack and stack2cell incredibly useful. Also, cellfun is a very fast way to implement many of the image processing routines. HTH- Nate

Nathan Orloff

I think this might be better. Also, I tried your code and I got a incorrect results, but I didn't really try very hard to make it work.

ind = unique(lblstack);
cmap = jet(numel(ind));
images = cell2stack(lblstack);
rgbcell = cellfun(@(x) label2rgb(x, cmap, 'k'), images, 'UniformOutput', false);


please can you tell me how to export the 4D image from matlab?

Alex Gough

MATLAB Release
MATLAB 7.0.4 (R14SP2)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video