Search code examples
pythonmatplotlibpoint-clouds

what distance represent two adjacents pixels


I have a 3d point cloud. I used matplotlib to draw a scatterplot representing the point cloud viewed from above. The point cloud is stored as a list of coordinates in meters. The output of matplotlib.pyplot.scatter is a png image.

In addition to saving the image, I want to save the correspondence pixels <-> meters. How to do that?

Here the code I use to make my image with matplotlib. I use a dataframe to manipulate the point cloud.

        colors = np.array((self.cloud["red"], self.cloud["green"], self.cloud["blue"])).T
        dpi = 72
        print("dpi: ",dpi)
        fig = plt.figure(figsize=(18000/dpi, 18000/dpi), dpi=dpi)
        ax = plt.axes(projection='3d')
        ax.view_init(elev=90., azim = 0)
        ax.set_snap(True)
        ax.scatter(
            self.cloud["x"],
            self.cloud["y"],
            self.cloud["z"],
            marker=MarkerStyle('.', fillstyle = 'full'),
            facecolors=colors / 255,
            zdir="z",
            #to set a point to 1 pixel we use the relation (dpi/fig.dpi) but
            #the problem of the point cloud is the fact that we didn't have a point at each pixel so we increase the size of a point 
            #the size is empiric so need to be careful 
            s = (25)**2,
            )
        plt.axis('off')
        self.set_proper_aspect_ratio(ax)
        fig.tight_layout()
        plt.savefig(file_name, orientation = 'portrait', transparent = True, dpi=fig.dpi)

Solution

  • To find this distance i use this code:

    inv = ax.transData.inverted()
    #center_image is in pixel
    distance_x = abs(inv.transform((center_image[0],center_image[1]))[0])
    distance_y = abs(inv.transform((center_image[0],center_image[1]))[1])