2D to 3D Projection with given Z in world












2















I'm sorry if it has been ask before but I couldn't find the proper answer to my question.



For a better understanding, let me briefly explain the context of my problem



Context



I have two images (A and B) with non planar object on it. I would like to be able to take the coordinate of a pixel pA from A and project it into B. Since my scene is not planar, I can't use homography. What I want to do is first project my pixel pA into the 3D world and then project the result into the image B to get pB.
pA (2D) -> pWorld (3D) -> pB (2D).
Fortunately, I know the coordinate z of pworld. My question concerns the first step pA (2D) -> pWorld (3D).



Question



How to project my 2D point pA (u,v) into the world (pWorld=(X,Y,Z)), Z being given?
I also have the extrinsic matrix Rt (3x4) and the intrinsic matrix K (3x3) of my camera.



What I tried



I know that :
s*(u v 1)' = K * Rt * (X Y Z)' [1]



s is the scale.
But I would like to have the opposite process, Z being given. Something like:



(X Y) = SOMETHING * (u v)



I can rewrite [1] to get
s*(u v 1/s 1/s)' = G * (X Y Z 1)'



with G = (l1 l2 l3 l4) (l means line)



l1 = first line of (K*Rt)



l2 = second line of (K*Rt)



l3 = 0 0 1/Z 0



l4 = 0 0 0 1



G is invertible and I can then have
(X Y Z 1)' = inv(G) * (us vs 1 1)'



But I can't use that since I don't know the scale. I think I'm a bit confused concerning this scale thing. I know usually we normalized to get rid of it but here, I can't.



Maybe that's not the good way to proceed. If someone can explain me the good way, I would be really glad to hear about it.



Thank you in advance.










share|improve this question

























  • it can help you : stackoverflow.com/questions/12007775/…

    – user2502117
    Nov 20 '18 at 16:02











  • Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

    – geko
    Nov 21 '18 at 8:20
















2















I'm sorry if it has been ask before but I couldn't find the proper answer to my question.



For a better understanding, let me briefly explain the context of my problem



Context



I have two images (A and B) with non planar object on it. I would like to be able to take the coordinate of a pixel pA from A and project it into B. Since my scene is not planar, I can't use homography. What I want to do is first project my pixel pA into the 3D world and then project the result into the image B to get pB.
pA (2D) -> pWorld (3D) -> pB (2D).
Fortunately, I know the coordinate z of pworld. My question concerns the first step pA (2D) -> pWorld (3D).



Question



How to project my 2D point pA (u,v) into the world (pWorld=(X,Y,Z)), Z being given?
I also have the extrinsic matrix Rt (3x4) and the intrinsic matrix K (3x3) of my camera.



What I tried



I know that :
s*(u v 1)' = K * Rt * (X Y Z)' [1]



s is the scale.
But I would like to have the opposite process, Z being given. Something like:



(X Y) = SOMETHING * (u v)



I can rewrite [1] to get
s*(u v 1/s 1/s)' = G * (X Y Z 1)'



with G = (l1 l2 l3 l4) (l means line)



l1 = first line of (K*Rt)



l2 = second line of (K*Rt)



l3 = 0 0 1/Z 0



l4 = 0 0 0 1



G is invertible and I can then have
(X Y Z 1)' = inv(G) * (us vs 1 1)'



But I can't use that since I don't know the scale. I think I'm a bit confused concerning this scale thing. I know usually we normalized to get rid of it but here, I can't.



Maybe that's not the good way to proceed. If someone can explain me the good way, I would be really glad to hear about it.



Thank you in advance.










share|improve this question

























  • it can help you : stackoverflow.com/questions/12007775/…

    – user2502117
    Nov 20 '18 at 16:02











  • Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

    – geko
    Nov 21 '18 at 8:20














2












2








2


1






I'm sorry if it has been ask before but I couldn't find the proper answer to my question.



For a better understanding, let me briefly explain the context of my problem



Context



I have two images (A and B) with non planar object on it. I would like to be able to take the coordinate of a pixel pA from A and project it into B. Since my scene is not planar, I can't use homography. What I want to do is first project my pixel pA into the 3D world and then project the result into the image B to get pB.
pA (2D) -> pWorld (3D) -> pB (2D).
Fortunately, I know the coordinate z of pworld. My question concerns the first step pA (2D) -> pWorld (3D).



Question



How to project my 2D point pA (u,v) into the world (pWorld=(X,Y,Z)), Z being given?
I also have the extrinsic matrix Rt (3x4) and the intrinsic matrix K (3x3) of my camera.



What I tried



I know that :
s*(u v 1)' = K * Rt * (X Y Z)' [1]



s is the scale.
But I would like to have the opposite process, Z being given. Something like:



(X Y) = SOMETHING * (u v)



I can rewrite [1] to get
s*(u v 1/s 1/s)' = G * (X Y Z 1)'



with G = (l1 l2 l3 l4) (l means line)



l1 = first line of (K*Rt)



l2 = second line of (K*Rt)



l3 = 0 0 1/Z 0



l4 = 0 0 0 1



G is invertible and I can then have
(X Y Z 1)' = inv(G) * (us vs 1 1)'



But I can't use that since I don't know the scale. I think I'm a bit confused concerning this scale thing. I know usually we normalized to get rid of it but here, I can't.



Maybe that's not the good way to proceed. If someone can explain me the good way, I would be really glad to hear about it.



Thank you in advance.










share|improve this question
















I'm sorry if it has been ask before but I couldn't find the proper answer to my question.



For a better understanding, let me briefly explain the context of my problem



Context



I have two images (A and B) with non planar object on it. I would like to be able to take the coordinate of a pixel pA from A and project it into B. Since my scene is not planar, I can't use homography. What I want to do is first project my pixel pA into the 3D world and then project the result into the image B to get pB.
pA (2D) -> pWorld (3D) -> pB (2D).
Fortunately, I know the coordinate z of pworld. My question concerns the first step pA (2D) -> pWorld (3D).



Question



How to project my 2D point pA (u,v) into the world (pWorld=(X,Y,Z)), Z being given?
I also have the extrinsic matrix Rt (3x4) and the intrinsic matrix K (3x3) of my camera.



What I tried



I know that :
s*(u v 1)' = K * Rt * (X Y Z)' [1]



s is the scale.
But I would like to have the opposite process, Z being given. Something like:



(X Y) = SOMETHING * (u v)



I can rewrite [1] to get
s*(u v 1/s 1/s)' = G * (X Y Z 1)'



with G = (l1 l2 l3 l4) (l means line)



l1 = first line of (K*Rt)



l2 = second line of (K*Rt)



l3 = 0 0 1/Z 0



l4 = 0 0 0 1



G is invertible and I can then have
(X Y Z 1)' = inv(G) * (us vs 1 1)'



But I can't use that since I don't know the scale. I think I'm a bit confused concerning this scale thing. I know usually we normalized to get rid of it but here, I can't.



Maybe that's not the good way to proceed. If someone can explain me the good way, I would be really glad to hear about it.



Thank you in advance.







3d camera 2d projection depth






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 2:21







geko

















asked Nov 20 '18 at 13:45









gekogeko

113




113













  • it can help you : stackoverflow.com/questions/12007775/…

    – user2502117
    Nov 20 '18 at 16:02











  • Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

    – geko
    Nov 21 '18 at 8:20



















  • it can help you : stackoverflow.com/questions/12007775/…

    – user2502117
    Nov 20 '18 at 16:02











  • Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

    – geko
    Nov 21 '18 at 8:20

















it can help you : stackoverflow.com/questions/12007775/…

– user2502117
Nov 20 '18 at 16:02





it can help you : stackoverflow.com/questions/12007775/…

– user2502117
Nov 20 '18 at 16:02













Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

– geko
Nov 21 '18 at 8:20





Thanks for your answer. I did check that. It is a simpler case than mine since my 3D world coordinate system is different from my 3D camera coordinate system. (s != z). I found a solution inspired by that kind of computation but it is damn ugly.

– geko
Nov 21 '18 at 8:20












1 Answer
1






active

oldest

votes


















0














I found a solution but it is damn ugly.



Let's consider the 3x4 matrix M:



M = K*Rt = (mij) 1<i<3, 1<j<4


For simplification, let's also consider the coefficients A and B:



A = (m12-m32*u)/(m22-m32v)
B = (m31*u-m11)/(m31*v-m21)


The notation explained, let's move on to the system.
As I said, the system is:



s*(u v 1)' = M*(X Y Z 1)'


We have 3 equations and 3 unknowns : s, X and Y.
We can notice that:



s = m31*X + m32*Y + m33*Z + m34


Note that if you want to project into the camera coordinates system and not in the world coordinates system (similar to a case where there is no rotation and translation), you have s = Z which is a way easier system to solve (example here To calculate world coordinates from screen coordinates with OpenCV)



With this in mind, we can reduce the original system into a system of 2 equations with 2 unknowns (X and Y):



Then, after some calculations, we finally get:



X = [Z*((m23-M33*v)*A-m13+m33*u) + (m24-m34*v)*A-m14+m34*u ] / [A*(m31*v-m21)-m31*u+m11]

Y = [Z*((m13-m33*u)-B*(m23-m33*v)) + m14-m34*u-B*(m24-m34*v)] / [B*(m22-m32*v)-m12+m32*u]


It is the expression of X and Y in function of u, v and Z.
I tested that with my project and it was working.



Don't know if there is a cleaner way to compute that (with Matrix and stuff), but that's all I could come up with for now.






