Creating a histogram in 3D using LAB colorspace

12 views (last 30 days)
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)

Image Analyst
Image Analyst on 23 Sep 2012
Edited: Image Analyst on 23 Sep 2012
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:

Lab Rat
Lab Rat on 23 Sep 2012
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

Image Analyst
Image Analyst on 24 Sep 2012
Edited: Image Analyst on 24 Sep 2012
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
Image Analyst
Image Analyst on 8 Aug 2014
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 Just for fun 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!