# Converting Gamut3D from RGB colorspace to LAB colorspace

I have this code for Gamut3D in RGB, but I'm looking to convert it into LAB colorspace but I'm not really sure what changes I should be making.

rgbImage = imread('pill1.jpg');
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
imshow(rgbImage);
title('Original Color Image');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Construct the 3D color gamut.
gamut3D = zeros(256,256,256);
for column = 1: columns
for row = 1 : rows
rIndex = redChannel(row, column) + 1;
gIndex = greenChannel(row, column) + 1;
bIndex = blueChannel(row, column) + 1;
gamut3D(rIndex, gIndex, bIndex) = gamut3D(rIndex, gIndex, bIndex) + 1;
end
end
% Get a list of non-zero colors so we can put it into scatter3()
% so that we can visualize the colors that are present.
r = zeros(256, 1);
g = zeros(256, 1);
b = zeros(256, 1);
nonZeroPixel = 1;
for red = 1 : 256
for green = 1: 256
for blue = 1: 256
if (gamut3D(red, green, blue) > 1)
% Record the RGB position of the color.
r(nonZeroPixel) = red;
g(nonZeroPixel) = green;
b(nonZeroPixel) = blue;
nonZeroPixel = nonZeroPixel + 1;
end
end
end
end
figure;
scatter3(r, g, b, 3);
xlabel('R' );
ylabel('G' );
zlabel('B' );


The so-called Gamut in the code, is nothing but a 3d color histogram. Colors from the image are collected into it, and then each unique (r,g,b) is being shown as a scatter point.

In order to show the distribution of the colors in LAB space, you can convert your image from sRGB to LAB, (Assuming that the image is in sRGB space) and then run the same procedure that you have now.

In order to convert it, use:

    cform = makecform('srgb2lab');
labImage = applycform(cform, rgbImage);


By the way, the code in your question can be optimized into 4 lines:

    im = imread('peppers.png');
im = reshape(im,[],3);
im = unique(im,'rows');
scatter3(im(:,1),im(:,2),im(:,3));

• That's good to know. But what if I wanted to cluster them together something like what's been done on here. I wish to view them similar to what's been done on this blog blogs.mathworks.com/steve/2010/12/23/two-dimensional-histograms , but not sure what changes I should be making. Oct 15 '12 at 21:30