Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The following code:

from __future__ import print_function
from __future__ import division

import numpy as np
import cv2 as cv

print('cv:\n', cv.getBuildInformation())

# img = cv.imread('8i77L.png')
img = cv.imread('left01.jpg')

CHECKERBOARD_WIDTH = 9
CHECKERBOARD_HEIGHT = 6

ret, corners = cv.findChessboardCorners(img, (CHECKERBOARD_WIDTH, CHECKERBOARD_HEIGHT))

if ret == True:
    cv.drawChessboardCorners(img, (CHECKERBOARD_WIDTH, CHECKERBOARD_HEIGHT), corners, ret)

    pattern_size = (CHECKERBOARD_WIDTH, CHECKERBOARD_HEIGHT)
    objp = np.zeros((np.prod(pattern_size), 3), np.float32)
    objp[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    square_size = 0.05
    objp *= square_size
    print('objp:', objp.shape)
    print('corners:', corners.shape)

    intrinsics = np.array([[600, 0, img.shape[1]/2],
                           [0, 600, img.shape[0]/2],
                           [0, 0, 1]],
                           dtype=np.float32)
    print('intrinsics: {}\n{}'.format(intrinsics.shape, intrinsics))

    ret, rvec, tvec = cv.solvePnP(objp, corners, intrinsics, None)
    print('rvec:', rvec.T)
    print('tvec:', tvec.T)

    img = cv.drawFrameAxes(img, intrinsics, None, rvec, tvec, square_size, 3)
    cv.imshow('Pose', img)
    cv.imwrite('test_solvePnP.png', img)
    cv.waitKey()

gives:

image description