- #1
Agent M27
- 171
- 0
I am currently working on an algorithm which will ultimately be able to take as input a 3 matrices, (R,G,B) and convert each pixel value into hue saturation and intensity values. So far I have been able to get all but the hue portion to work properly. Here is the code thus far:
RGBconvertHSI[R_, G_, B_] :=
Module[{r, g, b, r1, g1, b1, h, s, i},
r = R/255;
g = G/255;
b = B/255;
r1 = r/(r + g + b);
g1 = g/(r + g + b);
b1 = b/(r + g + b);
s =
Table[
Table[
1 - 3*Min[
r1[][[j]],
g1[][[j]],
b1[][[j]]
],
{
j, 1, Dimensions[r][[1]]
}
],
{
i, 1, Dimensions[r][[1]]
}
];
i = (r + g + b)/3;
Which[
r1 == 1/3,
h = 0,
B > G,
h =
360 - ArcCos[(.5 ((r1 - g1) + (r1 -
b1)))/(((r1 - g1)^2 + (r1 - b1) (g1 - b1))^(1/2))]*180/
Pi,
B <= G,
h =
ArcCos[(.5 ((r1 - g1) + (r1 -
b1)))/(((r1 - g1)^2 + (r1 - b1) (g1 - b1))^(1/2))]*180/
Pi
];
Print["Hue=", {N[h, 2]}, ",",
"Saturation=", {N[s, 2]} // MatrixForm, ",",
"Intensity=", {N[i, 2]} // MatrixForm]]
R = RandomInteger[{0, 255}, {2, 2}];
G = RandomInteger[{0, 255}, {2, 2}];
B = RandomInteger[{0, 255}, {2, 2}];
RGBconvertHSI[R, G, B]
When I execute the program, I get the following return:
Hue={h$9239},Saturation={{{0.88,0.54},{0.095,0.96}}},Intensity={{{0.32,0.49},{0.69,0.52}}}
I can't make heads or tails of what the value for hue that is being returned. There are no errors or red flags, so if anyone could help me understand this issue, it would be greatly appreciated. Everything else is fine. Although it seems to be pretty CPU intensive when the dimensions of the matrices increase, so I will need to try and make it more efficient, but one thing at a time. Thanks.
Joe
Edit: I have attached the files in a more legible form. Thanks
RGBconvertHSI[R_, G_, B_] :=
Module[{r, g, b, r1, g1, b1, h, s, i},
r = R/255;
g = G/255;
b = B/255;
r1 = r/(r + g + b);
g1 = g/(r + g + b);
b1 = b/(r + g + b);
s =
Table[
Table[
1 - 3*Min[
r1[][[j]],
g1[][[j]],
b1[][[j]]
],
{
j, 1, Dimensions[r][[1]]
}
],
{
i, 1, Dimensions[r][[1]]
}
];
i = (r + g + b)/3;
Which[
r1 == 1/3,
h = 0,
B > G,
h =
360 - ArcCos[(.5 ((r1 - g1) + (r1 -
b1)))/(((r1 - g1)^2 + (r1 - b1) (g1 - b1))^(1/2))]*180/
Pi,
B <= G,
h =
ArcCos[(.5 ((r1 - g1) + (r1 -
b1)))/(((r1 - g1)^2 + (r1 - b1) (g1 - b1))^(1/2))]*180/
Pi
];
Print["Hue=", {N[h, 2]}, ",",
"Saturation=", {N[s, 2]} // MatrixForm, ",",
"Intensity=", {N[i, 2]} // MatrixForm]]
R = RandomInteger[{0, 255}, {2, 2}];
G = RandomInteger[{0, 255}, {2, 2}];
B = RandomInteger[{0, 255}, {2, 2}];
RGBconvertHSI[R, G, B]
When I execute the program, I get the following return:
Hue={h$9239},Saturation={{{0.88,0.54},{0.095,0.96}}},Intensity={{{0.32,0.49},{0.69,0.52}}}
I can't make heads or tails of what the value for hue that is being returned. There are no errors or red flags, so if anyone could help me understand this issue, it would be greatly appreciated. Everything else is fine. Although it seems to be pretty CPU intensive when the dimensions of the matrices increase, so I will need to try and make it more efficient, but one thing at a time. Thanks.
Joe
Edit: I have attached the files in a more legible form. Thanks
Attachments
Last edited: