BlenderProc学习记录(续三)- Camera Random Trajectories

二、高阶案例学习

Camera Random Trajectories

在这个示例中,展示了如何使用随机游走抽样器来生成摄像机轨迹。主要步骤如下:

  • 先确定一个兴趣点poi(相机“看”的目标)。

  • 给poi叠加一个小幅随机漂移 poi_drift[ i ]目标在抖动/移动)。

  • 相机沿着四分之一圆的轨迹移动(位置 location_cam 随帧 i 变化)。

  • 让相机朝向 poi + poi_drift[ i ];再在此基础上叠加一个小幅角度抖动(axis-angle 随帧随机),形成每帧的最终外参 cam2world 并加入渲染队列。

  • 降低采样数以便快速渲(set_max_amount_of_samples(50))。

全部代码如下:

import blenderproc as bproc
import numpy as np
import argparse
import mathutils

from blenderproc.python.utility.SetupUtility import SetupUtility

parser = argparse.ArgumentParser()
parser.add_argument('scene', nargs='?', default="advanced/resources/scene.obj", help="Path to the scene.obj file")
parser.add_argument('output_dir', nargs='?', default="advanced/camera_random_trajectories/output", help="Path to where the final files, will be saved")
args = parser.parse_args()

bproc.init()

# load the objects into the scene
objs = bproc.loader.load_obj(args.scene)

# define a light and set its location and energy level
light = bproc.types.Light()
light.set_type("POINT")
light.set_location([5, -5, 5])
light.set_energy(1000)

#通常计算传入对象集合的兴趣点(常为几何中心/质心)
poi = np.array(bproc.object.compute_poi(objs), dtype=np.float32)

#为 POI 生成一个“三维随机游走”的位移序列(逐帧的位置漂移)
poi_drift = bproc.sampler.random_walk(
    total_length = 25,     # 共生成 25 帧/步
    dims = 3,              # 3 维(x,y,z 都漂移)
    step_magnitude = 0.005,# 单步步长(幅度因子)
    window_size = 5,       # 滑动窗口平滑(越大越平滑)
    interval = [-0.03, 0.03],      # 限制漂移分量的取值范围(夹在这个区间)
    distribution = 'uniform'       # 采样分布:均匀
)

# 为“角度”做 1 维随机游走,作为相机抖动的角度大小(弧度)
camera_shaking_rot_angle = bproc.sampler.random_walk(
    total_length = 25,
    dims = 1,
    step_magnitude = np.pi/32,   # 单步角度变化上限(约 5.6°)
    window_size = 5,
    interval = [-np.pi/6, np.pi/6], # 角度被限制在 ±30° 内
    distribution = 'uniform',
    order = 2                    # 高阶滤波/平滑阶数(2 表示更平滑的变化)
)
# 为“旋转轴”做 3 维随机游走,然后逐帧单位化(变成单位向量)
camera_shaking_rot_axis = bproc.sampler.random_walk(
    total_length = 25, dims = 3,
    window_size = 10, distribution = 'normal'
)
camera_shaking_rot_axis /= np.clip(np.linalg.norm(camera_shaking_rot_axis, axis=1, keepdims=True), 1e-8, None)
# 对每一帧得到一个 3D 轴向量,然后归一化成单位轴。分布用 normal(高斯),window_size=10 让轴缓慢变化,避免每帧乱跳

for i in range(25):
    # 定义相机的轨迹:x,y 绕原点旋转,z 高度不变
    location_cam = np.array([
        10*np.cos(i/25 * np.pi), # x: 半径 10 的圆
        10*np.sin(i/25 * np.pi), # y
        8                        # z 固定在 8
    ]) 
    
    # 让相机“看向”目标(兴趣点 + 漂移),求一个“看向旋转矩阵”
    rotation_matrix = bproc.camera.rotation_from_forward_vec(poi + poi_drift[i] - location_cam)
    # 把“轴-角”变成旋转矩阵:构造一个小抖动旋转 R_rand
    R_rand = np.array(
        mathutils.Matrix.Rotation(
            camera_shaking_rot_angle[i],  # 本帧抖动角(标量,弧度)
            3,                             # 3x3 旋转
            camera_shaking_rot_axis[i]     # 本帧抖动轴(单位向量)
        )
    )
    # 把抖动叠加到相机朝向上
    rotation_matrix = R_rand @ rotation_matrix
    # 把位置 + 旋转组成 cam->world 齐次外参,并加入相机轨迹
    cam2world_matrix = bproc.math.build_transformation_mat(location_cam, rotation_matrix)
    bproc.camera.add_camera_pose(cam2world_matrix)

# 降低每像素采样数,快速渲染(噪点会多,但出图快)
bproc.renderer.set_max_amount_of_samples(50)

# render the whole pipeline
data = bproc.renderer.render()

# write the data to a .hdf5 container
bproc.writer.write_hdf5(args.output_dir, data)

# write the animations into .gif files
bproc.writer.write_gif_animation(args.output_dir, data, frame_duration_in_ms=80)

结果如下:

补充说明:

random_walk 通用参数

  • total_length:生成多少帧(要和你的循环帧数一致)。
  • dims:维度(角度取 1,轴/位移取 3)。
  • step_magnitude:单步变化幅度。
  • interval:把数值钳制在某区间(防止漂太远/角度太大)。
  • window_size:时间平滑窗口,越大越平滑。
  • distribution:‘uniform’ 或 ‘normal’。
  • order:高阶平滑(对角度很有用)。

rotation_from_forward_vec(target - location)

  • 计算相机“look-at”朝向。你只要提供从相机到目标的向量即可。
  • 若想固定地平线(不roll),有的版本支持参数 inplane_rot=0;若无,可后处理约束滚转。

乘法顺序

  • R_rand @ R_look:更像“世界系扰动”。
  • R_look @ R_rand:更像“相机手抖”(局部扰动)。
  • 二者皆可,按你想要的视觉效果选。
在gym - pybullet - drones 2.0.0中,trajectory_cpp项目找不到`setup.py`和`pyproject.toml`文件的错误,可尝试以下方法解决。 ### 检查文件是否存在 首先要确认`setup.py`和`pyproject.toml`文件是否确实存在于trajectory_cpp项目目录中。可以使用以下命令查看目录内容: ```bash ls path/to/trajectory_cpp ``` 若文件不存在,可能是项目文件不完整,需要重新下载或克隆gym - pybullet - drones 2.0.0项目。 ### 检查项目路径 确保当前工作目录是trajectory_cpp项目的正确路径。可以使用以下命令切换到项目目录: ```bash cd path/to/trajectory_cpp ``` ### 重新克隆项目 如果文件确实缺失,从官方仓库重新克隆gym - pybullet - drones 2.0.0项目: ```bash git clone -b v2.0.0 https://github.com/utiasDSL/gym-pybullet-drones.git cd gym-pybullet-drones/trajectory_cpp ``` ### 手动创建文件 若重新克隆后文件仍缺失,可以手动创建`setup.py`和`pyproject.toml`文件。以下是简单示例: #### setup.py ```python from setuptools import setup, find_packages setup( name='trajectory_cpp', version='0.1', packages=find_packages(), ) ``` #### pyproject.toml ```toml [build-system] requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" ``` ### 检查环境变量 确保环境变量没有影响项目文件的查找。有时候,环境变量可能会指向错误的路径。可以使用以下命令查看环境变量: ```bash echo $PYTHONPATH ``` 如果发现环境变量设置有误,可以使用以下命令修改: ```bash export PYTHONPATH=path/to/correct/directory ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值