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
답변 (1개)
Image Analyst
2012년 11월 4일
편집: Image Analyst
2012년 11월 5일
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
Lab Rat
2012년 11월 5일
Image Analyst
2012년 11월 5일
편집: Image Analyst
2012년 11월 5일
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
2012년 11월 5일
Image Analyst
2012년 11월 6일
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
2012년 11월 6일
Image Analyst
2012년 11월 6일
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.
카테고리
도움말 센터 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
