Я получаю сообщение об ошибке значения, когда хочу сохранить данные облака точек в виде файла .ply. Это немного измененный код примера, который можно найти здесь: https://github.com/opencv/opencv/blob/master/samples/python/stereo_match.py
Я использую другой ввод данных, но создал необходимые массивы для x y z -data и r g b -data в том же формате файла, что и в примере. x y z сохраняется как массив float32-(n,3), а r g b сохраняется как массив uint8-(n,3). Сообщение об ошибке касается неподдерживаемого символа формата, но я не могу понять, почему я получаю эту ошибку после изменения только входных данных?
(с оригинальными изображениями файл .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