Search Unity

Question World Position to Image Coordinates (PhotoCaptureFrame Hololens 2)

Discussion in 'AR' started by felderto, Jun 9, 2021.

  1. felderto

    felderto

    Joined:
    Mar 4, 2021
    Posts:
    1
    Hello everyone

    I am currently working with Vuforia on Hololens 2 in Unity 2019.4.28f1. I track 4 points (corners) of a Marker and then take an Image of that scene (PhotoCapture class), while also saving the World Space Coordinates of the 4 points that I track, and also the cameraToWorldMatrix and projectionMatrix with the help of the according methods in the PhotoCaptureFrame class from Unity.Windows.WebCam namespace.

    My goal is to convert these 4 points from World Space to Pixel Coordinates so that I can later locate these points on the image that I took with the PhotoCapture Class.

    Unfortunately, the documentation for the TryGetProjectionMatrix Method is rather sparse.

    Everything seemed to work at first, since I get some cameraToWorldMatrix and projectionMatrix from the PhotoCaptureFrame Class. I save the image by exporting the texture to a PNG format with the EncodeToPNG() Method.

    Now my Question:

    How is the projectionMatrix composed? Are camera intrinsics included? It is not clear from the documentation.

    I tried to get the Pixel coordinates in the following way (source is this work):

    Code (CSharp):
    1.     public void WorldToPixelPos(Vector3 WorldSpacePos, Matrix4x4 WorldToCam, Matrix4x4 ProjMat, Texture Tex)
    2.     {
    3.         Vector3 CameraSpacePos = WorldToCam.MultiplyPoint(WorldSpacePos);
    4.         Vector3 ImagePosUnnormalized = ProjMat.MultiplyPoint(CameraSpacePos);
    5.         Vector2 ImagePosProjected = new Vector2(ImagePosUnnormalized.x, ImagePosUnnormalized.y) / ImagePosUnnormalized.z;
    6.         Vector2 ImagePosZeroToOne = (ImagePosProjected * 0.5f) + new Vector2(0.5f, 0.5f);
    7.         Vector2 PixelPos = new Vector2(ImagePosZeroToOne.x * Tex.width, (1 - ImagePosZeroToOne.y) * Tex.height);
    8.         dataList.Add(PixelPos[0].ToString() + ",");
    9.         dataList.Add(PixelPos[1].ToString() + ",");
    10.     }
    But this gives me the wrong image coordinates. What am I doing wrong in the calculations? Can someone tell me how to calculate the exact pixel coordinates from the 3D world position?

    Here is an image of the wrongly calculated corner points:

    CornerPointsImageCoord.jpg


    Thanks for your help guys!
     
    Last edited: Jun 10, 2021