
November 5th, 2009, 07:23 AM
#1
Implementing an algorithm, math error.
Hi, im making a program that can convert RGB to HSI. Hue, Saturation and Intensity.
HSI is abit more complex than HSV and HSB.
Im using OpenCV for doing it. But i however cant used the premade funcs in opencv.
Im loading my input image into a matrix, and im dragging out the rgbinfo i want.
Done in a loop for x,y.
i have succesfully made the conversion of S and I, but im stuggling alot with H.
This is the formulas for the conversion:
Code equivalent:
Code:
float H, S, I;
I = ((R+G+B)/3);
float min=R;
if (min>G) min=G;
if (min>B) min=B;
S = 1(3*(min/(R+G+B)));
H, is more complex though. It rely on whether (G>=B) is true or false.
Code:
if (G>=B)
{
H = acos((1/2)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB))))));
cout << "without 360part H = " << H << endl; //writes the S value
}
else
{
H = 360(acos((1/2)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB)))))));
cout << "360part H = " << H << endl; //writes the S value
}
when i manually plug in some static RGB values to test if the results is correct, then with (0,255,0) Green i get this:
I=85
S=1
H=1.5708
and the correct results would be:
I=85
S=1
H=120
so my question is, have I implemented it wrong in c++ ?
and how is the right way?
also i have found another formula of the Hpart, but i think its the same, though im not sure how to implement the squared part. but its on this link: http://www5.informatik.tumuenchen.d...csc/COL_24.htm

November 5th, 2009, 07:28 AM
#2
Re: Implementing an algorithm, math error.
Im using codeblocks though, but quite sure it has nothing to do with the program, but error in the c++ language ive made.

November 5th, 2009, 07:29 AM
#3
Re: Implementing an algorithm, math error.
cos^(1) from the formula is not acos. you need 1/cos(...).

November 5th, 2009, 07:38 AM
#4
Re: Implementing an algorithm, math error.
1) You do the following:
This will do integer arithmetic (giving a value of 0). Change to:
2) acos returns the value in radians , not degrees
Code:
H = acos((1.0/2.0)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB))))));
H = H *180 / 3.14159;
cout << "without 360part H = " << H << endl; //writes the S value
The second part will have a similar problem

November 5th, 2009, 07:40 AM
#5
Re: Implementing an algorithm, math error.
if i write "1/cos" it just returns "1" on a 0,255,0 green. Where it should return a 120 value.
Code:
H = 1/cos((1/2)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB))))));

November 5th, 2009, 07:45 AM
#6
Re: Implementing an algorithm, math error.
1) The inverse cosine is the arccosine ... you were correct on that.
2) see my previous post

November 5th, 2009, 07:51 AM
#7
Re: Implementing an algorithm, math error.
Yes, i crossposted, but thanks alot. Changing the ½ to your suggestions and making radians to degrees did the trick.
Code:
//Calculating "H" Hue
if (G>=B)
{
H = acos((1.0/2.0)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB))))));
H = H *180 / 3.14159;
cout << "without 360part H = " << H << endl; //writes the S value
}
else
{
H = (acos((1.0/2.0)*(((RG)+(RB))/(sqrt(((RG)*(RG))+((RB)*(GB)))))));
H = 360(H *180 / 3.14159);
cout << "360part H = " << H << endl; //writes the S value
}
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