share|improve this answer























    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%2f53394418%2f2d-to-3d-projection-with-given-z-in-world%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found a solution but it is damn ugly.



    Let's consider the 3x4 matrix M:



    M = K*Rt = (mij) 1<i<3, 1<j<4


    For simplification, let's also consider the coefficients A and B:



    A = (m12-m32*u)/(m22-m32v)
    B = (m31*u-m11)/(m31*v-m21)


    The notation explained, let's move on to the system.
    As I said, the system is:



    s*(u v 1)' = M*(X Y Z 1)'


    We have 3 equations and 3 unknowns : s, X and Y.
    We can notice that:



    s = m31*X + m32*Y + m33*Z + m34


    Note that if you want to project into the camera coordinates system and not in the world coordinates system (similar to a case where there is no rotation and translation), you have s = Z which is a way easier system to solve (example here To calculate world coordinates from screen coordinates with OpenCV)



    With this in mind, we can reduce the original system into a system of 2 equations with 2 unknowns (X and Y):



    Then, after some calculations, we finally get:



    X = [Z*((m23-M33*v)*A-m13+m33*u) + (m24-m34*v)*A-m14+m34*u ] / [A*(m31*v-m21)-m31*u+m11]

    Y = [Z*((m13-m33*u)-B*(m23-m33*v)) + m14-m34*u-B*(m24-m34*v)] / [B*(m22-m32*v)-m12+m32*u]


    It is the expression of X and Y in function of u, v and Z.
    I tested that with my project and it was working.



    Don't know if there is a cleaner way to compute that (with Matrix and stuff), but that's all I could come up with for now.






    share|improve this answer




























      0














      I found a solution but it is damn ugly.



      Let's consider the 3x4 matrix M:



      M = K*Rt = (mij) 1<i<3, 1<j<4


      For simplification, let's also consider the coefficients A and B:



      A = (m12-m32*u)/(m22-m32v)
      B = (m31*u-m11)/(m31*v-m21)


      The notation explained, let's move on to the system.
      As I said, the system is:



      s*(u v 1)' = M*(X Y Z 1)'


      We have 3 equations and 3 unknowns : s, X and Y.
      We can notice that:



      s = m31*X + m32*Y + m33*Z + m34


      Note that if you want to project into the camera coordinates system and not in the world coordinates system (similar to a case where there is no rotation and translation), you have s = Z which is a way easier system to solve (example here To calculate world coordinates from screen coordinates with OpenCV)



      With this in mind, we can reduce the original system into a system of 2 equations with 2 unknowns (X and Y):



      Then, after some calculations, we finally get:



      X = [Z*((m23-M33*v)*A-m13+m33*u) + (m24-m34*v)*A-m14+m34*u ] / [A*(m31*v-m21)-m31*u+m11]

      Y = [Z*((m13-m33*u)-B*(m23-m33*v)) + m14-m34*u-B*(m24-m34*v)] / [B*(m22-m32*v)-m12+m32*u]


      It is the expression of X and Y in function of u, v and Z.
      I tested that with my project and it was working.



      Don't know if there is a cleaner way to compute that (with Matrix and stuff), but that's all I could come up with for now.






      share|improve this answer


























        0












        0








        0







        I found a solution but it is damn ugly.



        Let's consider the 3x4 matrix M:



        M = K*Rt = (mij) 1<i<3, 1<j<4


        For simplification, let's also consider the coefficients A and B:



        A = (m12-m32*u)/(m22-m32v)
        B = (m31*u-m11)/(m31*v-m21)


        The notation explained, let's move on to the system.
        As I said, the system is:



        s*(u v 1)' = M*(X Y Z 1)'


        We have 3 equations and 3 unknowns : s, X and Y.
        We can notice that:



        s = m31*X + m32*Y + m33*Z + m34


        Note that if you want to project into the camera coordinates system and not in the world coordinates system (similar to a case where there is no rotation and translation), you have s = Z which is a way easier system to solve (example here To calculate world coordinates from screen coordinates with OpenCV)



        With this in mind, we can reduce the original system into a system of 2 equations with 2 unknowns (X and Y):



        Then, after some calculations, we finally get:



        X = [Z*((m23-M33*v)*A-m13+m33*u) + (m24-m34*v)*A-m14+m34*u ] / [A*(m31*v-m21)-m31*u+m11]

        Y = [Z*((m13-m33*u)-B*(m23-m33*v)) + m14-m34*u-B*(m24-m34*v)] / [B*(m22-m32*v)-m12+m32*u]


        It is the expression of X and Y in function of u, v and Z.
        I tested that with my project and it was working.



        Don't know if there is a cleaner way to compute that (with Matrix and stuff), but that's all I could come up with for now.






        share|improve this answer













        I found a solution but it is damn ugly.



        Let's consider the 3x4 matrix M:



        M = K*Rt = (mij) 1<i<3, 1<j<4


        For simplification, let's also consider the coefficients A and B:



        A = (m12-m32*u)/(m22-m32v)
        B = (m31*u-m11)/(m31*v-m21)


        The notation explained, let's move on to the system.
        As I said, the system is:



        s*(u v 1)' = M*(X Y Z 1)'


        We have 3 equations and 3 unknowns : s, X and Y.
        We can notice that:



        s = m31*X + m32*Y + m33*Z + m34


        Note that if you want to project into the camera coordinates system and not in the world coordinates system (similar to a case where there is no rotation and translation), you have s = Z which is a way easier system to solve (example here To calculate world coordinates from screen coordinates with OpenCV)



        With this in mind, we can reduce the original system into a system of 2 equations with 2 unknowns (X and Y):



        Then, after some calculations, we finally get:



        X = [Z*((m23-M33*v)*A-m13+m33*u) + (m24-m34*v)*A-m14+m34*u ] / [A*(m31*v-m21)-m31*u+m11]

        Y = [Z*((m13-m33*u)-B*(m23-m33*v)) + m14-m34*u-B*(m24-m34*v)] / [B*(m22-m32*v)-m12+m32*u]


        It is the expression of X and Y in function of u, v and Z.
        I tested that with my project and it was working.



        Don't know if there is a cleaner way to compute that (with Matrix and stuff), but that's all I could come up with for now.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 8:16









        gekogeko

        113




        113
































            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%2f53394418%2f2d-to-3d-projection-with-given-z-in-world%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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents