Simple way of fusing a few close points?

前端 未结 4 2128
南旧
南旧 2021-02-06 16:57

I have a list of points as such

points = [(-57.213878612138828, 17.916958304169601),
          (76.392039480378514, 0.060882542482108504),
          (0.124176706         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 17:33

    def merge_close_pts(pts, rad=5): # pts is a numpy array of size mx2 where each row is ( xy )
    pts = np.float32(pts) # avoid issues with ints
    
    # iteratively make points that are close to each other get closer ( robust to clouds of multiple close pts merge )
    pts_to_merge = (np.sqrt(np.power(pts[:, 0].reshape(-1, 1) - pts[:, 0].reshape(1,-1),2) + \
                       np.power(pts[:, 1].reshape(-1, 1) - pts[:, 1].reshape(1, -1), 2))) <= rad
    
    for _ in range(5):
        for pt_ind in range(pts.shape[0]):
            pts[pt_ind,:] = pts[pts_to_merge[pt_ind, :], :].reshape(-1, 2).mean(axis=0)
    
    #now keep only one pts from each group. 
    pts_to_merge = ((np.sqrt(np.power(pts[:, 0].reshape(-1, 1) - pts[:, 0].reshape(1, -1), 2) + \
                            np.power(pts[:, 1].reshape(-1, 1) - pts[:, 1].reshape(1, -1), 2))) <= rad) * \
                   (np.eye(pts.shape[0])== 0)
    
    
    for pt_ind in range(pts.shape[0]):
        if (pts[pt_ind,:] == 0).all() == False:
            inds_to_erase = pts_to_merge[pt_ind, :]
            pts[inds_to_erase, :] = 0
    
    
    return pts[(pts==0).all() == False, :]
    

提交回复
热议问题