Converting RGB to HSV - java

Hi I want convert from rgb to hsv and I have been following the algorithm from easyRGB.com. But doesn't work it show more red than normal. I rewrite the same algorithm a few time and revised, but I can't find the error. Any idea? There is the algorithm.
public static double[] RGB2HSV(double[] tmp){
double R = tmp[0] / 255.0;
double G = tmp[1] / 255.0;
double B = tmp[2] / 255.0;
double min = Math.min(Math.min(R, G), B);
double max = Math.max(Math.max(R, G), B);
double delta = max - min;
double H = max;
double S = max;
double V = max;
if(delta == 0){
H = 0;
S = 0;
}else{
S = delta / max;
double delR = ( ( ( max - R ) / 6 ) + ( delta / 2 ) ) / delta;
double delG = ( ( ( max - G ) / 6 ) + ( delta / 2 ) ) / delta;
double delB = ( ( ( max - B ) / 6 ) + ( delta / 2 ) ) / delta;
if(R == max){
H = delB - delG;
}else if(G == max){
H = (1/3) + delR - delB;
}else if(B == max){
H = (2/3) + delG - delR;
}
if(H < 0) H += 1;
if(H > 1) H -= 1;
}
double[] hsv = new double[3];
hsv[0] = H;
hsv[1] = S;
hsv[2] = V;
return hsv;
}

The values of 1/3 and (2/3) are 0, because you are operating with two integers, so the result is the integer too.
Use 1.0 / 3.0 and 2.0 / 3.0 instead.

Related

how can I use a counter controlled iteration in the print/math section on this square root program

I was thinking about adding a while loop to the program that'll loop for maybe 10 times instead of writing 10 println statements. but I found it hard since the value of x is different every time in the math section. I wrote this code before, now I want to shorten it. It's a square root finder program that uses the Babylonian method to find the square root of an integer between [S > 20 || S < 400]
int S;
System.out.print("Enter an integer, S: ");
S = myInput.nextInt();
if (S < 0) {
System.out.println("This program can not take the square root of a negative number.");
}
else if (S < 20 || S > 400) {
System.out.println("This value is out of range.");
}
else {
double a = S / 2.0;
double b = S / a;
double c = a + b;
double d = 0.5 * c;
// for x2
double e = S / d;
double f = d + e;
double g = 0.5 * f;
// for x3
double h = S / g;
double i = g + h;
double j = 0.5 * i;
// for x4
double k = S / j;
double l = j + k;
double m = 0.5 * l;
// for x5
double n = S / m;
double o = m + n;
double p = 0.5 * o;
// for x6
double q = S / p;
double r = p + q;
double s = 0.5 * r;
// for x7
double t = S / s;
double u = s + t;
double v = 0.5 * u;
// for x8
double w = S / v;
double x = v + w;
double y = 0.5 * x;
// for x9
double z = S / y;
double aa = y + z;
double ab = 0.5 * aa;
System.out.printf("%nx0 = %8.4f ", a);
System.out.printf("%nx1 = %8.4f ", d);
System.out.printf("%nx2 = %8.4f ", g);
System.out.printf("%nx3 = %8.4f ", j);
System.out.printf("%nx4 = %8.4f ", m);
System.out.printf("%nx5 = %8.4f ", p);
System.out.printf("%nx6 = %8.4f ", s);
System.out.printf("%nx7 = %8.4f ", v);
System.out.printf("%nx8 = %8.4f ", y);
System.out.printf("%nx9 = %8.4f ", ab);
}
All you need to do is set d to 2.0 outside the loop. Then use d in place of 2.0 inside the loop. The loop index of i is also used to name the iterations (x0, x1, x2, ...) when printing.
double d = 2.0; // set d to 2.0 here
for (int i = 0; i < 10; i++) {
double a = S / d; // use d here, the modified value will be used again
double b = S / a;
double c = a + b;
d = 0.5 * c;
System.out.printf("x%d = %8.4f%n", i, a);
}
prints the following for the input value of 50
x0 = 25.0000
x1 = 3.7037
x2 = 5.8127
x3 = 6.9374
x4 = 7.0698
x5 = 7.0711
x6 = 7.0711
x7 = 7.0711
x8 = 7.0711
x9 = 7.0711
Here is an exercise. Compare the current computed value to the last. If they are equal (or their difference is small), then you can exit the loop since repeated iterations won't improve the accuracy very much.

Modulo operation will produce no negative numbers

