理论基础:
焦距 f 就是胶片到小孔的距离
摄像机焦距和fov存在一一对应的关系,fov其实就是摄像机焦距的另一种更直观的表达方式
摄像机坐标系的原点就是针孔位置:
摄像机焦距就是原点到像平面的距离
通过将: ( 点的物理坐标/点的深度z )* f-[相机以像素为单位的焦距] 就可以得到点的物理坐标到像平面坐标的转换
计算方式:
根据图像长宽和fov得到图像的相机内参,从而得到3D点到图像平面的映射关系
代码实现:
从图像通过假设fov的方式得到相机内参的代码:
def generate_camera_params(image_path, fx=None, fy=None, cx=None, cy=None, fov=60): """ Generate camera parameters for 3D visualization. Args: image_path: Path to the image fx, fy: Focal lengths in pixels (if None, will be calculated from fov) cx, cy: Principal point coordinates in pixels (if None, will be set to image center) fov: Field of view in degrees (default: 60°) Returns: dict: Camera parameters with keys 'fx', 'fy', 'cx', 'cy' """ image = Image.open(image_path) w, h = image.size # Generate pseudo camera params if not provided if fx is None or fy is None: fx = round(w / (2 * np.tan(np.deg2rad(fov) / 2)), 2) fy = round(h / (2 * np.tan(np.deg2rad(fov) / 2)), 2) if cx is None or cy is None: cx = round(w / 2, 2) cy = round(h / 2, 2) cam_params = {'fx': fx, 'fy': fy, 'cx': cx, 'cy': cy} return cam_params由摄像机坐标系下的物理3D点,根据对应摄像机内参来得到对应像平面上2D像素坐标的代码:
def convert_3dbbox(point, cam_params): """ Convert 3D bounding box to 2D image coordinates We represent 3D bounding boxes as: `[x_center, y_center, z_center, x_size, y_size, z_size, roll, pitch, yaw]` - **x_center, y_center, z_center**: Object center in camera coordinates (meters) - **x_size, y_size, z_size**: Object dimensions (meters) - **roll, pitch, yaw**: Rotation angles (radians) """ x, y, z, x_size, y_size, z_size, pitch, yaw, roll = point hx, hy, hz = x_size / 2, y_size / 2, z_size / 2 local_corners = [ [ hx, hy, hz], [ hx, hy, -hz], [ hx, -hy, hz], [ hx, -hy, -hz], [-hx, hy, hz], [-hx, hy, -hz], [-hx, -hy, hz], [-hx, -hy, -hz] ] def rotate_xyz(_point, _pitch, _yaw, _roll): # 这是一个 XYZ 顺序的欧拉角旋转 x0, y0, z0 = _point x1 = x0 y1 = y0 * math.cos(_pitch) - z0 * math.sin(_pitch) z1 = y0 * math.sin(_pitch) + z0 * math.cos(_pitch) x2 = x1 * math.cos(_yaw) + z1 * math.sin(_yaw) y2 = y1 z2 = -x1 * math.sin(_yaw) + z1 * math.cos(_yaw) x3 = x2 * math.cos(_roll) - y2 * math.sin(_roll) y3 = x2 * math.sin(_roll) + y2 * math.cos(_roll) z3 = z2 return [x3, y3, z3] img_corners = [] for corner in local_corners: # 8个顶点 rotated = rotate_xyz(corner, np.deg2rad(pitch), np.deg2rad(yaw), np.deg2rad(roll)) X, Y, Z = rotated[0] + x, rotated[1] + y, rotated[2] + z if Z > 0: x_2d = cam_params['fx'] * (X / Z) + cam_params['cx'] y_2d = cam_params['fy'] * (Y / Z) + cam_params['cy'] img_corners.append([x_2d, y_2d]) return img_corners多视图情况:
多视图中的物体表示方法:
1,所有帧都以第一帧的相机坐标系为世界坐标系,直接预测物体在世界坐标系中的3D位置
2,每一帧都: 预测目标物体在当前帧的相机坐标系下的3d坐标,这样z_center就是对应的相对于当前帧的相机的深度