2013-07-29 33 views
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; 

但錯誤的是:

對象引用不設置到對象的實例。

+0

你說你試圖從......變成......但它們都是一樣的。你應該編輯.. –

回答

0

也檢查points.Count

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

解決..謝謝你的解決方案 –

相關問題