简体   繁体   English

Open3D 场景渲染器 render_to_image 返回黑屏

[英]Open3D scene renderer render_to_image returns black screen

I am trying to render a scene that simply contains a mesh of obj file and the material file.我正在尝试渲染一个只包含 obj 文件和材质文件网格的场景。 It looks okay when I try to view with当我尝试查看时看起来没问题

o3d.visualization.draw([{
    "name": "Model",
    "geometry": model,
    "material": mat
}])

But when I try to render the same scene with the following code, the output image is just black.但是当我尝试使用以下代码渲染同一场景时,output 图像只是黑色。 I have tried everything but nothing seems to be working at the moment我已经尝试了一切,但目前似乎没有任何效果

import numpy as np
import open3d as o3d
import cv2

def main():
    render = o3d.visualization.rendering.OffscreenRenderer(640, 480)
    model, mat=getModel()
    render.scene.set_background([0, 0, 0, 0])
    render.scene.add_geometry("model", model, mat)
    render.scene.set_lighting(render.scene.LightingProfile.NO_SHADOWS, (0, 0, 0))
    render.scene.camera.look_at([0, 0, 0], [0, 10, 0], [0, 0, 1])
    img_o3d = render.render_to_image()
    o3d.io.write_image("mtest2.jpeg", img_o3d, 9)
    img = np.array(img_o3d)
    cv2.imshow("model", img)
    cv2.waitKey(1)

def getModel():

    model_name = "mouse.obj"
    model = o3d.io.read_triangle_mesh(model_name)
    material = o3d.visualization.rendering.MaterialRecord()
    material.shader = "defaultLit"
    albedo_name = "albedo.jpeg"
    material.albedo_img = o3d.io.read_image(albedo_name)
    return (model, material)

main()

在此处输入图像描述

I'm also certain it's because your camera is not looking at the object.我也确定这是因为你的相机没有在看 object。

First method第一种方法

In your render try changing the position. Like so在您的渲染中尝试更改 position。像这样

render.scene.camera.look_at([0, 0, 0], [0, 0, -800], [0, 1, 0])

Remember the first argument is where to look, the second argument is the position of the camera (which has to be far enough to see the object), and the third is the camera orientation.记住第一个参数是看哪里,第二个参数是相机的 position(必须足够远才能看到物体),第三个是相机方向。

The rest of your code is fine, and I've tested it to render some images.您的代码的 rest 没问题,我已经测试过它可以渲染一些图像。

Second method第二种方法

Also, big help to find the camera position, for any orientation, is using the visualizer.此外,对于在任何方向上找到相机 position 的巨大帮助,正在使用可视化工具。 Use the mouse to navigate and find the position which you want to render and then hit the button p .使用鼠标导航并找到要渲染的 position,然后点击按钮p This will save a JSON file in the same directory called something like ScreenCamera_<somedate>.json .这将在同一目录中保存一个 JSON 文件,名称类似于ScreenCamera_<somedate>.json

Example of using interactive visualizer for you为您使用交互式可视化工具的示例

render = o3d.visualization.Visualizer()
render.create_window()
model, mat = getModel()
render.add_geometry(model)
render.run()

Then if you want to reuse that pinhole camera configuration later on in your offscreen render do this.然后,如果您想稍后在屏幕外渲染中重复使用该针孔相机配置,请执行此操作。

parameters = o3d.io.read_pinhole_camera_parameters("ScreenCamera_<somedate>.json")
render.setup_camera(parameters.intrinsic, parameters.extrinsic)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM