November 5th, 2009, 07:23 AM
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 rgb-info 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:
H, is more complex though. It rely on whether (G>=B) is true or false.
float H, S, I;
I = ((R+G+B)/3);
if (min>G) min=G;
if (min>B) min=B;
S = 1-(3*(min/(R+G+B)));
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:
H = acos((1/2)*(((R-G)+(R-B))/(sqrt(((R-G)*(R-G))+((R-B)*(G-B))))));
cout << "without 360-part H = " << H << endl; //writes the S value
H = 360-(acos((1/2)*(((R-G)+(R-B))/(sqrt(((R-G)*(R-G))+((R-B)*(G-B)))))));
cout << "360-part H = " << H << endl; //writes the S value
and the correct results would be:
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 H-part, but i think its the same, though im not sure how to implement the squared part. but its on this link: http://www5.informatik.tu-muenchen.d...csc/COL_24.htm
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.