在研究SLAM時常常需要對其輸出的位姿進行復現以檢測算法效果,在ubuntu系統中使用Python可以很好的完成相關的工作。
一. Ubuntu下Python的使用
在Ubuntu下使用Python有兩種方法,一種是直接在控制台中運行Python文件,一種是下載IDE編輯並運行Python文件。
在控制台中使用Python方法如下:
首先確認有Python文件(filename.py),然后打開控制台進入文件當前目錄,並輸入以下內容就可以運行了。
python file_name.py
雖然控制台可以運行Python,不過由於不能調試等問題仍然比較推薦使用IDE。
目前使用的Python IDE為PyCharm,官方下載地址為https://www.jetbrains.com/pycharm/download/#section=linux
官網中提供professional和community兩種版本,因為community版本免費大家可以直接下載使用。下載好后直接放到安裝目錄中解壓,然后跟着解壓后的說明文件執行安裝命令即可安裝成功(部分電腦由於配置原因可能會報錯,網上有很多講解配置環境安裝博客,大家可以參考)。安裝成功后,PyCharm界面如下圖。
二. matplotlib繪制三維軌跡
Matplotlib是Python的一個繪圖庫,想面將講解如何使用這個庫來繪制三維線段,以此檢測SLAM算法的輸出結果(電腦配置Python 2.7)。
2.1. 繪制基本三維曲線
首先給出完整代碼,以及輸出結果。
# import necessary module from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np # load data from file # you can replace this using with open data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt") first_2000 = data1[:, 3] second_2000 = data1[:, 7] third_2000 = data1[:, 11] # print to check data print first_2000 print second_2000 print third_2000 # new a figure and set it into 3d fig = plt.figure() ax = fig.gca(projection='3d') # set figure information ax.set_title("3D_Curve") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure = ax.plot(first_2000, second_2000, third_2000, c='r') plt.show()
這段代碼非常簡單,而且相關的注釋也很完善,因此只簡要說明幾個需要注意的地方。第一個需要注意的是讀取文件中數據比較推薦用with open 然后逐行讀取;第二點是在新建圖像時一定別忘了添加這段代碼,這是輸出圖像設定為3D的關鍵。
ax = fig.gca(projection='3d')
2.2. 同一張圖中繪制多個三維曲線
代碼和輸出結果如下:
# import necessary module from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np # load data from file # you replace this using with open data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt") first_2000 = data1[:, 3] second_2000 = data1[:, 7] third_2000 = data1[:, 11] data2 = np.loadtxt("./stereo/CameraTrajectoryNew1500.txt") first_1000 = data2[:, 3] second_1000 = data2[:, 7] third_1000 = data2[:, 11] # new a figure and set it into 3d fig = plt.figure() ax = fig.gca(projection='3d') # set figure information ax.set_title("3D_Curve") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure1 = ax.plot(first_2000, second_2000, third_2000, c='r') figure2 = ax.plot(first_1000, second_1000, third_1000, c='b') plt.show()
實現這個功能只需要在之前代碼中加入讀取新數據的相關代碼,以及在畫圖時多生成一個圖即可,也是非常的簡單。
2.3. 將區域划分后繪制三維圖像
代碼和輸出結果如下:
# import necessary module from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np # load data from file # you replace this using with open data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt") first_2000 = data1[:, 3] second_2000 = data1[:, 7] third_2000 = data1[:, 11] data2 = np.loadtxt("./stereo/CameraTrajectoryNew1500.txt") first_1500 = data2[:, 3] second_1500 = data2[:, 7] third_1500 = data2[:, 11] # new a figure and set it into 3d fig = plt.figure() # ############ first subplot ############ ax = fig.add_subplot(2, 2, 1, projection='3d') ax.set_title("3D_Curve1") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure1 = ax.plot(first_2000, second_2000, third_2000, c='r') figure2 = ax.plot(first_1500, second_1500, third_1500, c='b') # ############ second subplot ############ ax = fig.add_subplot(2, 2, 2, projection='3d') # set figure information ax.set_title("3D_Curve2") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure1 = ax.plot(first_2000, second_2000, third_2000, c='r') figure2 = ax.plot(first_1500, second_1500, third_1500, c='b') # ############ third subplot ############ ax = fig.add_subplot(2, 2, 3, projection='3d') # set figure information ax.set_title("3D_Curve3") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure1 = ax.plot(first_2000, second_2000, third_2000, c='r') figure2 = ax.plot(first_1500, second_1500, third_1500, c='b') # ############ fourth subplot ############ ax = fig.add_subplot(2, 2, 4, projection='3d') # set figure information ax.set_title("3D_Curve4") ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") # draw the figure, the color is r = read figure1 = ax.plot(first_2000, second_2000, third_2000, c='r') figure2 = ax.plot(first_1500, second_1500, third_1500, c='b') plt.show()
主要需要解釋下面這行代碼:
ax = fig.add_subplot(2, 2, 1, projection='3d')
這行代碼主要是說,將當前空間拆分建立新的子圖,子圖個數為四,按照2x2矩陣排列方式進行,當前子圖為四個子圖中的第一個,且為3D模式。
以上就是就是如何使用matplotlib繪制三位曲線。總的來說比較簡單,沒有什么難度,Python的確是一個非常好用的編程語言。更多關於matplotlib的使用方法大家可以參考他們的官方網站,里面有相關的tutorial以及examples。
Matplotlib Introduction:http://matplotlib.org/index.html
Matplotlib Samples:http://matplotlib.org/examples/index.html