【发布时间】:2016-10-12 17:18:09
【问题描述】:
我有一个通过 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-goodies 收集的
这是我的 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"
【问题讨论】:
标签: python xbox360 openkinect