pythonopencvaruco

how to estimate pose of single marker in opencv-python 4.8.0?


As shown in this Question There are API change for aruco package. I managed to detect the markers and drawDetectedMarkers by following accepted answer but I can't find 'estimatePoseSingleMarkers' in both cv2.aruco and cv2.aruco.detector attributes. still got error message

module 'cv2.aruco' has no attribute 'estimatePoseSingleMarkers'

or

'cv2.aruco.ArucoDetector' object has no attribute 'estimatePoseSingleMarkers'


Solution

  • Based on Christoph Rackwitz comment as i searching for solvePnP with aruco tag, I found this Answer

    estimatePoseSingleMarkers no longer exists as of version 4.7. Here, I replaced the function for you using SolvePnP:

    def my_estimatePoseSingleMarkers(corners, marker_size, mtx, distortion):
        '''
        This will estimate the rvec and tvec for each of the marker corners detected by:
           corners, ids, rejectedImgPoints = detector.detectMarkers(image)
        corners - is an array of detected corners for each detected marker in the image
        marker_size - is the size of the detected markers
        mtx - is the camera matrix
        distortion - is the camera distortion matrix
        RETURN list of rvecs, tvecs, and trash (so that it corresponds to the old estimatePoseSingleMarkers())
        '''
        marker_points = np.array([[-marker_size / 2, marker_size / 2, 0],
                                  [marker_size / 2, marker_size / 2, 0],
                                  [marker_size / 2, -marker_size / 2, 0],
                                  [-marker_size / 2, -marker_size / 2, 0]], dtype=np.float32)
        trash = []
        rvecs = []
        tvecs = []
        for c in corners:
            nada, R, t = cv2.solvePnP(marker_points, c, mtx, distortion, False, cv2.SOLVEPNP_IPPE_SQUARE)
            rvecs.append(R)
            tvecs.append(t)
            trash.append(nada)
        return rvecs, tvecs, trash
    

    if anyone have this problem,I've wrote example script in my github. https://github.com/Menginventor/aruco_example_cv_4.8.0