opencv - 无法在 Open3D 中渲染对象的特定 View

标签 opencv graphics 3d computer-vision open3d

我想在 Open3D/OpenGL 中渲染 3D 对象旋转特定角度后的前 View 。我应该在下面的代码中给出哪些参数

import numpy as np
import open3d as o3d
import cv2
import copy
mesh_sphere = o3d.geometry.TriangleMesh.create_sphere(radius=1.0)
R = mesh.get_rotation_matrix_from_xyz((np.pi / 2, 0, np.pi / 4))
mesh_r = copy.deepcopy(mesh)
mesh_r.rotate(R, center=(0, 0, 0))
render.scene.set_background([0, 0, 0, 0])
render.scene.add_geometry("model", mesh , mtl)
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]) # what parameters should I give here ?
img_o3d = render.render_to_image()
img = np.array(img_o3d)
  
cv2.imshow("foot_model", img)
cv2.waitKey()

我得到的输出是对象的顶 View ,因为我需要对象的前 View (前面是我能够看到的桌面 View )。请帮我解决这个问题,如果通过 Open3D 无法解决这个问题,也欢迎链接到 OpenGL 中解决的类似问题。

最佳答案

Open3D 使用以下(右手)轴约定:

  • +X 指向右侧
  • +Y 向上
  • +Z 指向屏幕外

在您的示例中,相机位于 Y=10 处,位于目标上方,向下看。要从正面观察屏幕,只需切换 Y 和 Z 值即可。

因此 look_at([0, 0, 0], [0, 10, 0], [0, 0, 1]) 变为 look_at([0, 0, 0] , [0, 0, 10], [0, 1, 0]).

这是一个使用 Open3D 0.12.0 的带注释的工作示例:

# To install dependencies: pip install numpy, open3d, opencv-python
import numpy as np
import open3d as o3d
import cv2
import copy

# Create a renderer with the desired image size
img_width = 640
img_height = 480
render = o3d.visualization.rendering.OffscreenRenderer(img_width, img_height)

# Pick a background colour (default is light gray)
render.scene.set_background([0.1, 0.2, 0.3, 1.0])  # RGBA

# Create the mesh geometry.
# (We use arrows instead of a sphere, to show the rotation more clearly.)
mesh = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0, origin=np.array([0.0, 0.0, 0.0]))

# Create a copy of the above mesh and rotate it around the origin.
# (If the original mesh is not going to be used, we can just rotate it directly without making a copy.)
R = mesh.get_rotation_matrix_from_xyz((np.pi / 2, 0, np.pi / 4))
mesh_r = copy.deepcopy(mesh)
mesh_r.rotate(R, center=(0, 0, 0))

# Show the original coordinate axes for comparison.
# X is red, Y is green and Z is blue.
render.scene.show_axes(True)

# Define a simple unlit Material.
# (The base color does not replace the arrows' own colors.)
mtl = o3d.visualization.rendering.Material()  # or MaterialRecord(), for later versions of Open3D
mtl.base_color = [1.0, 1.0, 1.0, 1.0]  # RGBA
mtl.shader = "defaultUnlit"

# Add the arrow mesh to the scene.
# (These are thicker than the main axis arrows, but the same length.)
render.scene.add_geometry("rotated_model", mesh_r, mtl)

# Since the arrow material is unlit, it is not necessary to change the scene lighting.
#render.scene.scene.enable_sun_light(False)
#render.scene.set_lighting(render.scene.LightingProfile.NO_SHADOWS, (0, 0, 0))

# Optionally set the camera field of view (to zoom in a bit)
vertical_field_of_view = 15.0  # between 5 and 90 degrees
aspect_ratio = img_width / img_height  # azimuth over elevation
near_plane = 0.1
far_plane = 50.0
fov_type = o3d.visualization.rendering.Camera.FovType.Vertical
render.scene.camera.set_projection(vertical_field_of_view, aspect_ratio, near_plane, far_plane, fov_type)

# Look at the origin from the front (along the -Z direction, into the screen), with Y as Up.
center = [0, 0, 0]  # look_at target
eye = [0, 0, 10]  # camera position
up = [0, 1, 0]  # camera orientation
render.scene.camera.look_at(center, eye, up)

# Read the image into a variable
img_o3d = render.render_to_image()

# Display the image in a separate window
# (Note: OpenCV expects the color in BGR format, so swop red and blue.)
img_cv2 = cv2.cvtColor(np.array(img_o3d), cv2.COLOR_RGBA2BGRA)
cv2.imshow("Preview window", img_cv2)
cv2.waitKey()

# Optionally write it to a PNG file
o3d.io.write_image("output.png", img_o3d, 9)

结果:

output.png

细线是未旋转的轴,其中 Up = +Y = 绿色,Right = +X = 红色。

关于opencv - 无法在 Open3D 中渲染对象的特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67179977/

相关文章:

android - Harism 的 Page Curl 动画没有获得 Canvas 上同一组图像的更新,而是附加到已经设置的图像

c++ - 将 Mat 转换为不同类型的自身是否安全?

c++ - OpenGL 冒名顶替者球体 : problem when calculating the depth value

c# - 如何在面板上画线?它没有出现

android - 有没有办法将 3D 模型导入 Android?

c# - 通过 3D 投影从图像中获取纹理

c++ - 在 OpenCV FOURCC 编解码器中写入视频

c++ - 如何进行逐元素比较并根据结果做不同的操作

python - 图像去模糊

algorithm - 遮挡算法集合