Quantization HSV Space by assigning 8 level each to hue, saturation and value to give a quantized HSV space with 8x8x8=512 histogram bins

Hello,
I will try to quantize the HSV color space but I can not, can anyone help me please?
I want to Quantization HSV Space by assigning 8 level each to hue, saturation and value to give a quantized HSV space with 8x8x8=512 histogram bins.
Thanks

 Risposta accettata

Here is an example of quantization of the HSV colorspace
clc; close all;
rgbImage = imread('peppers.png');
hsvImage = rgb2hsv(rgbImage);
montage(hsvImage,'Size', [1, 3]); title('Before Quantization'); colorbar;
threshRGB = multithresh(hsvImage, 8);
threshForPlanes = zeros(3, 8);
for i = 1:3
threshForPlanes(i, :) = multithresh(hsvImage(:, :, i), 8);
end
quantPlane = zeros(size(hsvImage));
for i = 1:3
value = [0 threshForPlanes(i, 2:end) 1];
quantPlane(:, :, i) = imquantize(hsvImage(:, :, i), threshForPlanes(i, :), value);
end
quantPlane = double(rescale(quantPlane, 0, 1));
figure; montage(quantPlane, 'Size', [1,3]); title('After Quantization');
colormap(parula(8)); colorbar;
Hope this helps!

12 Commenti

Dear Subhadeep,
Thanks for your help, but please I have one question, do you think the number 0, 255 is right for HSV color space as same of RGB?
@ shalaw faraj Yes, you are right, [0 - 1] range is better for HSV.
I have edited the answer. Thanks!
Dear Subhadeep,
Do you think my output was right?
I think no difference between the after and before.
I think you're displaying the same image twice. Can you share the code which you've used to generate the above figures?
clc; close all;
rgbImage = imread('peppers.png');
hsvImage = rgb2hsv(rgbImage);
montage(hsvImage,'Size', [1, 3]); title('Before Quantization'); colorbar;
threshRGB = multithresh(hsvImage, 8);
threshForPlanes = zeros(3, 8);
for i = 1:3
threshForPlanes(i, :) = multithresh(hsvImage(:, :, i), 8);
end
quantPlane = zeros(size(hsvImage));
for i = 1:3
value = [0 threshForPlanes(i, 2:end) 1];
quantPlane(:, :, i) = imquantize(hsvImage(:, :, i), threshForPlanes(i, :), value);
end
quantPlane = double(rescale(quantPlane, 0, 1));
figure; montage(quantPlane, 'Size', [1,3]); title('After Quantization');
colormap(parula(8)); colorbar;
@ shalaw faraj I am getting the correct output when executing the above code.
Which MATLAB version are you using? Also you can try to convert image to double precision before rgb2hsv like below.
rgbImage = im2double(imread('peppers.png'));
Matlab R2017b
but why we have just one subplot and the other two are black? also you have three HSV image output.
I tried your new code im2double but it was the same.
@ shalaw faraj I have been able to reproduce the same issue in R2017b, earlier I was trying it in R2019b.
It seems it is a problem of the function montage, which is not scaling the images properly (this issue is addressed in later releases). However you can use subplot instead of montage. Refer the code below.
Hope this helps!
clc; close all;
rgbImage = im2double(imread('peppers.png'));
hsvImage = rgb2hsv(rgbImage);
figure('Name', 'Before Quantization',...
'units','normalized','outerposition',[0 0 1 1]);
subplot(1, 3, 1); imshow(hsvImage(:, :, 1)); colorbar;
subplot(1, 3, 2); imshow(hsvImage(:, :, 2)); colorbar;
subplot(1, 3, 3); imshow(hsvImage(:, :, 3)); colorbar;
threshRGB = multithresh(hsvImage, 8);
threshForPlanes = zeros(3, 8);
for i = 1:3
threshForPlanes(i, :) = multithresh(hsvImage(:, :, i), 8);
end
quantPlane = zeros(size(hsvImage));
for i = 1:3
value = [0 threshForPlanes(i, 2:end) 1];
quantPlane(:, :, i) = imquantize(hsvImage(:, :, i), threshForPlanes(i, :), value);
end
quantPlane = double(rescale(quantPlane, 0, 1));
figure('Name', 'After Quantization',...
'units','normalized','outerposition',[0 0 1 1]);
subplot(1, 3, 1); imshow(quantPlane(:, :, 1)); colormap(gca, parula(8)); colorbar;
subplot(1, 3, 2); imshow(quantPlane(:, :, 2)); colormap(gca, parula(8)); colorbar;
subplot(1, 3, 3); imshow(quantPlane(:, :, 3)); colormap(gca, parula(8)); colorbar;
It was very helpful, I don't know how can I thanks to you, I have the last question please
how can I equalization separately, for example (18,3,3) instead of (8,8,8).
@ shalaw faraj Instead of using loop you can quantize them individually according to your need
Dear @Subhadeep,
According my question I want to quantize the image RGB from 256*256*256 to 4*4*4 which it give 64 bin of histogram can you help me please?
thanks

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by