Changing bin sizes for scatter plot
3 views (last 30 days)
Show older comments
Hi,
I'm using the following code to represent the pixel values of an image on scatter plot, but I wish to change the bin sizes for each of the three dimensions so that I can get a desired scatter plot, but I'm not exactly sure where and what to modify.
if true
clc;
close all;
clear all;
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]);
% Convert RGB colorspace to LAB colorspace
labImage=applycform(rgbImage,makecform('srgb2lab'));
% Extract the individual l, a, and b color channels.
lChannel = labImage(:, :, 1);
aChannel = labImage(:, :, 2);
bChannel = labImage(:, :, 3);
% Construct the 3D color gamut.
gamut3D = zeros(256,256,256);
for column = 1: columns
for row = 1 : rows
lIndex = lChannel(row, column) + 1;
aIndex = aChannel(row, column) + 1;
bIndex = bChannel(row, column) + 1;
gamut3D(lIndex, aIndex, bIndex) = gamut3D(lIndex, aIndex, 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.
l = zeros(256, 1);
a = zeros(256, 1);
b = zeros(256, 1);
nonZeroPixel = 1;
for lax = 1 : 256
for aax = 1: 256
for bax = 1: 256
if (gamut3D(lax, aax, bax) > 1)
% Record the l position of the color.
l(nonZeroPixel) = lax;
a(nonZeroPixel) = aax;
b(nonZeroPixel) = bax;
nonZeroPixel = nonZeroPixel + 1;
end
end
end
end
figure;
scatter3(l, a, b, 3);
xlabel('L' );
ylabel('A' );
zlabel('B' );
end
0 Comments
Answers (1)
Image Analyst
on 4 Nov 2012
Edited: Image Analyst
on 5 Nov 2012
Gee, that code looks awfully familiar. ;-)
You just need to change your indexes to be smaller. So that instead of going from 0-255, you need to go from 0-31 (for example). Thus
lIndex = lChannel(row, column) + 1;
aIndex = aChannel(row, column) + 1;
bIndex = bChannel(row, column) + 1;
would be something like
lIndex = int32(floor(lChannel(row, column) / 8) + 1); % 32 bins instead of 256
aIndex = int32(floor(aChannel(row, column) / 8) + 1);
bIndex = int32(floor(bChannel(row, column) / 8) + 1);
6 Comments
Image Analyst
on 6 Nov 2012
Correct. It's now a "bin" and that bin (just like with any histogram) represents several pixels if the "counts" value is more than 1. The pixels may or may not have the exact same value but if they're all in the same bin, they will at least be close in value. For example if a bin covers gray levels 0-7, then there might be a pixel counted in that bin that has a gray level of 2 and another pixel in the same bin that has a gray level of 6. Any pixel with a gray level anywhere from 0-7 inclusive will be counted in that bin.
See Also
Categories
Find more on Discrete Data Plots 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!