Why is cv2.projectPoints not behaving as I expect?
10,269
For no operation the rotation matrix should be an identity
1 0 0
0 1 0
0 0 1
edit: It's a very simple function to write yourself.
foreach input 3dpoint xyz
3dpoint tmp = cameraintrinsic(3x3) * rotationvect(1x3) * xyz(3x1) + cameraintrinsic(3x3)*translation(3,1)
2dpoint screen = tmp.x/tmp.z, tmp.y / tmp.z
The /tmp.z is because the result is returned in homogeneous coords = essentially 2d coords with a scaling factor.
Author by
Phil Mayes
Updated on June 10, 2022Comments
-
Phil Mayes almost 2 years
I have a 3D world that I am trying to map to a 2D view using cv2.projectPoints, but it is not acting as I expect. My grasp of opencv, numpy and matrix ops is weak, so I must be making a wrong assumption somewhere. This code:
src = np.ones((6, 3)) src[:,1] = 2 src[:,2] = range(6) # source points rvec = np.array([0,0,0], np.float) # rotation vector tvec = np.array([0,0,0], np.float) # translation vector fx = fy = 1.0 cx = cy = 0.0 cameraMatrix = np.array([[fx,0,cx],[0,fy,cy],[0,0,1]]) result = cv2.projectPoints(src, rvec, tvec, cameraMatrix, None) for n in range(len(src)): print src[n], '==>', result[0][n]
generates this output:
[ 1. 2. 0.] ==> [[ 1. 2.]] [ 1. 2. 1.] ==> [[ 1. 2.]] [ 1. 2. 2.] ==> [[ 0.5 1. ]] [ 1. 2. 3.] ==> [[ 0.33333333 0.66666667]] [ 1. 2. 4.] ==> [[ 0.25 0.5 ]] [ 1. 2. 5.] ==> [[ 0.2 0.4]]
The x- and y-values are being divided by the z-value?! I thought that as I am applying no transformations with rvec and tvec that the output should match the [x,y] values of src.