I have a little method in java to calculate coordinates. But if one of the coodinates are negative, I only get a 0 instead of the negative number.
private static void highlightIslandBorders(Location loc) {
World world = loc.getWorld();
int sx = loc.getBlockX();
sx -= sx % islandSize;
int sz = loc.getBlockZ();
sz -= sz % islandSize;
if ((sx < 0) || (sz < 0)) {
return;
}
int ex = sx + islandSize - 1;
int ez = sz + islandSize - 1;
int y = loc.getBlockY() - 1;
Material cornerMat = Material.GLOWSTONE;
world.getBlockAt(sx, y, sz).setType(cornerMat);
world.getBlockAt(ex, y, sz).setType(cornerMat);
world.getBlockAt(sx, y, ez).setType(cornerMat);
world.getBlockAt(ex, y, ez).setType(cornerMat);
}
Where exactly do you get the 0?
int islandSize = 3;
int sx = -100;
System.out.println("Mod: " + (sx % islandSize));
sx -= sx % islandSize;
System.out.println("sx: " + sx);
Results in
Mod: -1
sx: -99

How to calculate the angle between two vectors?

We are trying to get the cos value between v and u but we are getting results much higher than 1 or lesser than 0
Where :
vx = in.nextInt(); // x speed of your pod
vy = in.nextInt(); // y speed of your pod
int ux = nextCheckPointIdX - x;
int uy = nextCheckPointIdY - y;
Here is the formula :
double cos = (vx*ux + vy*uy) / ( Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) + Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2)) );
Do you find any errors in the previous line ?
The denominator was having the problem.
int num = (vx*ux + vy*uy);
double den = (Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) * (Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2))) );
double cos = num / den;
System.out.println(cos);
System.out.println(Math.acos(cos));

Perspective View to Parallel View

