Changing bin sizes for scatter plot

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

Réponses (1)

Image Analyst
Image Analyst le 4 Nov 2012
Modifié(e) : Image Analyst le 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 commentaires

Lab Rat
Lab Rat le 5 Nov 2012
Haha. Of course it is, when its yours. Just for understanding purposes, can you just explain a bit on how that part of the code works ? I'm having trouble understanding that part of it.
Image Analyst
Image Analyst le 5 Nov 2012
Modifié(e) : Image Analyst le 5 Nov 2012
Well let's say you wanted 8 bins instead of 256, so values of 0-7 go into bin 1, values of 8-15 go into bin 2, etc. So a value of 9 instead of going into bin #9 would go into bin 2. So basically you have to divide by 8, round down to the next lower integer, and add 1.
Lab Rat
Lab Rat le 5 Nov 2012
If I've understood that right, wouldn't the number of pixel representations on the scatter plot be the same for any bin size ? However, I'm not sure if it is something that I'm doing wrong, but the lesser the number of bins, the fewer pixel representations are what I see on the scatter plot.
Image Analyst
Image Analyst le 6 Nov 2012
No. The number of scatter points will of course be fewer because you have fewer bins. However if you add up the counts in all bins, then the total should be the same, and equal the number of pixels in the image. To view this is a 3D scatter plot you'd have to make the size, color, or intensity of the markers change depending on the count value for that marker.
Lab Rat
Lab Rat le 6 Nov 2012
You mean to say that each scatter point in the bins represents one or more pixels of the same value and that each scatter point is not exactly a one to one representation of a pixel value?
Image Analyst
Image Analyst le 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.

Connectez-vous pour commenter.

Question posée :

le 4 Nov 2012

Community Treasure Hunt

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

Start Hunting!

Translated by