www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: faridrakh on August 20, 2015, 10:38:38 am

Title: JPCT Solvepnp
Post by: faridrakh on August 20, 2015, 10:38:38 am
i tried pose estimation for my augmeted reality.  i used solvepnp opencv
this is the code
Code: [Select]
TargetPoint = new MatOfPoint3f();
TargetPoint.alloc(4);
TargetPoint.put(0, 0, 0, 0, 0);
TargetPoint.put(1, 0, 4, 0, 0);
TargetPoint.put(2, 0, 4, -4, 0);
TargetPoint.put(3, 0, 0, -4, 0);

imagePointsMat = new MatOfPoint2f();
imagePointsMat.alloc(4);

index1 = point.getCenter();
if (index1 != false){
index1x = point.getCenterx()*2.4f; index1y = point.getCentery()*2.4f;
imagePointsMat.put(0, 0, index1x-960, index1y-540);
}
thumb1 = point.getThumb1();
if (thumb1 != false){
thumb1x = point.getThumb1x()*2.4f; thumb1y = point.getThumb1y()*2.4f;
imagePointsMat.put(1, 0, thumb1x-960, thumb1y-540);
}
middle1 = point.getMiddle1();
if (middle1 != false){
middle1x = point.getMiddle1x()*2.4f; middle1y = point.getMiddle1y()*2.4f;
imagePointsMat.put(2, 0, middle1x-960, middle1y-540);
}
ring = point.getLittle1();
if (ring != false){
ringx = point.getLittle1x()*2.4f; ringy = point.getLittle1y()*2.4f;
imagePointsMat.put(3, 0, ringx-960, ringy-540);
}

Mat cameraMatrix = Mat.eye(3, 3, CvType.CV_32F);
cameraMatrix.put(0, 0, 800);
    cameraMatrix.put(1, 1, 800);
    cameraMatrix.put(0, 2, 800/2);
    cameraMatrix.put(1, 2, 450/2);
    cameraMatrix.put(2, 2, 1);
   
MatOfDouble distCoeffs = new MatOfDouble();
distCoeffs.alloc(5);
distCoeffs.put(1, 0, -0.04372335597872734);
distCoeffs.put(2, 0, 1.203663110733032);
distCoeffs.put(3, 0, 0);
distCoeffs.put(4, 0, 0);
distCoeffs.put(5, 0, -4.552499294281006);

Mat rvec = new Mat(3,1,CvType.CV_32FC1);
    Mat tvec = new Mat(3,1,CvType.CV_32FC1);
   
    Calib3d.solvePnP(TargetPoint, imagePointsMat, cameraMatrix, distCoeffs, rvec, tvec);

Mat R = new Mat(3,3,CvType.CV_32FC1);                       
    Calib3d.Rodrigues(rvec, R);

Mat m44 =  new Mat(4, 4, CvType.CV_32FC1);

        for (int i=0; i<3; i++)
         for (int j=0; j<3; j++)
        m44.put(i, j, R.get(i, j));

        for(int k=0;k<3;k++)
         m44.put(k, 3, tvec.get(k, 0));
       
        Matrix result = new Matrix();
        result.set(0, 0, (float) m44.get(0, 0)[0]);
        result.set(0, 1, (float) m44.get(0, 1)[0]);
        result.set(0, 2, (float) m44.get(0, 2)[0]);
        result.set(0, 3, (float) m44.get(0, 3)[0]);
        result.set(1, 0, (float) m44.get(1, 0)[0]);
        result.set(1, 1, (float) m44.get(1, 1)[0]);
        result.set(1, 2, (float) m44.get(1, 2)[0]);
        result.set(1, 3, (float) m44.get(1, 3)[0]);
        result.set(2, 0, (float) m44.get(2, 0)[0]);
        result.set(2, 1, (float) m44.get(2, 1)[0]);
        result.set(2, 2, (float) m44.get(2, 2)[0]);
        result.set(2, 3, (float) m44.get(2, 3)[0]);
        result.set(3, 0, (float) m44.get(3, 0)[0]);
        result.set(3, 1, (float) m44.get(3, 1)[0]);
        result.set(3, 2, (float) m44.get(3, 2)[0]);
        result.set(3, 3, (float) m44.get(3, 3)[0]);
       
return result;

and i rotate my model using
object.setRotationMatrix(result); but the model rotate all over place

and i tried to rotate camera using
camera.setBack(result); i got result like object rotate.

rotation gone wild all over screen. and blinking when rotate.

is there anyone know where my problem?
Title: Re: JPCT Solvepnp
Post by: EgonOlsen on August 20, 2015, 12:39:54 pm
If it blinks, chances are that you are doing it in the wrong thread. You must not modify jPCT releated instances in any than the rendering thread, for example not in a touch event listener.

If that still doesn't help, make sure that the matrices that you are getting are actually valid and that the coordinate system als well as the kind of matrix (row-major vs. column-major) is correct.

Look at http://www.jpct.net/wiki/index.php?title=Coordinate_system (http://www.jpct.net/wiki/index.php?title=Coordinate_system) and at http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Matrix.html#transformToGL() (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Matrix.html#transformToGL()) for more information.