Need some assistance with Mathematica Module

In summary, the code is working except for the output for the hue component. 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_] :=This is where you should start your summary.
  • #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
 

Attachments

  • RGBconvertHSI Alpha 2.0.nb
    9.6 KB · Views: 564
  • RGBconvertHSI Alpha 2.0.doc
    20.5 KB · Views: 224
Last edited:
Physics news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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
 
  • #5
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.
 
  • #6
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
 
  • #7
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}}
 
  • #8
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
Views
6K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
4
Views
3K
Replies
3
Views
2K
Back
Top