Log in or register to post comments

Android- solution- calculate the distance between camera to the image target?

March 10, 2017 - 5:06am #1

Hello,



I am beginner in Vuforia, I’m trying to calculate the real distance between the AR camera and an image target as cm via Vuforia on Android, I found this code on the vuforia forums but when I try it, I don't get the good results.

rackableResult result = state.getTrackableResult(tIdx);    

Trackable trackable = result.getTrackable();

Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());

Matrix44F inversMV = SampleMath.Matrix44FInverse(modelViewMatrix_Vuforia);

Matrix44F invTranspMV = SampleMath.Matrix44FTranspose(inversMV);

            float cam_x = invTranspMV.getData()[12];

            float cam_y = invTranspMV.getData()[13];

            float cam_z = invTranspMV.getData()[14];

            Log.v("QCV", "Posx=" + cam_x + ",posy=" + cam_y + ",posz=" + cam_z);

            float distance = new Float(Math.sqrt(cam_x * cam_x + cam_y * cam_y + cam_z * cam_z));

Log.v("distance ",""+distance);

Can you help me please ? is there another function or code to calculate the distance?



Thanks

Android- solution- calculate the distance between camera to the image target?

March 16, 2017 - 4:20am #3

Thank you Jobigoud, it's work correctly for me when i try with this code:

Vuforia::Matrix34F pose = trackable->getPose();     

Vuforia::Vec3F position(pose.data[3], pose.data[7], pose.data[11]);

float distance = sqrt(position.data[0] * position.data[0] +

                      position.data[1] * position.data[1] +

                      position.data[2] * position.data[2]);

LOG("distance: %f", distance);

Replacing modelViewMatrix_Vuforia with getPose matrix and get the values of 3rd,7th and 11th elements fo getPos matrix.

 

Good luck.

 

   

Android- solution- calculate the distance between camera to the image target?

March 14, 2017 - 5:14am #2

The code looks correct, although I don't think you need the step that invert the matrix. The trackable gives the pose of the object in the camera frame of reference, get the position components and compute the euclidean distance.

The values are in meters, not centimeters.

Log in or register to post comments