creating rotation matrices in opencv coordinate system












1















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.










share|improve this question























  • There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

    – Nico Schertler
    Nov 26 '18 at 17:08
















1















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.










share|improve this question























  • There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

    – Nico Schertler
    Nov 26 '18 at 17:08














1












1








1








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.










share|improve this question














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.







opencv geometry coordinates coordinate-transformation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 10:48









LucaLuca

3,25552782




3,25552782













  • There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

    – Nico Schertler
    Nov 26 '18 at 17:08



















  • There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

    – Nico Schertler
    Nov 26 '18 at 17:08

















There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

– Nico Schertler
Nov 26 '18 at 17:08





There is an infinite number of rotation matrices with a given z-axis. You chose one of them. Whatever created your reference matrix r simply chose another (either because it had more information about the underlying problem or randomly).

– Nico Schertler
Nov 26 '18 at 17:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479479%2fcreating-rotation-matrices-in-opencv-coordinate-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479479%2fcreating-rotation-matrices-in-opencv-coordinate-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)