Python+OpenCV:姿态估计(Pose Estimation)

82 篇文章 20 订阅

Python+OpenCV:姿态估计(Pose Estimation)

####################################################################################################
# 姿态估计(Pose Estimation)
def lmc_cv_pose_estimation(method):
    """
        函数功能: 姿态估计(Pose Estimation)。
    """

    # Load previously saved data
    with np.load('Calibration parameters.npz') as X:
        camera_matrix, dist_coeffs, rvecs, tvecs = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]

    # 终止条件(termination criteria)
    criteria = (lmc_cv.TERM_CRITERIA_EPS + lmc_cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((6 * 7, 3), np.float32)
    objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)

    # draw a cube or a three-dimensional coordinate axis
    axis = []
    if 0 == method:
        axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3)

    if 1 == method:
        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]])

    # 读取所有匹配的图像
    stacking_images = []
    corners_image = []
    gray_image = []
    images = glob.glob('D:/99-Research/TestData/cv/stereo/case1/left*.png')
    for image_name in images:
        image = lmc_cv.imread(image_name, lmc_cv.IMREAD_UNCHANGED)
        image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
        corners_image = image.copy()
        gray_image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)

        # Find the chess board corners
        ret, corners = lmc_cv.findChessboardCorners(gray_image, (7, 6), None)

        # If found, add object points, image points (after refining them)
        if ret:
            # 亚像素角点
            corners2 = lmc_cv.cornerSubPix(gray_image, corners, (11, 11), (-1, -1), criteria)

            # Find the rotation and translation vectors.
            ret, rvecs, tvecs = lmc_cv.solvePnP(objp, corners2, camera_matrix, dist_coeffs)

            # project 3D points to image plane
            imgpts, jac = lmc_cv.projectPoints(axis, rvecs, tvecs, camera_matrix, dist_coeffs)

            # 绘制三维坐标轴
            corners_image = lmc_cv_draw_3d_coordinate_axis(method, corners_image, corners2, imgpts)

            # stacking images side-by-side
            stacking_image = np.hstack((image, corners_image))
            stacking_images.append(stacking_image)

    # 显示图像
    for i in range(len(stacking_images)):
        pyplot.figure('Pose Estimation %d' % (i + 1), figsize=(16, 9))
        # pyplot.subplot(1, 1, 1)
        pyplot.imshow(stacking_images[i], 'gray')
        pyplot.title('Pose Estimation')
        pyplot.xticks([])
        pyplot.yticks([])
        pyplot.savefig('%02d.png' % (i + 1))
    pyplot.show()
####################################################################################################
# 绘制3D轴
def lmc_cv_draw_3d_coordinate_axis(method, image, corners, imgpts):
    if 0 == method:
        corners = np.int32(corners)
        imgpts = np.int32(imgpts)
        corner = tuple(corners[0].ravel())
        image = lmc_cv.line(image, corner, tuple(imgpts[0].ravel()), (255, 0, 0), 5)
        image = lmc_cv.line(image, corner, tuple(imgpts[1].ravel()), (0, 255, 0), 5)
        image = lmc_cv.line(image, corner, tuple(imgpts[2].ravel()), (0, 0, 255), 5)

    if 1 == method:
        imgpts = np.int32(imgpts).reshape(-1, 2)
        # draw ground floor in green
        image = lmc_cv.drawContours(image, [imgpts[:4]], -1, (0, 255, 0), -3)
        # draw pillars in blue color
        for i, j in zip(range(4), range(4, 8)):
            image = lmc_cv.line(image, tuple(imgpts[i]), tuple(imgpts[j]), 255, 3)
        # draw top layer in red color
        image = lmc_cv.drawContours(image, [imgpts[4:]], -1, (0, 0, 255), 3)

    return image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值