plot 3D stem

发布时间:2023年12月18日
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd

from matplotlib import cm


width = 3088
height = 2064

def gen_data():


  x = np.linspace(-10, 10,21)
  y = np.linspace(-10, 10, 21)
  z = np.linspace(15, 25, 21)

  X,Y,Z = np.meshgrid(x,y,z)
  coors = np.concatenate((X[:, :, :, None], Y[:, :, :, None], Z[:, :, :, None]), axis=-1)
  pt3ds = 0.01*np.float32(coors).reshape(-1,3)

  data = []
  for pt3d in pt3ds:
    pix_left = reproject2_left(pt3d) #np.float32([[1196.6746,   796.03284]])
    pix_right = reproject2_right(pt3d) # np.float32( [[1125.056 , 1051.7277]])

    pix_left = reproject2_left(pt3d)
    pix_right = reproject2_right(pt3d)

    # 改变x值
    distx = []
    for i in range(11):
      step = i/10
      pl =  pix_left.copy()
      pl[0][0]+=step
      pt3d_c =  triangle3d(pl, pix_right)
      distx.append(1000*np.linalg.norm(pt3d_c-pt3d))


    disty=[]
    for i in range(11):
      step = i / 10
      pl = pix_left.copy()
      pl[0][1] += step
      pt3d_c = triangle3d(pl, pix_right)
      disty.append(1000*np.linalg.norm(pt3d_c - pt3d))

    out = [*pt3d, *pix_left[0], *pix_right[0],  distx[5],  max(distx), disty[5], max(disty), np.linalg.norm(pt3d) ]
    data.append(out)


  df = pd.DataFrame(data)
  df.columns = ['x','y','z', 'xl', 'yl', 'xr', 'yr', 'dist_x0.5', 'dist_x1', 'dist_y0.5', 'dist_y1', 'dist_cam']
  df_filter = df.loc[(df['xl'] >= 0) & (df['yl'] >= 0) & (df['xr'] >= 0) & (df['yr'] >= 0)]
  df_filter.to_csv(r'C:\Users\31408\Desktop\datamat\cube_model_8000.csv', index=False, sep=',')
  return df_filter


def viz_cube3d_effect():
  df = pd.read_csv(r'C:\Users\31408\Desktop\datamat\cube_model_8000.csv')
  df_filter = df.loc[(df['xl'] <= width) & (df['yl'] <= height) & (df['xr'] < width) & (df['yr'] < height)]
  data = np.array(df_filter.values)
  layersz = list(set(data[:, 2]))
  layersz.sort()
  layersz.pop()

  rows, cols = 1, 1
  x = np.linspace(-10, 10, 21) / 100
  y = np.linspace(-10, 10, 21) / 100

  for ii, layerz in enumerate(layersz):
    fig = plt.figure(figsize=(1200, 1000))
    ax = fig.add_subplot(rows, cols, 1, projection='3d')
    ax.set_title(f'z={np.round(layerz, 3)}')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    vizd = np.float32([item for item in data if item[2] == layerz])
    ax.stem(vizd[:, 0], vizd[:, 1], vizd[:, -5])

    X, Y = np.meshgrid(x, y)
    # R = np.sqrt(X ** 2 + Y ** 2)
    Z = np.ones((21, 21)) * 0.1
    surf = ax.plot_surface(X, Y, Z, cmap='rainbow', linewidth=0, antialiased=False)
    plt.show()


def viz_image_effect():
  df = pd.read_csv(r'C:\Users\31408\Desktop\datamat\cube_model_8000.csv')
  df_filter = df.loc[(df['xl'] <= width) & (df['yl'] <= height) & (df['xr'] < width) & (df['yr'] < height)]
  data = np.array(df_filter.values)

  leftx = data[:,3]
  lefty = data[:,4]
  err = data[:,7]

  fig = plt.figure(figsize=(1200, 1000))
  ax = fig.add_subplot(1, 1, 1, projection='3d')
  ax.set_title(f'left')
  ax.stem(leftx, lefty, err)

  x = np.linspace(0, 3088, 20)
  y = np.linspace(0, 2064, 20)
  X, Y = np.meshgrid( x,  y)
  Z = np.ones((len(X), len(Y))) * 0.1
  surf = ax.plot_surface(X, Y, Z, cmap='rainbow', linewidth=0, antialiased=False)
  plt.show()


viz_cube3d_effect()

文章来源:https://blog.csdn.net/Lyncai/article/details/135014992
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。