From: shilpa khinvasara on
is the following code correct to divide an image into 18,3,3, bins and how to plot its histogram


clc;
close all;
clear all;
clc;
close all;
% Open a standard image.
rgbImage = imread('apple_logo_rainbow_fruit.jpg');

% Display the original image.
subplot(2, 4, 1);
imshow(rgbImage, []);
title('Original RGB image');

% Convert to HSV color space
hsvimage = rgb2hsv(rgbImage);

[x,y]=hist(hsvimage,255);
subplot(2, 4, 8);
bar(y, x);
title('original Histogram');
% Extract out the individual channels.
h = hsvimage(:,:,1);
s = hsvimage(:,:,2);
v = hsvimage(:,:,3);
h=h*360;
[M,N,O]=size(rgbImage);
%when v<0.2,it is a black area;when s<0.2&0.2=<v<0.8,it is a gray area
for i=1:M
for j=1:N

if v(i,j)<0.25
L(i,j)=0;
end
if v(i,j)>0.25&& 0<s(i,j)<=0.067
L(i,j)=1;
end
if v(i,j)>0.25&& 0.067<s(i,j)<=0.126
L(i,j)=2;
end
if v(i,j)>0.25&& 0.126<s(i,j)<=0.2
L(i,j)=3;
end

end
end

for i = 1:M
for j = 1:N
if h(i,j)>0&&h(i,j)<=20
H(i,j) = 0;
end
if h(i,j)>20&&h(i,j)<=40
H(i,j)=1;
end
if h(i,j)>40&&h(i,j)<=60
H(i,j)=2;
end
if h(i,j)>60&&h(i,j)<=80
H(i,j)=3;
end
if h(i,j)>80&&h(i,j)<=100
H(i,j)=4;
end
if h(i,j)>100&&h(i,j)<=120
H(i,j)=5;
end
if h(i,j)>120&&h(i,j)<=140
H(i,j)=6;
end
if h(i,j)>140&&h(i,j)<=160
H(i,j)=7;
end
if h(i,j)>160&&h(i,j)<=180
H(i,j)=8;
end
if h(i,j)>180&&h(i,j)<=200
H(i,j)=9;
end
if h(i,j)>200&&h(i,j)<=220
H(i,j)=10;
end
if h(i,j)>220&&h(i,j)<=240
H(i,j)=11;
end
if h(i,j)>240&&h(i,j)<=260
H(i,j)=12;
end
if h(i,j)>260&&h(i,j)<=280
H(i,j)=13;
end
if h(i,j)>280&&h(i,j)<=300
H(i,j)=14;
end
if h(i,j)>300&&h(i,j)<=320
H(i,j)=15;
end
if h(i,j)>320&&h(i,j)<=340
H(i,j)=16;
end
if h(i,j)>340&&h(i,j)<=360
H(i,j)=17;
end
end
end
for i = 1:M
for j = 1:N
if s(i,j)>0.2&&s(i,j)<=0.65
S(i,j)=0;
end
if s(i,j)>0.65&&s(i,j)<=1
S(i,j)=1;
end
end
end
for i=1:M
for j=1:N
if v(i,j)>0.25&&v(i,j)<=0.7
V(i,j)=0;
end
if v(i,j)>0.7&&v(i,j)<=1
V(i,j)=1;
end
end
end

for i=1:M
for j=1:N
if s(i,j)>0.2&&s(i,j)<=1&&v(i,j)>0.25&&v(i,j)<=1
L(i,j)=9*H(i,j)+3*S(i,j)+V(i,j)+4;
end
end

end
[q,r]=hist(L(i,j),165);
subplot(2, 4, 7);
bar(r, q);
title('quantized Histogram');

figure,hist(L(i,j),166);
From: Walter Roberson on
shilpa khinvasara wrote:
> is the following code correct to divide an image into 18,3,3, bins and
> how to plot its histogram

Correct? No, definitely not.

> clc;
> close all;
> clear all;
> clc;
> close all;

But in my opinion, if your code uses clc and "close all" multiple times
then it is either incorrect or not well written.

> % Open a standard image.
> rgbImage = imread('apple_logo_rainbow_fruit.jpg');
>
> % Display the original image.
> subplot(2, 4, 1);
> imshow(rgbImage, []);
> title('Original RGB image');
>
> % Convert to HSV color space
> hsvimage = rgb2hsv(rgbImage);
> [x,y]=hist(hsvimage,255);
> subplot(2, 4, 8);
> bar(y, x);
> title('original Histogram');
> % Extract out the individual channels.
> h = hsvimage(:,:,1); s = hsvimage(:,:,2); v = hsvimage(:,:,3); h=h*360;
> [M,N,O]=size(rgbImage); %when v<0.2,it is a black area;when
> s<0.2&0.2=<v<0.8,it is a gray area
> for i=1:M
> for j=1:N
> if v(i,j)<0.25
> L(i,j)=0;
> end
> if v(i,j)>0.25&& 0<s(i,j)<=0.067
> L(i,j)=1;
> end
> if v(i,j)>0.25&& 0.067<s(i,j)<=0.126
> L(i,j)=2;
> end
> if v(i,j)>0.25&& 0.126<s(i,j)<=0.2
> L(i,j)=3;
> end
> end
> end

You don't need to loop.

L = zeros(M,N);
vplaces = v > 0.25;
L(vplaces & s <= 0.2) = 3;
L(vplaces & s <= 0.126) = 2;
L(vplaces & s <= 0.67) = 1;

and you don't need to explicitly set L(~vplaces) to 0 because it was
initialized to 0 by using zeros() to allocate the memory.

The test 0<s(i,j)<=0.067 would be parsed as (0<s(i,j)) <= 0.067 . The
first of the comparisons would result in a logical 0 or 1 value, and
that logical 0 or 1 value would then be compared to 0.067 .

There is no specific operator in Matlab to compare something to a range
of values. 0 < s(i,j) & s(i,j) <= 0.067 is as close as it gets.

Your tests that compare v to 0.25 do not appear to conform to your
comments about v of 0.2 being the boundary between black and gray.

You test v(i,j) < 0.25 in one place, and the other places you test
v(i,j) > 0.25 . What if v(i,j) is 0.25 exactly? Your code missed that
possibility.


There is no point in my going through the rest of your code: the above
should be enough to get you going.

I will leave you with a further hint, though:
floor(h * (1-eps) / 20)