Camera coordinate system -> Pixel coordinate system

Code link: https://github.com/PanJinquan/python-learning-notes/blob/master/modules/utils_3d/camera_tools.py 

Reference article: h36m data set preprocessing - short book

def __cam2pixel(cam_coord, f, c):
        """
        相机坐标系 -> 像素坐标系: (f / dx) * (X / Z) = f * (X / Z) / dx
        cx,ppx=260.166; cy,ppy=205.197; fx=367.535; fy=367.535
        将从3D(X,Y,Z)映射到2D像素坐标P(u,v)计算公式为:
        u = X * fx / Z + cx
        v = Y * fy / Z + cy
        D(v,u) = Z / Alpha
        =====================================================
        camera_matrix = [[428.30114, 0.,   316.41648],
                        [   0.,    427.00564, 218.34591],
                        [   0.,      0.,    1.]])

        fx = camera_intrinsic[0, 0]
        fy = camera_intrinsic[1, 1]
        cx = camera_intrinsic[0, 2]
        cy = camera_intrinsic[1, 2]
        =====================================================
        :param cam_coord:
        :param f: [fx,fy]
        :param c: [cx,cy]
        :return:
        """
        # 等价于:(f / dx) * (X / Z) = f * (X / Z) / dx
        # 三角变换, / dx, + center_x
        u = cam_coord[..., 0] / cam_coord[..., 2] * f[0] + c[0]
        v = cam_coord[..., 1] / cam_coord[..., 2] * f[1] + c[1]
        d = cam_coord[..., 2]
        return u, v, d

    @staticmethod
    def convert_cc_to_ic(joint_cam):
        """
        相机坐标系 -> 像素坐标系
        :param joint_cam:
        :return:
        """
        # 相机坐标系 -> 像素坐标系,并 get relative depth
        # Subtract center depth
        # 选择 Pelvis骨盆 所在位置作为相机中心,后面用之求relative depth
        root_idx = 0
        center_cam = joint_cam[root_idx]  # (x,y,z) mm
        joint_num = len(joint_cam)
        f = camera_intrinsic["f"]
        c = camera_intrinsic["c"]
        # joint image_dict,像素坐标系,Depth 为相对深度 mm
        joint_img = np.zeros((joint_num, 3))
        joint_img[:, 0], joint_img[:, 1], joint_img[:, 2] = CameraTools.__cam2pixel(joint_cam, f, c)  # x,y
        joint_img[:, 2] = joint_img[:, 2] - center_cam[2]  # z
        return joint_img

The __cam2pixel function does not process the value of cam_coord[..., 2] in the camera coordinate system and directly returns it as the depth value. This is because this function is only used to convert points in the camera coordinate system to points in the pixel coordinate system, and does not require any transformation of the depth value. The transformation of the depth value is performed in the convert_cc_to_ic function. This function will subtract the depth value of the root node (Pelvis pelvis) from the depth value of all points to obtain the relative depth value. The purpose of this is to eliminate the influence of the camera position and attitude on the depth value, and only retain the relative distance between points. 

Guess you like

Origin blog.csdn.net/u010087338/article/details/132844871