0

我有 ac# 函数,其中包含计算某些点的欧几里得距离的公式。我得到了由 R(rx,ry) 和 L(lx,ly) 定义的点位置。

起初,我尝试编写这样的代码:

double dRightLeft = Math.Sqrt((Math.Pow(rx - lx, 2) + Math.Pow(ry - ly, 2)));

它返回 0.0。

然后我尝试拆分变量以检查我在哪里做错了,如下所示:

 double rl = (Math.Pow(rx - lx, 2) + Math.Pow(ry - ly, 2));
 double dRightLeft = Math.Sqrt(rl);

rl 变量返回其操作的有效值。但是当我试图从中得到平方根时,dRighLeft 变量仍然返回 0.0。我尝试了分配和未分配的 dRightLeft,如下所示:

//assigned
dRightLeft = 0;
//unassigned
dRightLeft;

他们都仍然返回 0.0 值。

这是我得到 rx、ry、lx 和 ly 值的简短但完整的程序:

public Bitmap getDetectedImage()
{
int rx, rx, lx, ly, ...;
double dRightLeft = 0;
...
//righteyeloop
for (int x = fo.rightEye.X; x < (fo.rightEye.X + fo.rightEye.Width); x++)
{
    for (int y = fo.rightEye.Y; y < (fo.rightEye.Y + fo.rightEye.Height); y++)
    {    //segmentation...//   
         rPixel++;
         result.byteImage[x, y].R = 0;
         result.byteImage[x, y].G = 255;
         result.byteImage[x, y].B = 0;
//to get the the first pixel detected//
         if (rPixel == 1)
         {
             result.byteImage[x, y].R = 255;
             result.byteImage[x, y].G = 0;
             result.byteImage[x, y].B = 0;

rx = x + (fo.rightEye.Width / setting.featureWidth * setting.eyeHeight / setting.eyeWidth);
ry = y + (fo.rightEye.Height / setting.featureWidth * setting.eyeHeight / setting.eyeWidth);
         }
    }
}
//lefteyeloop basically the same type as righteyeloop//
.....
//this to count the distance of righteye and lefteye
    double rl = ((rx - lx) * (rx - lx) + (ry - ly) * (ry - ly));
    double dRightLeft = Math.Pow(rl, 0.5);
}
4

1 回答 1

0

我怀疑问题是 Math.Pow 处理精度较低的双精度数(有关更多讨论,请参见此 SO 问题)。直接的直觉是通过写出乘法来替换 Math.Pow:

double rl = ((rx - lx) * (rx - lx) + (ry - ly) * (ry - ly));
double dRightLeft = Math.Sqrt(rl);

Math.Pow 参考资料看来,如果指数为 2,返回 0 的唯一方法是您的基数(即rx - lxry - ly)也为 0。

于 2015-06-26T13:55:33.490 回答