Need help with a ForAll[] in Mathematica

  • Mathematica
  • Thread starter Agent M27
  • Start date
  • #1
171
0
As you may have seen in previous postings I am developing a code that will ultimately take as input the Red, Green, Blue channels of an image and convert each pixel value to its corresponding Hue, Saturation, Intensity value. I have gotten a great deal of the code to work properly but am really hitting a stumbling block when it comes to the hue calculation. A little background on hue calculation. Hue is based off the inequality of the proportions of blue to green. If there is more blue in say element B[i,j] than there is green in element G[i,j], that is a greater value number,it then needs to perform a specific formula based on this inequality. This is the tricky part. As you can see from my code I have made use of a ForAll to define the parameters of hue, but it is basing it's calculations solely off of the first element and going with that which is incorrect, it needs to check each element for this inequality. If anyone could offer some advice around this issue it would be greatly appreciated. Thanks in advance.

Joe
 

Attachments

  • RGBconvertHSI ForAll 11_27.nb
    22.7 KB · Views: 327

Answers and Replies

  • #2
1,069
29
ForAll is not what you want.

I am not at all certain what you are trying to do with this, but maybe this will help you.

Map[If[#,360 - ArcCos[(.5((r1 - g1) + (r1 - b1)))/(((r1 - g1)^2 + (r1 - b1)(g1 - b1))^(1/2))]*180/Pi,ArcCos[(.5((r1 - g1) + (r1 - b1)))/(((r1 - g1)^2 + (r1 - b1)(g1 - b1))^(1/2))]*180/Pi]&,matGreater]

Map[function,list] is going to go through that list and apply the function to each element. So what I wrote above is a big complicated If[] function that will look at each element of your matGreater and depending on whether the element is True or False will give you one or the other results of the If[]. When all this is done Map[] will put all those results into a list and that will be your result.

It is not clear to me where your r1, b1 and g1 are coming from. Are those supposed to be picked up out of your list and be potentially different for each of your tests? I suspect this is true but I cannot tell.

Could you perhaps forget Mathematica, and all the complexity that brings in, temporarily and just write the most stunningly clear description of what you really need to accomplish? It might take you days to get that description so well written that someone who knows nothing about what you are trying to do could read it and understand exactly what needs to be done. But with that description I suspect it will be possible to write far simpler and correct Mathematica code.
 
  • #3
171
0
Thank you for all your assistance throughout this process Bill. So here is an explicit rundown of what I am after. Starting with the idea, I am trying to develop a module which will take input matrices which will represent the red, green, and blue channels of a digital image. This module will evaluate each pixel, or element, and do several calculations to convert each pixel into an hue, saturation, and intensity value. The last two operations, saturation and intensity, have been figured out, but I will explain what they are doing since it may help with the hue problem.

So we begin with a cube of equal length one, inside of which all red, green, and blue pixel values will lie, these are denoted by r,g,b=R,G,B/255. Once these values are obtained they are used to calculate the intensity. Going back to the cube, the line of intensity runs from 0,0,0 to 1,1,1 inside the cube, so intensity is the average of the three channels. Once along this intensity line we can form a plane triangle which has each of its vertices at {(0,0,1) (0,1,0) (1,0,0)}. To find where those r,g,b values lie on this triangle we convert them into r1,g1,b1 by taking the average of the normalized values which is done by r1,g1,b1=r,g,b/(r+g+b). Once these values are obtained we can use them to find the saturation and hue of each pixel/element in the matrix.

Saturation can be thought of as a vector emanating from the line of intensity in the center of the plane triangle. For saturation I created a composite matrix to represent a real image, where each element is a triplet of r1,g1,b1. Saturation is based off the lowest valued pixel of the three which is done using the Min[] function, these values are between (0,1) and are returned in a matrix detailing the saturation for each individual pixel. Hue is the last part and is confusing to me as to why it is somehow different.

Hue is the angular separation of the r1,g1,b1 pixels with respect to some reference point which is built into the equations I used. So for the hue calculation we need to first evaluate the input matrices which is what I did using your recommendation of matGreater, which is very useful by the way thanks again. Once we do this matGreater figures out which elements of blue are greater than green, and we can begin calculating hue for each pixel in R,G,B based on the values of r1,g1,b1. As you can see when B is greater than G so to will b1 and we use a specific calculation on this element using the values located in the same element of r1, g1, & b1. From here I want to be able to construct a matrix with values between (0,360) which will denote the hue angle of each pixel. Sorry for being so short in the beginning, I sometimes get caught up and forget not everyone is working on the same things I am, thanks for your patience. Take care.

Joe
 
  • #4
1,069
29
OK. It looks like you have made a serious effort here and aren't just begging for someone to do your work for you.

I Google for rgb to hue saturation and I in the top six hits I find http://support.microsoft.com/kb/29240

Is that source code the simplest description of what you need to do that you have seen? Or have you found something even simpler and easier to understand out there to base this on?

If you haven't found anything simpler then that web page gives you a Hue calculation in about a dozen lines. If you have a better example calculation we can use it instead.

Either way we now need to translate whatever the source language is into Mathematica.

When I look at your Notebook above I assume RGBConvertHSI[] is passed equal length vectors of numeric values. True? And you are scaling 0..255 down to 0..1 for all those values. True?

Now on the next line with your Table of Table of bunch of subscripts of Extract of Map of who knows what, what are you really trying to accomplish? Are you trying to turn r={h,i,j...}, g={k,l,m...}, b={n,o,p...} into rgb={{h,k,n},{i,l,o},{j,m,p},...}? If so then try rgb=Transpose[{r,g,b}] and VERIFY that this works correctly.

Now on the next line with your summ=Total[rgb,{3}] what are you trying to do? I'm very confused by this. Are you trying to add all the values of all the r and g and b together? Or just add the r and separately add the g and separately add the b? What do you want summ to be?

Now on your next line you want to take a Determinant and you say it doesn't work. Determinant works on a (usually square) matrix. Usually Total is going to give you a number, like 12, and the Determinant of 12 needs explaining.

Ok, maybe this is enough to start with, and we haven't even gotten to Hue yet.

Please answer these questions and hopefully we can help you get a working notebook that is far simpler than what you have now.
 
  • #5
1,069
29
I'm experimenting, trying to guess what you are doing.

As a test case begin with
r = {{1, 2}, {3, 4}}; g = {{5, 6}, {7, 8}}; b = {{9, 10}, {11, 12}};
and step by step go through these

rgb=Table[{r[[i,j]],g[[i,j]],b[[i,j]]},{i,Length[r]},{j,Length[First[r]]}]
or you could use this instead
rgb=Partition[Transpose[{Flatten[r],Flatten[g],Flatten}],Length[r]]

summ=Map[Total,rgb,{2}]

Det[summ]

If[Det[summ]==0,(*then*)r1=g1=b1=Table[1/3,{i,Length[r]},{j,Length[First[r]]}],
(*else*) r1=r/(r+g+b); g1=g/(r+g+b); b1=b/(r+g+b)]

r1g1b1comp=Table[{r1[[i,j]],g1[[i,j]],b1[[i,j]]},{i,Length[r1]},{j,Length[First[r1]]}]

Then try values for r,g,b that make Det[summ] zero and nonzero to see if you can assure yourself that every detail of the saturation and intensity calculations are correct. Follow each of those numbers in r,g and b to see they are going the right place. If anything is incorrect then we have to fix my mistakes.
 

Related Threads on Need help with a ForAll[] in Mathematica

Replies
1
Views
2K
Replies
23
Views
10K
Replies
1
Views
3K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
9
Views
12K
  • Last Post
Replies
1
Views
2K
Top