首页 > 解决方案 > 将来自不同函数 python3 opencv 的几个读数附加到 txt 文件

问题描述

我的代码出现错误。基本上,我有一些函数叫做 Triangulate

triangulate = cv2.triangulatePoints(projection_matrix_1,projection_matrix_2,projection_points1,projection_points_2)

该函数给了我一些这样的输出

[[ X1 X2 X3 ... Xn]
 [ Y1 Y2 Y3 ... Yn]
 [ Z1 Z2 Z3 ... Zn]
 [ W1 W2 W3 ... Wn]]

我还有另一个功能可以从某些相机帧中检测描述符。

kps, desc = self.detector.detectAndCompute(self.image, None)

desc的输出是这样的

here [[ 97  93   2 ..., 255  63   1]
 [ 64 141   1 ..., 123 255  62]
 [ 97  29  14 ..., 253 127   3]
 ..., 
 [218 233 131 ..., 253 255  15]
 [  1 237 143 ..., 127 255  15]
 [ 97 253  13 ..., 176 255   3]]

here [[ 64 141   0 ..., 123 255  31]
 [ 97  29  78 ..., 253 127   1]
 [  0  13   0 ..., 123 255  30]
 ..., 
 [ 97 253  15 ..., 255 255   3]
 [ 64 141   1 ...,   2  16  48]
 [  1 237 143 ..., 255 102  15]]

每当有新读数出现时,这两个函数的输出都会发生变化。读数来自机器人运动。它记录在rosbag中。

现在我想写一个txt文件。

X1 Y1 Z1 W1 desc1 X2 Y2 Z2 W2 desc2  ... Xn Yn Zn Wn descn

 

其中 xyzw 是 triangulate 的第一次读取的输出,而 desc1 是 desc 函数的第一次读取的输出 .. 等直到它完成。

我能够读取 triangulate 的读数并添加它,而这段代码没有任何问题

            f = open("11.txt", "w")  # Erases the file content.
            f.close()

这里存在两个功能

            f = open("11.txt", "a")
            f.write(','.join(str(v) for v in self.tringulate.T.flat) + '\n')
            f.close()

我能够保存 X1 Y1 Z1 W1 X2 Y2 Z2 W2 但没有 desc1 ... desc2 ...等

请问有什么建议吗?

标签: pythonopencvcomputer-visionappendtriangulation

解决方案


推荐阅读