I get a value error when I want to save my point cloud data as a .ply file. This is slightly modificated code of an example that can be found here: https://github.com/opencv/opencv/blob/master/samples/python/stereo_match.py
I use a different data-input but I created the necessary arrays for x y z -data and r g b -data in the same file format as in the example. x y z is saved as a float32-(n,3)-array and r g b is saved as an uint8-(n,3)-array. The error message is about an unsupported format character but I can't figure out why I get this error after changing only the input data?
( with the Original Images the .ply file was written without problems )
This is the error message:
f.write((plyHeader %
dict(vert_num=len(verts))).encode('utf-8'))
ValueError: unsupported format character '
' (0xa) at index 47
The code that creates the .ply file:
#create standard ply header
plyHeader = '''ply
format ascii 1.0
element vertex %(vert_num)
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
'''
#function that writes the .ply file
def writePly(fn, verts, colors):
#reshaping (if not already in (nx3))
verts = verts.reshape(-1, 3)
colors = colors.reshape(-1, 3)
#horizontal stacking so I have an (nx6) array
verts = np.hstack([verts, colors]) (nx6)
with open(fn, 'wb') as f:
#write the file, this is the part where the error occurs
f.write((plyHeader % dict(vert_num=len(verts))).encode('utf-8'))
np.savetxt(f, verts, fmt='%f %f %f %d %d %d ')
#create the used data using a mask that masks out bad pixels
colors = imgL[mask]
#point cloud data
out_points = np.float32(
np.squeeze(np.dstack((xWorld,yWorld,zWorld))))
#colors, my image is grayscales so I use the same values for rgb
out_colors = np.squeeze(np.dstack((colors,colors,colors)))
out_fn = 'out.ply'
writePly('out.ply', out_points, out_colors)
print('%s saved' % 'out.ply')
So my question is, what format character is creating this error and how can I solve this problem? In the end I want a file like this containing my data:
ply
format ascii 1.0
element vertex 231229
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
-9.215469 12.265193 -22.665194 228 199 141
-9.171270 12.265193 -22.665194 211 183 128
-9.127071 12.265193 -22.665194 197 183 138
-9.082873 12.265193 -22.665194 180 181 145
-9.038674 12.265193 -22.665194 148 162 132
-9.019390 12.299169 -22.727978 143 165 134
-9.000000 12.333333 -22.791111 156 182 147
-8.980501 12.367688 -22.854595 164 190 151
-8.935933 12.367688 -22.854595 169 194 153
-8.891365 12.367688 -22.854595 175 196 156
-8.797784 12.299169 -22.727978 180 196 159
-8.729281 12.265193 -22.665194 182 195 160
-8.685082 12.265193 -22.665194 183 196 161
-8.640884 12.265193 -22.665194 182 199 162
-8.596685 12.265193 -22.665194 177 201 162
-8.552486 12.265193 -22.665194 174 202 161
Well I came up with some kind of "workaround". I couldn't figure out the error that was caused by filling in the header somehow. So I just write a txt-file with 6 columns and then I import the data in CloudCompare a tool to visualize pointclouds. Works without problems.
def writeFile(fn, verts, colors):
verts = np.array(verts.reshape(-1, 3))
colors = np.array(colors.reshape(-1, 3))
vertsStack = np.squeeze([np.hstack([verts, colors])])
with open(fn, 'wb') as f:
np.savetxt(f, vertsStack, fmt='%f %f %f %d %d %d ')
For some more information about CloudCompare and links check this question: Point cloud XYZ format specification