python如何拟合三维平面(拟合Intel Realsense D435深度数据点)
生活随笔
收集整理的這篇文章主要介紹了
python如何拟合三维平面(拟合Intel Realsense D435深度数据点)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 擬合Intel Realsense D435深度數據點
參考文章:【MQ筆記】超簡單的最小二乘法擬合平面(Python)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D# 創建函數,用于生成不同屬于一個平面的100個離散點 def not_all_in_plane(a, b, c):x = np.random.uniform(-10, 10, size=100)y = np.random.uniform(-10, 10, size=100)z = (a * x + b * y + c) + np.random.normal(-1, 1, size=100)return x, y, z# 調用函數,生成離散點 x, y, z = not_all_in_plane(2, 5, 6)#創建系數矩陣A a = 0 A = np.ones((100, 3)) for i in range(0, 100):A[i, 0] = x[a]A[i, 1] = y[a]a = a + 1 #print(A)#創建矩陣b b = np.zeros((100, 1)) a = 0 for i in range(0, 100):b[i, 0] = z[a]a = a + 1 #print(b)#通過X=(AT*A)-1*AT*b直接求解 A_T = A.T A1 = np.dot(A_T,A) A2 = np.linalg.inv(A1) A3 = np.dot(A2,A_T) X= np.dot(A3, b) print('平面擬合結果為:z = %.3f * x + %.3f * y + %.3f'%(X[0,0],X[1,0],X[2,0]))#計算方差 R=0 for i in range(0,100):R=R+(X[0, 0] * x[i] + X[1, 0] * y[i] + X[2, 0] - z[i])**2 print ('方差為:%.*f'%(3,R))# 展示圖像 fig1 = plt.figure() ax1 = fig1.add_subplot(111, projection='3d') ax1.set_xlabel("x") ax1.set_ylabel("y") ax1.set_zlabel("z") ax1.scatter(x,y,z,c='r',marker='o') x_p = np.linspace(-10, 10, 100) y_p = np.linspace(-10, 10, 100) x_p, y_p = np.meshgrid(x_p, y_p) z_p = X[0, 0] * x_p + X[1, 0] * y_p + X[2, 0] ax1.plot_wireframe(x_p, y_p, z_p, rstride=10, cstride=10) plt.show()
擬合Intel Realsense D435深度數據點
# -*- coding: utf-8 -*- """ @File : 攝像頭精度測試.py @Time : 2020/9/7 10:49 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import datetime import time from datetime import dateimport pyrealsense2 as rs import cv2 as cv import numpy as npimport matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3Dfrom numba import jitdef fit_flat(x, y, z):# 取樣點數量point_num = len(x)print(point_num)# 創建系數矩陣Aa = 0A = np.ones((point_num, 3))for i in range(0, point_num):A[i, 0] = x[a]A[i, 1] = y[a]a = a + 1# print(A)# 創建矩陣bb = np.zeros((point_num, 1))a = 0for i in range(0, point_num):b[i, 0] = z[a]a = a + 1# print(b)# 通過X=(AT*A)-1*AT*b直接求解A_T = A.TA1 = np.dot(A_T, A)A2 = np.linalg.inv(A1)A3 = np.dot(A2, A_T)X = np.dot(A3, b)print('平面擬合結果為:z = %.3f * x + %.3f * y + %.3f' % (X[0, 0], X[1, 0], X[2, 0]))# 計算方差R = 0for i in range(0, point_num):R = R + (X[0, 0] * x[i] + X[1, 0] * y[i] + X[2, 0] - z[i]) ** 2print('方差為:%.*f' % (3, R))# 展示圖像fig1 = plt.figure()ax1 = fig1.add_subplot(111, projection='3d')ax1.set_xlabel("x")ax1.set_ylabel("y")ax1.set_zlabel("z")ax1.scatter(x, y, z, c='r', marker='o')x_p = np.linspace(0, 1500, 150)y_p = np.linspace(0, 1500, 150)x_p, y_p = np.meshgrid(x_p, y_p)z_p = X[0, 0] * x_p + X[1, 0] * y_p + X[2, 0]ax1.plot_wireframe(x_p, y_p, z_p, rstride=10, cstride=10)plt.show()def run_cam():ctx = rs.context()pipeline = rs.pipeline(ctx)cfg = rs.config()cfg.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)cfg.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)profile = pipeline.start(cfg)try:count = 0while True:fs = pipeline.wait_for_frames()color_frame = fs.get_color_frame()depth_frame = fs.get_depth_frame()# print(type(depth_frame)) # <class 'pyrealsense2.pyrealsense2.depth_frame'>if not depth_frame or not color_frame:continue# color_image = np.asanyarray(color_frame.get_data())depth_image = np.asanyarray(depth_frame.get_data())# print(type(depth_image)) # <class 'numpy.ndarray'>x, y, z = [], [], []for i in range(1280):for j in range(720):# if depth_image[j, i] == 0:# 發現繪制的點中有很多深度幾萬到六萬多不等的,把它們過濾掉point_depth = np.asanyarray(depth_frame.get_data())[j, i]if point_depth == 0 or point_depth >= 2000:continueelse:x.append(i)y.append(j)z.append(point_depth)# 將數據寫入numpy文件count += 1np.savez('{}_{}'.format(count, time.time()), x=x, y=y, z=z)fit_flat(x, y, z)finally:pipeline.stop()# print('hh') if __name__ == '__main__':print('hh')run_cam()結果:
總結
以上是生活随笔為你收集整理的python如何拟合三维平面(拟合Intel Realsense D435深度数据点)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量子力学——超弦理论、M理论(膜论)
- 下一篇: python绘制柱状图,如何改变柱状柱间