Calculating an HSV histogram

I have to compute an HSV histogram of a colored image. Is it same as an H-S histogram?

I have found H-S histogram computation code everywhere. What is the method to change an H-S histogram to an HSV?

• Is you end-goal a marginal histogram, that is, one histogram for each variable dimension (H, S and V respectively), or a joint histogram of all three variables at once? Jul 19 '14 at 10:03

2 Answers

Since you want an histogram, I believe you want to plot it. In GNU Octave you may use hist3 and make three histograms (one for each pair: HxS, HxV and SxV). The code follows bellow:

pkg load statistics
I = imread ('lena.png');
sI = size (I);
Ihsv = rgb2hsv (I);
figure ("position",get(0,"screensize")./[1 1 2 3]);
subplot (1,3,1);
hist3 ( reshape (permute (Ihsv(:,:,[1,2]), [3, 1, 2]), 2, prod(sI(1:2)))' );
title ('H vs S'); xlabel ('H'); ylabel ('S');
subplot (1,3,2);
hist3 ( reshape (permute (Ihsv(:,:,[1,3]), [3, 1, 2]), 2, prod(sI(1:2)))' );
title ('H vs V'); xlabel ('H'); ylabel ('V');
subplot (1,3,3);
hist3 ( reshape (permute (Ihsv(:,:,[2,3]), [3, 1, 2]), 2, prod(sI(1:2)))' );
title ('S vs V'); xlabel ('S'); ylabel ('V');


They are not the same. HSV histogram would involve computing the frequencies in a 3D cube where the volume is (depending on the implementation) 360x255x255. Due to the memory requirements, this is often not preferred (~90MB) (This might seem pretty small nowadays, but traversing this histogram is very inefficient because one cannot benefit from random accesses over CPU cache. In other words, accessing elements of the histogram - even to fill the entire volume causes tons of cache misses). In brief, I don't recommend computing this at all.

On the other hand, HS histogram is a simple 2D joint histogram with only 255x255 values. It can be cached and used even in real-time applications safely.

• Also think that the histogram will be essentially void, with less than 0.05 pixel per bin for a 1 Mpixel image.
– user7657
Mar 16 '15 at 8:14
• In that case you could use sparse storage, to save certain cache misses and to avoid unnecessary memory allocation. Sep 12 '15 at 7:55
• ok for parse storage, but my point is more about the usefulness of such a histogram where most nonzero frequencies are just ones.
– user7657
Sep 12 '15 at 8:48
• It totally depends on your data and representation. If you have a quantized / cartoon-like image, they won't all be 1s. Nevertheless, the question is related to the implementation, not the philosophical usefulness. Sep 12 '15 at 15:33