簡體   English   中英

在pyrender中將場景渲染為圖像后如何獲取對象的像素坐標?

[英]How to get pixel coordinates of object after rendering the scene as image in pyrender?

我正在嘗試獲取使用 pyrender 渲染的對象的像素坐標 (x, y)。 目的是獲取該對象的邊界框坐標。 我使用 OffScreenRenderer 來渲染場景。

r = pyrender.OffscreenRenderer(viewport_width=640,
                            viewport_height=480,
                            point_size=1.0)
color, depth = r.render(scene)

可用信息:camera_to_world 姿勢矩陣

首先,我嘗試繪制對象的質心,如下所示:

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_extras.object_utils.world_to_camera_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]),
    ))

在 Blender Stack Exchange 上看到這個答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM