Need some assistance with Mathematica Module

Click For Summary

Discussion Overview

The discussion revolves around a Mathematica algorithm designed to convert RGB values into hue, saturation, and intensity (HSI) values. Participants address issues related to the implementation of the algorithm, particularly focusing on the calculation of hue and the comparison of matrix elements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an algorithm that converts RGB matrices to HSI values but encounters issues with the hue calculation, specifically that the hue value returned is not interpretable.
  • Another participant points out a potential issue with using the symbol 'i' for both intensity and as an iterator, suggesting it should be fixed.
  • A different participant argues that the use of 'i' is not problematic due to its local scope within the Table function, but identifies that comparisons between matrices B and G do not yield True or False, leading to hue not being assigned a value.
  • One participant expresses confusion over the lack of a direct greater than/less than comparison for matrices in Mathematica, contrasting it with MATLAB's capabilities.
  • Another participant offers to create a function that compares corresponding elements of two matrices, asking for clarification on the desired comparison.
  • A participant clarifies that they need to compare individual elements of the matrices rather than the matrices as a whole, specifying the need for element-wise comparisons for hue calculations.
  • A function is proposed that compares corresponding elements of two matrices and returns a matrix of Boolean values indicating the results of the comparisons.
  • The original poster confirms that the proposed function works and expresses gratitude, indicating progress towards resolving the next issue in the algorithm.

Areas of Agreement / Disagreement

Participants generally agree on the need for element-wise comparisons of matrices but have differing views on the implications of using the symbol 'i' in the code. The discussion remains unresolved regarding the overall efficiency and correctness of the hue calculation.

Contextual Notes

Participants note limitations in Mathematica's ability to compare matrices directly for inequalities, which affects the algorithm's functionality. There are also unresolved concerns about the efficiency of the algorithm as matrix dimensions increase.

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 6 ·
Replies
6
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K