【发布时间】:2019-09-21 18:09:10
【问题描述】:
我想用我用相机和 OpenCV 在 Python 中检测到的机械臂拾取彩色立方体。我设法检测到不同颜色的立方体,并使用棋盘过程校准了相机。
设置:
问题是我不明白用相机从物体获取坐标并将它们转换到机械臂进行拾取的其余过程。
以下步骤已完成:
使用 HSV 分隔颜色边界并绘制边界框。所以我有对象的像素 x,y。
-
校准相机得到以下相机矩阵和畸变系数:
相机矩阵:[[1.42715609e+03 0.00000000e+00 9.13700651e+02] [0.00000000e+00 1.43275509e+03 5.58917609e+02] [0.00000000e+00 0.00000000e+00 1.00000000e+00]]
失真:[[ 0.03924722 -0.30622971 0.00124042 -0.00303094 0.49458539]]]
试图找出 OpenCV 文档中的后续步骤
结果
我阅读了此页面上的 API 文档:https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html
但以我的技能水平,我似乎无法提取实际步骤来实现我的目标。
我的问题:
- 如何使用相机矩阵和畸变系数获取图像帧中对象的坐标?
- 如何将图像帧上的坐标转换为机器人末端执行器坐标?
- 如果我将相机保持在固定位置。这是否意味着我只需要进行校准?
******* 编辑: *******
我采用了不同的方法。我设法通过SVD解决了两个坐标系之间的旋转和平移。但我错误地认为我可以使用像素坐标将相机坐标系转换为机器人坐标系。我认为您需要先翻译 u, v 值。
如何将像素 uv 转换为世界坐标,以便我可以使用下面的代码对机械臂进行平移和旋转?
这是我的代码:
#######################################################################
# Step 1: Input camera and world coordinates, and calculate centroids #
#######################################################################
print("")
# Camera and robot to world coordinates
Pc = np.matrix([[604,119,0],[473,351,0], [730,329,0]])
print("Camera points matrix: ")
print(Pc)
print("")
Pr = np.matrix([[177,-38,0],[264,-93,0], [258,4.7,0]])
print("Robot points matrix: ")
print(Pr)
print("")
# Calculate centroids
Cc = Pc.mean(axis=0)
Cr = Pr.mean(axis=0)
print("Centroid camera: ")
print(Cc)
print("")
print("Centroid robot: ")
print(Cr)
print("")
# Pc and Pr - centroids of Pc and Pr
Pc_minus_Cc = np.subtract(Pc, Cc)
print("Pc - centroidC: ")
print(Pc_minus_Cc)
print("")
Pr_minus_Cr = np.subtract(Pr, Cr)
print("Pr - centroidR: ")
print(Pr_minus_Cr)
print("")
############################################################################
# Step 2: Calculate H, perform SVD and get rotation and translation matrix #
############################################################################
# Get H
print("(Pr - centroidR) transposed: ")
print(Pr_minus_Cr.T)
print("")
H = np.matmul(Pc_minus_Cc, Pr_minus_Cr.T)
print("H: ")
print(H)
print("")
# Perform SVD
u, s, v = np.linalg.svd(H)
print("SVD result: ")
print("u: ")
print("")
print(u)
print("")
print("s: ")
print(s)
print("")
print("v: ")
print(v)
print("")
# Calculate rotation matrix
R = np.matmul(v,u.T)
print("Rotation matrix: ")
print(R)
# Calculate t
t = -R * Cc.T + Cr.T
print("t: ")
print(t)
【问题讨论】:
-
相机矩阵与相机本身(焦距、图像大小等)有关,我建议this article。换句话说,对于问题 3,如果您有相同的相机/分辨率,则校准(在这种情况下为相机矩阵)是相同的。对于问题 1 和 2,您需要深度,或者知道可以从中获得深度的点。你可以看看this和this
-
谢谢!我现在正在阅读它。
-
嗨!我现在有点远,请参阅我的编辑。如果你能帮助我完成最后一步,能否告诉我?
标签: python opencv camera-calibration robotics coordinate-transformation