How can I convert XYZ point cloud to binary mask image

823 Views Asked by At

I want to convert a set of point cloud (X, Y, Z) to a binary mask image using python. The problem is that these points are float and out of range of 0-255. To more specific, the points are related to an object (rectangle or ellipsoid), so I should make a binary image based on Z dimension, to specify the rectangle, for example, as 0 number and other points as 1 number in binary mask. Can anyone give me some ideas to achieve my goal?

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]]

I was tried to get such binary mask that was answered in this example ():

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()

The image would look like this: enter image description here

0

There are 0 best solutions below