0
 static List<List<Point>> GetClusters(List<Point> points, double eps, int minPts)
        {
            if (points == null) return null;
            List<List<Point>> clusters = new List<List<Point>>();
            eps *= eps; // square eps
            int clusterId = 1;
            for (int i = 0; i < points.Count; i++)
            {
                Point p = points[i];
                if (p.ClusterId == Point.UNCLASSIFIED)
                {
                    if (ExpandCluster(points, p, clusterId, eps, minPts)) clusterId++;
                }
            }
            // sort out points into their clusters, if any
            int maxClusterId = points.OrderBy(p => p.ClusterId).Last().ClusterId;
            if (maxClusterId < 1) return clusters; // no clusters, so list is empty
            for (int i = 0; i < maxClusterId; i++) clusters.Add(new List<Point>());
            foreach (Point p in points)
            {
                if (p.ClusterId > 0) clusters[p.ClusterId - 1].Add(p);
            }
            return clusters;
        }

我正在使用上面的方法获取图像集群,但如果我运行它
错误: 序列在以下位置不包含任何元素:

int maxClusterId = points.OrderBy(p => p.ClusterId).Last().ClusterId;

我应该怎么做才能解决这些错误?

我试图改变:

int maxClusterId = points.OrderBy(p => p.ClusterId).Last().ClusterId;

至 :

int maxClusterId = points.OrderBy(p => p.ClusterId).LastOrDefault().ClusterId;

但错误是:

你调用的对象是空的。

4

1 回答 1

0

还要检查points.Count

if (points == null || points.Count==0) return null;
于 2013-07-29T10:38:16.137 回答