Creating a histogram in 3D using LAB colorspace

I'm trying to segment an image using kmeans, but I'm trying to find the number of clusters by creating a 3D histogram in the LAB colorspace. I'm looking for a histogram similar to http://blogs.mathworks.com/steve/2010/12/23/two-dimensional-histograms/ but in all the 3 dimensions. So I'd appreciate any help with this.

Answers (3)

Try this: http://rsb.info.nih.gov/ij/plugins/color-inspector.html It can be downloaded and run from within MATLAB using code like this that I've put into a shortcut:
% Windows installation instructions:
% First, download and install ImageJ from here:
% http://rsb.info.nih.gov/ij/download.html
% Then download the 3D Color Inspector plug-in for ImageJ here:
% http://rsb.info.nih.gov/ij/plugins/color-inspector.html
% and save it here:
% C:/Program Files/ImageJ/plugins/Color
%
% Now we need to enable the plug-in to work with MATLAB.
% First open your classpath file:
% C:\Program Files\MATLAB\R2012b\toolbox\local\classpath.txt
% for editing. Then add the following lines as new lines to your classpath file:
% C:/Program Files/ImageJ/ij.jar
% C:/Program Files/ImageJ/plugins/Color/Color_Inspector_3D.jar
clc; % Clear the MATLAB command window.
% User must browse for the file they want to open.
% Set up a starting/initial folder that they will start browsing from.
startingFolder = pwd; % Change to something specific if you want.
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
% Bring up the "Open File" dialog box.
[baseFileName, folder] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','Image Files (jpg,tif,png,bmp,gif)';...
'*.*','All Files (*.*)' },'Specify image file', startingFolder);
if baseFileName ~= 0
% If they didn't click Cancel, build the full filename:
fullFileName = fullfile(folder, baseFileName)
% Now pass that into the 3D Color Inspector plugin, and you're done.
color_inspector(fullFileName)
end
You might also be interested in the "color frequency image". Code for it is here:
Thanks! That looks to be an excellent tool. But this is kind of an assignment, so I'm trying to code it myself albeit with a bit of help. From the link that I've pasted above, the author mentions that it is possible to modify the code a bit to view the histogram in 3D, so I've made a few changes but with no result. Here is the code so far
if true
I=imread('pill2.jpg');
imshow(I);
cform= makecform('srgb2lab');
im=applycform(I,cform);
im = lab2double(im);
l = im(:,:,1);
a = im(:,:,2);
b = im(:,:,3);
N = 101;
bin_centers = linspace(-100, 100, N);
subscripts = 1:N;
ai = interp1(bin_centers, subscripts, a, 'linear', 'extrap');
bi = interp1(bin_centers, subscripts, b, 'linear', 'extrap');
li = interp1(bin_centers, subscripts, l, 'linear', 'extrap');
ai = round(ai);
bi = round(bi);
li = round(li);
ai = max(min(ai, N), 1);
bi = max(min(bi, N), 1);
li = max(min(li, N), 1);
H = accumarray([bi(:), ai(:), li(:)], 1, [N N N]);
imshow(H);
end
Well then just follow the demo given by Mathworks for this: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html
Or, why not just create the 3D scatter plot of the RGB color gamut. You can construct 2D histograms by projecting them along the 3 different axial dimensions. See this demo:
% Creates a 3D scatter plot of an RGB color gamut of a color image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% 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', 'FontSize', fontSize);
% 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', 'FontSize', fontSize);
ylabel('G', 'FontSize', fontSize);
zlabel('B', 'FontSize', fontSize);

8 Comments

Am I able to use this code for LAB rather than RGB ?
Yes, with modifications. You will need to see have your loop counters converted into a bin number, but that's not too hard. Like
for L = 0 : 100
lBin = int32(L)
for A = -30 : 30
ABin = int32(A + 30);
or something like that.
I tried this out but I'm not able to achieve the result that I'm looking for. Do you mind elaborating on the last response ?
I am trying to use a technique similar to the one mentioned here http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html , but I need the colors in the all the 3 color spaces L, a and b in order to determine the number of clusters. Using a and b becomes a problem when the image has only black and white, since it would show up as a single cluster
Yeah, so don't use LAB segmentation. Just convert to grayscale and use traditional gray level segmentation methods.
I'm not doing the program specifically for the black and white images, since I'm not going to have any control over the images that are going to be used. They can be anything and I want to make sure I have all the bases covered.
Is it possible to add the plots colors like the origianl plot found http://rsb.info.nih.gov/ij/plugins/color-inspector.html
Yes, you can actually run that program from MATLAB since it's a jar file. Here's a shortcut I made to do it:
% Windows installation instructions:
% First, download and install ImageJ from here:
% http://rsb.info.nih.gov/ij/download.html
% Then download the 3D Color Inspector plug-in for ImageJ here:
% http://rsb.info.nih.gov/ij/plugins/color-inspector.html
% and save it here:
% C:/Program Files/ImageJ/plugins/Color
%
% Now we need to enable the plug-in to work with MATLAB.
% First open your classpath file:
% C:\Program Files\MATLAB\R2013b\toolbox\local\classpath.txt
% for editing. Then add the following lines as new lines to your classpath file:
% C:/Program Files/ImageJ/ij.jar
% C:/Program Files/ImageJ/plugins/Color/Color_Inspector_3D.jar
% Just put them as the first two lines, without any # symbols, right before this line:
% # DO NOT MODIFY THIS FILE.
%
% With Windows 7 you will need to save this to a different folder, like your Documents folder
% or some folder you have write privileges for, because Windows won't let you save
% anything under the Program Files folder. Then copy it over to the
% C:\Program Files\MATLAB\R2013b\toolbox\local folder and say Yes when it asks
% you if it's allowed to do the copy. Close down MATLAB if it's open, then start MATLAB.
% After starting MATLAB, type javaclasspath on the command line
% to verify that the folders have been recognized and added to the javaclasspath.
clc; % Clear the MATLAB command window.
% User must browse for the file they want to open.
% Set up a starting/initial folder that they will start browsing from.
startingFolder = pwd; % Change to something specific if you want.
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
% Bring up the "Open File" dialog box.
[baseFileName, folder] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','Image Files (jpg,tif,png,bmp,gif)';...
'*.*','All Files (*.*)' },'Specify image file', startingFolder);
if baseFileName ~= 0
% If they didn't click Cancel, build the full filename:
fullFileName = fullfile(folder, baseFileName)
% Now pass that into the 3D Color Inspector plugin, and you're done.
color_inspector(fullFileName)
end

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 23 Sep 2012

Commented:

on 8 Aug 2014

Community Treasure Hunt

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

Start Hunting!