Python 如何在pyrender中将场景渲染为图像后获取对象的像素坐标?

Python 如何在pyrender中将场景渲染为图像后获取对象的像素坐标?,python,trimesh,pyrender,Python,Trimesh,Pyrender,我试图获得使用pyrender渲染的对象的像素坐标(x,y)。目的是获取该对象的边界框坐标。我使用OffScreenRenderer渲染场景 r = pyrender.OffscreenRenderer(viewport_width=640, viewport_height=480, point_size=1.0) color, depth = r.render(scene) 可用信

我试图获得使用pyrender渲染的对象的像素坐标(x,y)。目的是获取该对象的边界框坐标。我使用OffScreenRenderer渲染场景

r = pyrender.OffscreenRenderer(viewport_width=640,
                            viewport_height=480,
                            point_size=1.0)
color, depth = r.render(scene)
可用信息:相机到世界姿势矩阵

首先,我尝试绘制对象的质心,如下所示:

x, y = trimesh_mesh.centroid[:2]
height, width = image.shape[:2]
x = int(x * width)
y = int(y * height)
plt.imshow(image)
plt.scatter(x, y)
plt.show()
我得到以下信息:

类似地,同一对象的边界框打印为


有人知道如何获得渲染对象的精确质心和长方体坐标吗?谢谢。

您正在寻找
bpy\u extras.object\u utils.world\u to\u camera\u view

像这样使用它:

import bpy
import bpy_extras

scene = bpy.context.scene
obj = bpy.context.object   # object you want the coordinates of
vertices = obj.data.vertices   # you will get the coordinates of its vertices

for v in vertices:
    # local to global coordinates
    co = v.co @ obj.matrix_world
    # calculate 2d image coordinates
    co_2d = bpy_extras.object_utils.world_to_camera_view(scene, camera, co)
    render_scale = scene.render.resolution_percentage / 100
    render_size = (
        int(scene.render.resolution_x * render_scale),
        int(scene.render.resolution_y * render_scale),
    )

    # this is the result
    pixel_coords = (co_2d.x * render_size[0],
                    co_2d.y * render_size[1])

    print("Pixel Coords:", (
          round(pixel_coords[0]),
          round(pixel_coords[1]),
    ))

请参见搅拌机堆栈交换上的

我也有同样的问题。你现在解决了吗?如果是这样的话,请回答你自己的问题。谢谢你的回答。我并没有使用BlenderAPI,而是导入trimesh和pyrender来渲染场景中的图像。我以不同的方式解决了这个问题。我从场景中渲染了相应的二进制遮罩。使用这些遮罩,我可以很容易地计算质心。