序列不包含用于图像处理的元素

时间:2013-07-29 08:26:51

标签: c# image image-processing sequence elements

 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;
        }

我正在使用上面的方法获取图像集群 但如果我跑它 错误:序列不包含任何元素 at:

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;

但错误是:

对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

还要检查points.Count

if (points == null || points.Count==0) return null;