Mathematica Need some assistance with Mathematica Module

AI Thread Summary
The discussion revolves around a Mathematica algorithm designed to convert RGB values into hue, saturation, and intensity (HSI) values. The user is struggling with the hue calculation, specifically due to issues with comparing two 2x2 matrices (B and G) using standard comparison operators, which do not return simple True or False values. A solution was proposed to create a function, `matLess`, that compares corresponding elements of the matrices and returns a 2x2 matrix of boolean values. This function successfully addressed the user's need to determine which matrix elements are greater, allowing progress on the hue calculation. The user expressed gratitude for the assistance and indicated they would continue to tackle further issues as they arise.
Agent M27
Messages
169
Reaction score
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
 

Attachments

Last edited:
Physics news on Phys.org
You're using the symbol i for both intensity and as an iterator. This may not be the only problem, but this certainly needs to be fixed before you can get any further.
 
I do not believe the use of i is an issue. The i within the Table[] will be local to that Table[] and have nothing to do with any other use of i outside. It might be questionable style but I do not believe it is the source of the problem.

After testing your code I believe your problem lies in B>G and B<=G. Both B and G are 2x2 matricies. When I initialize B and G and evaluate either B>G or B<=G I do not get back True or False but instead just B>G or B<=G, with the appropriate values substituted for B and G. Check this yourself to confirm this.

So neither the second or third alternative in your Which evaluate to True, thus h is never assigned a value and thus when it is printed it prints the aliased name for h inside a Module, as all variables inside a Module are aliased.

To fix your problem I believe you need to find a way to compare your B and G matricies that will return True or False and this problem should be solved.
 
Thanks Bill, this has been a long standing hurdle in this project. It seems that in mathematica, one can only test for equality of matrices, not necessarily which is the larger valued matrix. I found that in MatLab there is a greater than/less than command for matrices but not in mathematica, but I feel like there should be some sort of translation from one to the other, but I cannot seem to translate it. My only idea for inequality comparisons are by using the determinant, but this doesn't compare the matrices properly because it is based on the values at (a,b,c,d,...) instead of for example is {a11}>{b11} which is what I need. Thanks for your help.

Joe
 
Ok, I've got two matricies {{1,9},{9,9}} and {{4,4},{4,4}}.
Which one is greater and why?

If you can write down a clear simple description of exactly what you want < to do on a pair of 2x2 matricies each containing 4 finite numeric values then I'll write you a function that does it.
 
Well I misspoke when I said greater valued matrix. What I really need to do is to be able to see if {a11} is greater than {b11}. In the example you gave I need B={{1,9},{9,9}} G={{4,4},{4,4}}. I need to find which of the elements from each is the greater of the two. Going back to your example I need it to recognize that {G11} >{B11} and do this for all elements of the matrix. These are the values I want to use in with the hue calculations. Thanks again.

Joe
 
Does this do what you need?

matLess compares corresponding elements of a 2x2 matrix and returns True if the first is less than the second and False otherwise. The result is returned as a 2x2 matrix.
In[1]:= matLess[m1_,m2_]:=Partition[Thread[Less[Flatten[m1],Flatten[m2]]],2];
B={{1,5},{9,3}};
G={{4,5},{6,7}};
matLess[B,G]

Out[4]= {{True,False},{False,True}}

Thus {{1<4,5 not < 5},{9 not < 6,3<7}}
 
YES it works! Thank you so much Bill. Now I am moving along to the next issue which is telling the module to interpret those booleans and do the appropriate thing with them. I will be back if need be, but thank you very much.

Joe
 

Similar threads

Replies
19
Views
2K
Replies
6
Views
7K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
4
Views
3K
Replies
14
Views
3K
Replies
2
Views
3K
Replies
8
Views
3K
Back
Top