如何将 XYZ 点云转换为二进制蒙版图像

问题描述

我想使用 python 将一组点云 (X,Y,Z) 转换为二进制蒙版图像。问题是这些点是浮动的并且超出了 0-255 的范围。更具体地说,这些点与一个对象(矩形或椭圆体)有关,所以我应该根据Z维制作一个二进制图像,以指定矩形,例如,二进制掩码中的数字为0,其他点为1。 谁能给我一些想法来实现我的目标?

My point is like this array:

 [[-1.56675167e+01  1.59539632e+01  1.15432026e-02]
 [-1.26066835e+01  6.48645007e+00  1.15510724e-02]
 [-1.18854252e+01  1.71767061e+01  1.15392632e-02]
 ...
 [-1.45721083e+01  1.39116935e+01 -9.86438582e-04]
 [-1.42607847e+01  1.28141373e+01 -1.73514791e-03]
 [-1.48834319e+01  1.50092497e+01  7.59929187e-04]]

我试图得到这个例子中回答的这样的二进制掩码 ():

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.path import Path
from descartes import polygonPatch
import alphashape
from shapely.geometry import Point,polygon

def poly2mask():
    
    # First of all,I separated the contour of the polygon and get vertices 
    # in the border

    hull = alphashape.alphashape(surface_org,0.)  # convex hull  
    poly = polygonPatch(hull,alpha=0.2,edgecolor='#999999')
    vertices = poly.get_path().vertices
    x = vertices[:,0] * 10
    y = vertices[:,1] * 10
    vertices_ls = list(zip(x,y))

    width,height = 120,120

    poly_path = Path(vertices_ls,closed=True)

    x,y = np.mgrid[:height,:width]
    coors = np.hstack((x.reshape(-1,1),y.reshape(-1,1)))  

    mask = poly_path.contains_points(coors)
    mask = mask.reshape(height,width)
    #print(mask)
    plt.imshow(mask)
    plt.ylim(-200,200)
    plt.xlim(-200,200)
    plt.show()

图像看起来像这样: enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)