Count colours in HSV
2 次查看(过去 30 天)
显示 更早的评论
Hello I would like to know how to count unique colours in HSV. I need an algotithm not a ready function. I try to do it by creating 3 dimensional matrix (1000,1000,1000) amd iterally putting there one to values of H ,S and V but it's but the matrix is too big and kill my memory.
Do you have any ideas?
0 个评论
回答(1 个)
Image Analyst
2013-5-26
You don't do this in HSV space. You do it in RGB space. With uint8 pixel values, you can have as many as 16.7 million unique colors, or as many colors as you have pixels, if the number of pixels is less than 16.7 million. So you make up an RGB histogram. Here, run 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);
2 个评论
Image Analyst
2013-5-26
To do it in HSV you'd need to figure out what the unique values that H, S, and V take on - you might want to use unique for this. Then iterate over those like I did over RGB in RGB space. If you do it correctly, you will get exactly the same number of unique colors.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!