# RGB to HSV and then quantization of H and S into 72 and 20 bins respectively.

6 views (last 30 days)
Nitin Arora on 20 Oct 2021
Commented: Image Analyst on 20 Oct 2021
I have a RGB image that needs to be converted into HSV space. And then need to convert H and S components into 72 and 20 bins respectively.
RGB to HSV part comleted simply by using HSV = rbg2hsv(RGB) command. Can anyone help in 2nd part i.e. to convert H and S components into 72 and 20 bins?

Image Analyst on 20 Oct 2021
Edited: Image Analyst on 20 Oct 2021
Try this:
subplot(2, 2, 1);
imshow(rgbImage);
title('Original RGB Image')
impixelinfo;
% Convert into HSV color space.
hsvImage = rgb2hsv(rgbImage);
hImage = hsvImage(:, :, 1); % Extract only the hue channel.
sImage = hsvImage(:, :, 2); % Extract only the saturation channel.
subplot(2, 2, 2);
imshow(hImage);
title('Original H Image')
impixelinfo;
% Quantize/posterize into 20 bins.
numberOfBins = 72;
edges = linspace(0, 1, numberOfBins+1)
discreteH = discretize(hImage, edges) / numberOfBins;
subplot(2, 2, 3);
imshow(discreteH)
title('Quantized H Image')
impixelinfo;
% Repeat with 20 bins for S channel.
% Quantize/posterize into 20 bins.
numberOfBins = 20;
edges = linspace(0, 1, numberOfBins+1)
discreteS = discretize(sImage, edges) / numberOfBins;
subplot(2, 2, 4);
imshow(discreteS)
title('Quantized S Image')
impixelinfo; ##### 2 CommentsShowHide 1 older comment
Image Analyst on 20 Oct 2021
If it worked, yoiu can thank me by clicking the "Vote" icon and the "Accept this answer" link.
You can take a histogram of however many bins you want but it probably makes sense to use the number 70 or 20 and you can do that by passing edges into histogram
edgesH = linspace(0, 1, numberOfBins+1)
discreteH = discretize(hImage, edgesH) / numberOfBins;
subplot(2, 2, 3);
imshow(discreteH)
histogram(discreteH, edgesH);

R2021a

### Community Treasure Hunt

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

Start Hunting!