我正在处理机器学习任务,我正在尝试使用 Blender 生成合成图像作为神经网络的训练数据集。为此,我必须在渲染图像中找到对象的边界框。
到目前为止,我的代码主要基于此线程中建议的代码,但这并没有考虑顶点是否可见或被另一个对象遮挡。想要的结果确实和这里解释的完全一样。我已经尝试了那里给出的建议,但它不起作用。我不明白是因为我给 ray_cast 函数提供了错误的输入(因为 bpy API 真的很糟糕),还是仅仅因为函数的性能很差,正如我在其他地方读到的那样。我的代码,现在,是:
import bpy
import numpy as np
def boundingbox(scene, camera, obj, limit = 0.3):
# Get the inverse transformation matrix.
matrix = camera.matrix_world.normalized().inverted()
# Create a new mesh data block, using the inverse transform matrix to undo any transformations.
dg = bpy.context.evaluated_depsgraph_get()
# eval_obj = bpy.context.object.evaluated_get(dg)
eval_obj = obj.evaluated_get(dg)
mesh = eval_obj.to_mesh()
mesh.transform(obj.matrix_world)
mesh.transform(matrix)
# Get the world coordinates for the camera frame bounding box, before any transformations. …Run Code Online (Sandbox Code Playgroud)