0

I was able to get a Homography for a WebcamTexture on a texture and a target object.

Now, I want to change the transform of Unity's camera based on the Homography. I've found a way to get the Camera Position from Homography like this:

void cameraPoseFromHomography(const Mat& H, Mat& pose)

{
    pose = Mat::eye(3, 4, CV_32FC1);      // 3x4 matrix, the camera pose
    float norm1 = (float)norm(H.col(0));  
    float norm2 = (float)norm(H.col(1));  
    float tnorm = (norm1 + norm2) / 2.0f; // Normalization value

    Mat p1 = H.col(0);       // Pointer to first column of H
    Mat p2 = pose.col(0);    // Pointer to first column of pose (empty)

    cv::normalize(p1, p2);   // Normalize the rotation, and copies the column to pose

    p1 = H.col(1);           // Pointer to second column of H
    p2 = pose.col(1);        // Pointer to second column of pose (empty)

    cv::normalize(p1, p2);   // Normalize the rotation and copies the column to pose

    p1 = pose.col(0);
    p2 = pose.col(1);

    Mat p3 = p1.cross(p2);   // Computes the cross-product of p1 and p2
    Mat c2 = pose.col(2);    // Pointer to third column of pose
    p3.copyTo(c2);       // Third column is the crossproduct of columns one and two

    pose.col(3) = H.col(2) / tnorm;  //vector t [R|t] is the last column of pose
}

Source: https://stackoverflow.com/a/10781165/4382683

Now, this is relative to the OpenCV image which has dimensions in order of hundreds. But in Unity, the Quad has LocalScale (1,1,1) - I don't know how to translate the pose with this.

Also, how does a Mat of 3x4 be used a position in Unity - where position is just a Vector3 like (1,5,4) and rotation is also a Vector3 like (90, 180, 0).

Any hints or a direction to follow are good enough. Thanks.

Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51
  • So you mean you have a set of 2D screenpoints (vertices) from a camera which you would like to use to place a 3D object in based on their position? – Doh09 Mar 17 '18 at 10:35
  • 2D screenpoints - but not exactly in Unity's reference. I have 2D screenpoints from OpenCV image matrix applied on to a quad as a texture. – Faizuddin Mohammed Mar 17 '18 at 10:36
  • You could try experiment a bit with the different Camera space conversion functions. Such as Camera.WorldToViewPort. That might be helpful. https://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html. I am afraid I have to go for now, but I hope you find a solution. – Doh09 Mar 17 '18 at 10:41
  • 1
    Sure. Will post answer if I find any. – Faizuddin Mohammed Mar 17 '18 at 10:41
  • I suggest posting here as well: https://www.reddit.com/r/opencv/. Since it is dedicated to OpenCV people are likely to have some experience with it. – Doh09 Mar 17 '18 at 10:56

0 Answers0