# creating rotation matrix in a left handed coordinate system from a direction vector

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 r):

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] > 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[0], y[0], z[0]],
[x[1], y[1], z[1]],
[x[2], y[2], z[2]]])


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.

• This follows directly from the left-hand rule, doesn't it? If you hold your left hand so that your fingers curl along the angle from v to b1, your thumb points in the direction of their cross product b2. If you negate b1, then you have to turn your hand around to do the same trick, pointing your thumb in the opposite direction. Starting from a negated x gives you a negated y. Have I misunderstood what you're asking? – DMGregory Nov 26 '18 at 13:25