簡體   English   中英

使用Freenect和Python從Kinect點雲

[英]Point Cloud from Kinect using Freenect and Python

我有一個通過Linux Mint連接的XBox 360 Kinect傳感器。 我正在嘗試創建一個CSV文件,其中Z值表示距傳感器的距離 - 簡而言之,我想將Kinect用作短距離3D掃描儀。 我有下面的代碼生成一個CSV文件,但值很奇怪,似乎並不代表真實世界的XYZ值。 以下是生成的CSV文件的示例,所有行都具有相似的值。 x,y,z -0.22424937966362582,0.16117004627017431,-0.39249255932230664 -0.22424937966362582,0.16050597521014062,-0.39249255932230664 -0.22424937966362582,0.15984190415010693,-0.39249255932230664

這些數字是什么意思? 如何獲得以米或厘米為單位的Z值? 我哪里錯了?

這是我正在運行的代碼示例,該代碼是從這個Github頁面中收集的https://github.com/amiller/libfreenect-goodie s

這是我的Python代碼。

#!/usr/bin/python

import sys, traceback
import freenect
import cv2
import numpy as np
import csv
print "running"

def get_depth():
    array,_ = freenect.sync_get_depth()
    array = array.astype(np.uint8)
    return array

def depth2xyzuv(depth, u=None, v=None):
    if u is None or v is None:
        u,v = np.mgrid[:480,:640]  
    # Build a 3xN matrix of the d,u,v data
    C = np.vstack((u.flatten(), v.flatten(), depth.flatten(), 0*u.flatten()+1))
    # Project the duv matrix into xyz using xyz_matrix()
    X,Y,Z,W = np.dot(xyz_matrix(),C)
    X,Y,Z = X/W, Y/W, Z/W
    xyz = np.vstack((X,Y,Z)).transpose()
    xyz = xyz[Z<0,:]
    # Project the duv matrix into U,V rgb coordinates using rgb_matrix() and xyz_matrix()
    U,V,_,W = np.dot(np.dot(uv_matrix(), xyz_matrix()),C)
    U,V = U/W, V/W
    uv = np.vstack((U,V)).transpose()
    uv = uv[Z<0,:]
    return xyz
def uv_matrix():
  """Returns a matrix you can use to project XYZ coordinates (in meters) into
      U,V coordinates in the kinect RGB image"""
  rot = np.array([[ 9.99846e-01,   -1.26353e-03,   1.74872e-02], 
                  [-1.4779096e-03, -9.999238e-01,  1.225138e-02],
                  [1.747042e-02,   -1.227534e-02,  -9.99772e-01]])
  trans = np.array([[1.9985e-02, -7.44237e-04,-1.0916736e-02]])
  m = np.hstack((rot, -trans.transpose()))
  m = np.vstack((m, np.array([[0,0,0,1]])))
  KK = np.array([[529.2, 0, 329, 0],
                 [0, 525.6, 267.5, 0],
                 [0, 0, 0, 1],
                 [0, 0, 1, 0]])
  m = np.dot(KK, (m))
  return m
def xyz_matrix():
  fx = 594.21
  fy = 591.04
  a = -0.0030711
  b = 3.3309495
  cx = 339.5
  cy = 242.7
  mat = np.array([[1/fx, 0, 0, -cx/fx],
                  [0, -1/fy, 0, cy/fy],
                  [0,   0, 0,    -1],
                  [0,   0, a,     b]])
  return mat
depth = get_depth()
depthpoints = depth2xyzuv(depth)

print "Create csv header..."
f = open("/home/gerry/depthtest.csv",'a')
f.write("x,y,z\n")
f.close()
print "writing to text file...please wait...."
with open("/home/gerry/depthtest.csv", 'a') as f:
    csvwriter = csv.writer(f)
    csvwriter.writerows(depthpoints)   
print "finished writing to text file..."
print "done"

讀這個:

如何在Matlab中將Kinect原始深度信息轉換為米?

和libfreenect文檔https://openkinect.org/wiki/Imaging_Information

存在可用的校准信息z基本上是從第一鏈路示例處的示例計算的相機的距離

smaple python代碼來計算距離

def rawdepthtometer(x):

    ans = (x>>3)/1000   #from stackover flow and libfreekinect imaging documentation
    return ans

暫無
暫無

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

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