I have been looking at creating rotation matrices from a direction vector in the OpenCV coordinate space and there is one thing that has me slightly confused. Here is an OpenCV rotation matrix, that I got from an opencv function (let us call this matrix
array([[-0.4136457 , -0.19724711, 0.88881427], [-0.57926765, 0.810177 , -0.08978985], [-0.70238609, -0.55200255, -0.44938511]])
Now, starting from the z-axes unit vector, I am trying to recreate this vector. So, I have the following code:
def basis(v): v = v / np.linalg.norm(v) if v > 0.9: b1 = np.asarray([0.0, 1.0, 0.0]) else: b1 = np.asarray([1.0, 0.0, 0.0]) b1 -= v * np.dot(b1, v) b1 *= np.reciprocal(np.linalg.norm(b1)) b2 = np.cross(v, b1) return b1, b2, v
I can call this function as:
x, y, z = basis(r[:, 2])
Then I compute the rotation matrix as:
avg = np.asarray([[x, y, z], [x, y, z], [x, y, z]])
Now running this code returns:
array([[ 0.4582676 , 0. , 0.88881427], [ 0.17414826, -0.98061724, -0.08978985], [ 0.8715866 , 0.19593324, -0.44938511]])
So, the signs along the x and y-axes are flipped.
Now, in my basis function, if I change the line
b1 = np.asarray([1.0, 0.0, 0.0]) to
b1 = np.asarray([-1.0, 0.0, 0.0]). It returns with the correct sign like:
array([[-0.4582676 , 0. , 0.88881427], [-0.17414826, 0.98061724, -0.08978985], [-0.8715866 , -0.19593324, -0.44938511]])
I am guessing this has something to do with the handedness as the opencv origin is at the top left corner and the y axes is increasing in the downward direction rather than upward but the thing that has me confused is why does chaging the sign of the x coordinate of the unit vector makes a difference? I was expecting to have to change the other condition i.e.
b1 = np.asarray([0.0, 1.0, 0.0]) to have the sign flip in the y coordinate.