in the example which is given in the book "Computer Graphics for Java Programmers, Second Edition"
there is transformation with view vector
private void shiftToOrigin()
{ float xwC = 0.5F * (xMin + xMax),
ywC = 0.5F * (yMin + yMax),
zwC = 0.5F * (zMin + zMax);
int n = w.size();
for (int i=1; i<n; i++)
if (w.elementAt(i) != null)
{ ((Point3D)w.elementAt(i)).x -= xwC;
((Point3D)w.elementAt(i)).y -= ywC;
((Point3D)w.elementAt(i)).z -= zwC;
}
float dx = xMax - xMin, dy = yMax - yMin, dz = zMax - zMin;
rhoMin = 0.6F * (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
rhoMax = 1000 * rhoMin;
rho = 3 * rhoMin;
}
private void initPersp()
{
float costh = (float)Math.cos(theta),
sinth = (float)Math.sin(theta),
cosph = (float)Math.cos(phi),
sinph = (float)Math.sin(phi);
v11 = -sinth; v12 = -cosph * costh; v13 = sinph * costh;
v21 = costh; v22 = -cosph * sinth; v23 = sinph * sinth;
v32 = sinph; v33 = cosph;
v43 = -rho;
}
float eyeAndScreen(Dimension dim)
// Called in paint method of Canvas class
{ initPersp();
int n = w.size();
e = new Point3D[n];
vScr = new Point2D[n];
float xScrMin=1e30F, xScrMax=-1e30F,
yScrMin=1e30F, yScrMax=-1e30F;
for (int i=1; i<n; i++)
{
Point3D P = (Point3D)(w.elementAt(i));
if (P == null)
{ e[i] = null; vScr[i] = null;
}
else
{ float x = v11 * P.x + v21 * P.y;
float y = v12 * P.x + v22 * P.y + v32 * P.z;
float z = v13 * P.x + v23 * P.y + v33 * P.z + v43;
Point3D Pe = e[i] = new Point3D(x, y, z);
float xScr = -Pe.x/Pe.z, yScr = -Pe.y/Pe.z;
vScr[i] = new Point2D(xScr, yScr);
if (xScr < xScrMin) xScrMin = xScr;
if (xScr > xScrMax) xScrMax = xScr;
if (yScr < yScrMin) yScrMin = yScr;
if (yScr > yScrMax) yScrMax = yScr;
}
}
float rangeX = xScrMax - xScrMin, rangeY = yScrMax - yScrMin;
d = 0.95F * Math.min(dim.width/rangeX, dim.height/rangeY); //d burada
imgCenter = new Point2D(d * (xScrMin + xScrMax)/2,
d * (yScrMin + yScrMax)/2);
for (int i=1; i<n; i++)
{
if (vScr[i] != null){vScr[i].x *= d; vScr[i].y *= d;}
}
return d * Math.max(rangeX, rangeY);
// Maximum screen-coordinate range used in CvHLines for HP-GL
}
here float xScr = -Pe.x/Pe.z, yScr = -Pe.y/Pe.z; when we divide x and y with z that give as a perspective view if we don't divide it with z the view will be parallel(orthogonal)
this is OK but if we want to this parallel view coordinates with hidden line algorithm in same book it calculates lines wrongly. I couldn't find where problem is. What can cause this problem?
here hidden line algorithm:
private void lineSegment(Graphics g, Point3D Pe, Point3D Qe,
Point2D PScr, Point2D QScr, int iP, int iQ, int iStart)
{
double u1 = QScr.x - PScr.x; //t
double u2 = QScr.y - PScr.y; //t
double minPQx = Math.min(PScr.x, QScr.x);//t
double maxPQx = Math.max(PScr.x, QScr.x);//t
double minPQy = Math.min(PScr.y, QScr.y);//t
double maxPQy = Math.max(PScr.y, QScr.y);//t
double zP = Pe.z; //t
double zQ = Qe.z; //t
double minPQz = Math.min(zP, zQ);//t
Point3D[] e = obj.getE();//e eye
Point2D[] vScr = obj.getVScr(); //vscr screen
for (int i=iStart; i<nTria; i++)//t
{
Tria t = tr[i];
int iA = t.iA, iB = t.iB, iC = t.iC;
Point2D AScr = vScr[iA], BScr = vScr[iB], CScr = vScr[iC];
// 1. Minimax test for x and y screen coordinates: //t
if (maxPQx <= AScr.x && maxPQx <= BScr.x && maxPQx <= CScr.x
|| minPQx >= AScr.x && minPQx >= BScr.x && minPQx >= CScr.x
|| maxPQy <= AScr.y && maxPQy <= BScr.y && maxPQy <= CScr.y
|| minPQy >= AScr.y && minPQy >= BScr.y && minPQy >= CScr.y)
continue;
// 2. Test if PQ is an edge of ABC: //t
if ((iP == iA || iP == iB || iP == iC) &&
(iQ == iA || iQ == iB || iQ == iC))
continue;
// 3. Test if PQ is clearly nearer than ABC://t
Point3D Ae = e[iA], Be = e[iB], Ce = e[iC];
double zA = Ae.z, zB = Be.z, zC = Ce.z;
if (minPQz >= zA && minPQz >= zB && minPQz >= zC)
continue;
// 4. Do P and Q (in 2D) lie in a half plane defined
// by line AB, on the side other than that of C?
// Similar for the edges BC and CA.
double eps = 0.1; // Relative to numbers of pixels //t
if (Tools2D.area2(AScr, BScr, PScr) < eps &&
Tools2D.area2(AScr, BScr, QScr) < eps ||
Tools2D.area2(BScr, CScr, PScr) < eps &&
Tools2D.area2(BScr, CScr, QScr) < eps ||
Tools2D.area2(CScr, AScr, PScr) < eps &&
Tools2D.area2(CScr, AScr, QScr) < eps)
continue;
// 5. Test (2D) if A, B and C lie on the same side
// of the infinite line through P and Q://t
double PQA = Tools2D.area2(PScr, QScr, AScr);
double PQB = Tools2D.area2(PScr, QScr, BScr);
double PQC = Tools2D.area2(PScr, QScr, CScr);
if (PQA < +eps && PQB < +eps && PQC < +eps ||
PQA > -eps && PQB > -eps && PQC > -eps)
continue;
// 6. Test if neither P nor Q lies behind the
// infinite plane through A, B and C://t
int iPol = refPol[i];
Polygon3D pol = (Polygon3D)polyList.elementAt(iPol);
double a = pol.getA(), b = pol.getB(), c = pol.getC(),
h = pol.getH(), eps1 = 1e-5 * Math.abs(h),
hP = a * Pe.x + b * Pe.y + c * Pe.z,
hQ = a * Qe.x + b * Qe.y + c * Qe.z;
if (hP > h - eps1 && hQ > h - eps1)
continue;
// 7. Test if both P and Q behind triangle ABC://t
boolean PInside =
Tools2D.insideTriangle(AScr, BScr, CScr, PScr);
boolean QInside =
Tools2D.insideTriangle(AScr, BScr, CScr, QScr);
if (PInside && QInside)
return;
// 8. If P nearer than ABC and inside, PQ visible;//t
// the same for Q:
double h1 = h + eps1;
boolean PNear = hP > h1, QNear = hQ > h1;
if (PNear && PInside || QNear && QInside)
continue;
// 9. Compute the intersections I and J of PQ
// with ABC in 2D.
// If, in 3D, such an intersection lies in front of
// ABC, this triangle does not obscure PQ.
// Otherwise, the intersections lie behind ABC and
// this triangle obscures part of PQ:
double lambdaMin = 1.0, lambdaMax = 0.0;
for (int ii=0; ii<3; ii++)
{ double v1 = BScr.x - AScr.x, v2 = BScr.y - AScr.y,
w1 = AScr.x - PScr.x, w2 = AScr.y - PScr.y,
denom = u2 * v1 - u1 * v2;
if (denom != 0)
{ double mu = (u1 * w2 - u2 * w1)/denom;
// mu = 0 gives A and mu = 1 gives B.
if (mu > -0.0001 && mu < 1.0001)
{ double lambda = (v1 * w2 - v2 * w1)/denom;
// lambda = PI/PQ
// (I is point of intersection)
if (lambda > -0.0001 && lambda < 1.0001)
{ if (PInside != QInside &&
lambda > 0.0001 && lambda < 0.9999)
{ lambdaMin = lambdaMax = lambda;
break;
// Only one point of intersection
}
if (lambda < lambdaMin) lambdaMin = lambda;
if (lambda > lambdaMax) lambdaMax = lambda;
}
}
}
Point2D temp = AScr; AScr = BScr;
BScr = CScr; CScr = temp;
}
float d = obj.getD();
if (!PInside && lambdaMin > 0.001)
{ double IScrx = PScr.x + lambdaMin * u1,
IScry = PScr.y + lambdaMin * u2;
// Back from screen to eye coordinates:
double zI = 1/(lambdaMin/zQ + (1 - lambdaMin)/zP),
xI = -zI * IScrx / d, yI = -zI * IScry / d;
if (a * xI + b * yI + c * zI > h1) continue;
Point2D IScr = new Point2D((float)IScrx, (float)IScry);
if (Tools2D.distance2(IScr, PScr) >= 1.0)
lineSegment(g, Pe, new Point3D(xI, yI, zI), PScr,
IScr, iP, -1, i + 1);
}
if (!QInside && lambdaMax < 0.999)
{ double JScrx = PScr.x + lambdaMax * u1,
JScry = PScr.y + lambdaMax * u2;
double zJ =
1/(lambdaMax/zQ + (1 - lambdaMax)/zP),
xJ = -zJ * JScrx / d, yJ = -zJ * JScry / d;
if (a * xJ + b * yJ + c * zJ > h1) continue;
Point2D JScr = new Point2D((float)JScrx, (float)JScry);
if (Tools2D.distance2(JScr, QScr) >= 1.0)
lineSegment(g, Qe, new Point3D(xJ, yJ, zJ),
QScr, JScr, iQ, -1, i + 1);
}
return;
// if no continue-statement has been executed
}
drawLine(g, PScr.x, PScr.y, QScr.x, QScr.y);
}
}

Convert RGB value to HSV

I've found a method on the Internet to convert RGB values to HSV values.
Unfortunately, when the values are R=G=B, I'm getting a NaN, because of the 0/0 operation.
Do you know if there is an implemented method for this conversion in Java, or what do I have to do when I get the 0/0 division to get the right value of the HSV?
Here comes my method, adapted from some code on the Internet:
public static double[] RGBtoHSV(double r, double g, double b){
double h, s, v;
double min, max, delta;
min = Math.min(Math.min(r, g), b);
max = Math.max(Math.max(r, g), b);
// V
v = max;
delta = max - min;
// S
if( max != 0 )
s = delta / max;
else {
s = 0;
h = -1;
return new double[]{h,s,v};
}
// H
if( r == max )
h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
h = 2 + ( b - r ) / delta; // between cyan & yellow
else
h = 4 + ( r - g ) / delta; // between magenta & cyan
h *= 60; // degrees
if( h < 0 )
h += 360;
return new double[]{h,s,v};
}
I'm pretty sure what you want is RGBtoHSB
int r = ...
int g = ...
int b = ...
float[] hsv = new float[3];
Color.RGBtoHSB(r,g,b,hsv)
//hsv contains the desired values

Categories