Corbjn
Corbjn

Reputation: 286

How to fix inf coordinate system of point cloud?

Using OpenCvs Python library I processed a disparity map from two rectified stereo images. Afterwards I converted that map to 3D points using cv2.reprojectImageTo3D() and saved it as .ply. When I'm trying to open the point cloud in e.g. CloudCompare I get a "Global shift/scale" warning. The "Point in original coordinate system (on disk)" box says inf for x, y, z coordinates (see image).

Global Shift/Scale Warning

So it seems that I forgot some step(s) or did a mistake when generating the point cloud. I did:

import numpy as np
import cv2 as cv


ply_header = '''ply
format ascii 1.0
element vertex %(vert_num)d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
'''


def write_ply(fn, verts, colors):
    verts = verts.reshape(-1, 3)
    colors = colors.reshape(-1, 3)
    verts = np.hstack([verts, colors])
    with open(fn, 'wb') as f:
        f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8'))
        np.savetxt(f, verts, fmt='%f %f %f %d %d %d ')


pcd_dir = "/some/path/points.ply"
imL = cv.imread("/path/to/left/frame0000.jpg", cv.IMREAD_GRAYSCALE)
imR = cv.imread("/path/to/right/frame0000.jpg", cv.IMREAD_GRAYSCALE)

stereo = cv.StereoBM_create(numDisparities = 16, blockSize = 11)
disparity = stereo.compute(imL, imR)

h, w = imL.shape[:2]
f = 0.8 * w
Q = np.float32([[1, 0, 0, -0.5 * w],
                [0, -1, 0, 0.5 * h],
                [0, 0, 0, -f],
                [0, 0, 1, 0]])

points = cv.reprojectImageTo3D(disparity, Q)
print(type(points))
print(points.shape)

colors = cv.cvtColor(imL, cv.COLOR_BGR2RGB)
mask = disparity > disparity.min()
out_points = points[mask]
out_colors = colors[mask]
write_ply(pcd_dir, out_points, out_colors)

Expected output would be obviously a readable point cloud.

Upvotes: 0

Views: 109

Answers (0)

Related Questions