简体   繁体   English

通过X和Y从数组中查找点的索引

[英]Finding index of the point from array by X and Y

I have a problem with finding/soritng points in array. 我在数组中查找/索取点有问题。 When I want to find the point which is the nearest to the base point (0,0): 当我想找到最接近基准点(0,0)的点时:

CvPoint2D32f[] corners;
...
Cv.FindChessboardCorners(...)
...
int min = Array.IndexOf(corners, corners.Min(p => p.X + p.Y));

and I'm trying to display index of the point - I'm getting "-1" value on textBox, like the the array is empty. 并且我试图显示该点的索引-我在textBox上获得“ -1”值,就像数组为空。

In fact it isn't, because I can display one element from it typing eg.: 实际上不是,因为我可以输入以下内容显示其中的一个元素:

this.textBox1.Text = Convert.ToString(corners[0]);

And I'm getting in textBox: 我进入textBox:

CvPoint2D32f (x:179,143 y:60,15205)

You cant find it because Min returns the sum in this instance and not the object, hence the -1 result. 您找不到它,因为Min在此实例而不是对象中返回总和,因此返回-1。

You will have to loop through it by yourself... 您将不得不自己遍历它...

private int IndexOfMinPoint(CvPoint3D32f[] points) {
    int lowestValue = points[0].x + points[0].y;
    int lowestValueIndex = 0;

    for (int i = 1; i < points.length; i++) {

        int newLowestValue = points[i].x + points[i].y;

        if ( newLowestValue < lowestValue) {
             lowestValue = newLowestValue;
             lowestValueIndex = i;
        }

    }

    return lowestValueIndex;    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM