سؤال

ايم كتابة برنامج لمقارنة صورتين ضد بعضهما البعض استنادا إلى اللون والايم باستخدام خوارزمية المسافة Euclidean عندما أقوم بتشغيلها وتمرير صورتين أحصل على مسافة واحدة ثم عند المرور في نفس الصور ولكن الآخرالطريق جولة أحصل على مجموعة مختلفة تماما من النتائج.

هل هذا طبيعي أم أنه يجب أن تكون الإجابات هي نفسها؟

العبارة التي أستخدمها لحساب مسافة Euclidean هي: giveacodicetagpre.

هل كانت مفيدة؟

المحلول

Looking at the code you posted, it looks your RGB values are ints. However, the ^ operator is not the power operator, but XOR (exclusive-OR) - a bitwise operation. So in order to calculate the squares correctly, use regular multiplication - e.g., use a temporary variable int deltaR = rgb1.getR()-rgb2.getR(); and then in the formula write deltaR*deltaR instead of the ^ operator. Your RGB values will probably be in 0 to 255 range, so there shouldn't be overflow issues. Alternatively, you could use Math.pow(rgb1.getR()-rgb2.getR(),2) etc. in the formula.

نصائح أخرى

For squaring a number in Java, use Math.pow(x, 2) or even simpler, x * x. The expression x ^ 2 does not square x, instead it XORs x with 2.

In your code:

int diffR = rgb1.getR() - rgb2.getR();
int diffG = rgb1.getG() - rgb2.getG();
int diffB = rgb1.getB() - rgb2.getB();

int distance = (int) Math.sqrt(diffR*diffR + diffG*diffG + diffB*diffB);

... Although I'm not quite sure of your algorithm, but that's a different issue.

As people have said, you can use Math.pow(x, 2) for squaring. Just from personal experience, if you're going to call this function a lot it might be better to write out the multiplication yourself, i.e. Math.sqrt((deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ)); It may seem uglier but if you profile both forms of the code you'll see that the calls to Math.pow are much slower than the simple multiplications. Obviously there's nothing to do about the Math.sqrt call though.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top