当我想将我的点云数据保存为 .ply 文件时出现值错误。这是可以在此处找到的示例的稍微修改的代码:https ://github.com/opencv/opencv/blob/master/samples/python/stereo_match.py
我使用不同的数据输入,但我以与示例中相同的文件格式为 xyz -data 和 rgb -data 创建了必要的数组。xyz 保存为 float32-(n,3)-array,rgb 保存为 uint8-(n,3)-array。错误消息是关于不支持的格式字符,但我无法弄清楚为什么在仅更改输入数据后会出现此错误?
(使用原始图像,.ply 文件写入没有问题)
这是错误消息:
f.write((plyHeader %
dict(vert_num=len(verts))).encode('utf-8'))
ValueError: unsupported format character '
' (0xa) at index 47
创建 .ply 文件的代码:
#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')
所以我的问题是,什么格式字符造成了这个错误,我该如何解决这个问题?最后,我想要一个这样的文件,其中包含我的数据:
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