How to extract colour descriptor (using histogram of quantized values) from the image?

4 views (last 30 days)
Hello,
I want to extract color features by using histogram of quantized values of color in Hue(H) , Saturation(S),and Value(V) color space . Feature Vectors are generated by considering the values of H=9,S= 3,and V=3 to form the feature vector of size 81 bin.
Can someone help me in writing the code, I already used the code in https://www.mathworks.com/matlabcentral/answers/228178-how-to-extract-colour-descriptor-set-of-features-from-the-image which is shown below:
rgbImage = imread('1.jpg');
[rows, columns, numberOfColorChannels] = size(rgbImage);
hsvImage = rgb2hsv(rgbImage); % Ranges from 0 to 1.
hsvHist = zeros(9,3,3);
for col = 1 : columns
for row = 1 : rows
hBin = floor(hsvImage(row, col, 1) * 9) + 1;
sBin = floor(hsvImage(row, col, 2) * 3) + 1;
vBin = floor(hsvImage(row, col, 3) * 3) + 1;
hsvHist(hBin, sBin, vBin) = hsvHist(hBin, sBin, vBin) + 1;
end
end
but get the following error :
Index exceeds matrix dimensions.
Error in Untitled4 (line 12)
hsvHist(hBin, sBin, vBin) = hsvHist(hBin, sBin, vBin) + 1;
Thanks in advance,

Answers (1)

Harshavardhan
Harshavardhan on 25 Jun 2025
The error you're encountering, "Index exceeds matrix dimensions", is likely due to the quantization step producing an index that exceeds the size of your histogram array. This can happen when the value of “hsvImage(row, col, 1) * 9” equals 9, which after floor() becomes 9, and then adding 1 gives 10 — which is out of bounds for a 9-bin array.
To fix this, you need to cap the bin indices to their maximum values to avoid exceeding the array dimensions.
Here is the modified “for” loop code:
for col = 1 : columns
for row = 1 : rows
hBin = min(floor(hsvImage(row, col, 1) * 9) + 1, 9);
sBin = min(floor(hsvImage(row, col, 2) * 3) + 1, 3);
vBin = min(floor(hsvImage(row, col, 3) * 3) + 1, 3);
hsvHist(hBin, sBin, vBin) = hsvHist(hBin, sBin, vBin) + 1;
end
end
For more information on “min” refer to the link below:

Community Treasure Hunt

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

Start Hunting!