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

I am using method above for get cluster of image but if I run it
error : Sequence contains no elements at :

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

what should I do for solve that errors?

I am trying to change from :

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

to :

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

But the the error is :

Object reference not set to an instance of an object.

1
You say you are trying to change from...to... but they are both the same. You should probably edit that.. - Giannis Paraskevopoulos

1 Answers

0
votes

also check points.Count.

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