簡體   English   中英

Append 到 Python 和 OpenCV 中的文本文件

[英]Append to text file in Python and OpenCV

我的代碼出現錯誤。 基本上,我有一些名為 Triangulate 的 function

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

function 給了我一些像這樣的 output

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

我還有另一個 function 用於檢測來自某些相機幀的描述符。

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

desc的output是這樣的

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]]

每當有新讀數出現時,這兩個功能的 output 都會發生變化。 讀數來自機器人運動。 它記錄在rosbag中。

現在我想寫一個txt文件。

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

其中 xyzw 是第一次讀取 triangulate 的輸出,而 desc1 是 output 從第一次讀取 desc function.. 等直到完成。

我能夠讀取 triangulate 的讀數並添加它,而這段代碼沒有任何問題

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

here exists both functions

        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...等

嘗試先附加兩個數組(tringulate,desc),然后將它們存儲在文件中。 參考這個鏈接

您可以為此使用 numpy 庫。 First, convert both the array in numpy array(if they are not numpy arrays) and then append them using np.append() function with axis=1.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM