Ask Your Question

Revision history [back]

OpenCV pose enstimation python to java

I have pose estimation Image processing code on python -

> def process_img(fname, mtx, dist):
> 
>     img = fname  # cv2.imread(fname)
> 
>     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
> 
>     ret, corners = cv2.findChessboardCorners(gray, (7,
> 6), None)
> 
>     if ret:
>         criteria = (cv2.TERM_CRITERIA_EPS +
> cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
>         objp = np.zeros((6 * 7, 3), np.float32)
>         objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)
> 
>         axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3)
>         # axis = np.float32([[0, 0, 0], [0, 3, 0], [3, 3, 0], [3, 0, 0],
>         #                   [0, 0, -3], [0, 3, -3], [3, 3, -3], [3, 0, -3]])
> 
>         cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
> 
>         # Find the rotation and translation vectors.
>         rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners, mtx,
> dist)
> 
>         # project 3D points to image plane
>         imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs,
> mtx, dist)
>         img = draw_vectors(img, corners, imgpts)
>     return img

I wrote this on java

public Mat processImg(Mat frame, Mat mtx, Mat dist) {
    Mat tvec=new Mat();
    Mat rvec = new Mat();
    MatOfDouble dist1 = new MatOfDouble(dist);
    Mat gray = new Mat();
    MatOfPoint2f actual_corners = new MatOfPoint2f();
    MatOfPoint2f imgpts = new MatOfPoint2f();
    Size patternSize = new Size(7,6);
    Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);

    MatOfPoint3f objectPoints= new MatOfPoint3f();

    boolean found_chess = Calib3d.findChessboardCorners(gray, patternSize, actual_corners,  Calib3d.CALIB_CB_ADAPTIVE_THRESH + Calib3d.CALIB_CB_NORMALIZE_IMAGE);

    if(found_chess)
    {
        Calib3d.solvePnP(objectPoints, actual_corners, mtx, dist1, rvec, tvec);
        Calib3d.projectPoints(objectPoints, rvec, tvec, mtx, dist1, imgpts);


    }
    return frame; }

but I don't know what to do next, I cant understand how to convert objp var from python to java and how to convert the axis from python to java. maybe someone can help me with this part of the code ?