在python中的3D numpy数组中绘制三角形 [英] Draw triangle in 3D numpy array in python

查看:27
本文介绍了在python中的3D numpy数组中绘制三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有某种形状的 3D 阵列(矩形长方体),例如 (48, 32, 64).我在这个立方体中有 3 个点,有一些坐标.x1 = (10, 20, 30)x2 = (21, 15, 34)x3 = (33, 1, 62)

我需要在受此点限制的 3D 阵列中绘制填充平面,例如在 3D 数组中绘制三角形.在 2D 情况下,我们可以使用 openCV 来实现:opencv中的三角形填充

将 numpy 导入为 npa = np.zeros((48, 32, 64), dtype=np.uint8)x1 = (10, 20, 30)x2 = (21, 15, 34)x3 = (33, 1, 62)a = draw_3D_triangle(a, x1, x2, x3)

但是在 3D 情况下最简单的方法是什么?

解决方案

(已编辑:我之前忘记包含 full_triangle() 的代码).<小时>

假设您有一个绘制线条的算法,例如 Bresenham 算法 并假设它可以推广到N-dim情况.

幸运的是 raster-geometry 包有这样一个Bresenham算法的N-dim实现.

(免责声明:我是该软件包的主要作者.)

设 A、B、C 为三角形 ABC 的顶点坐标.

<小时>

如果您只需要绘制外部形状,则可以使用使用点的各种组合来形成线的算法:AB、BC、CA.

在代码中,这将是:

将 numpy 导入为 np将 raster_geometry 导入为 rga, b, c = (1, 1), (3, 7), (6, 4)coords = set(rg.bresenham_lines((a, b, c), closed=True))打印(坐标)# {(1, 2), (6, 4), (3, 2), (4, 6), (5, 5), (2, 2), (2, 3), (3, 6),(2, 4), (4, 3), (3, 7), (2, 5), (1, 1), (5, 3)}arr = rg.render_at((10, 10), 坐标)打印(arr.astype(int))# [[0 0 0 0 0 0 0 0 0 0]# [0 1 1 0 0 0 0 0 0 0]# [0 0 1 1 1 1 0 0 0 0]# [0 0 1 0 0 0 1 1 0 0]# [0 0 0 1 0 0 1 0 0 0]# [0 0 0 1 0 1 0 0 0 0]# [0 0 0 0 1 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]]

如果您需要绘制一个完整的三角形,您可以执行以下操作:

  • 从A点到B点画一条线
  • 为线中的每个点画一条从 C 到 AB 线中一点的线.

虽然这可能不是最有效的方法,但它会运行得相当好.可能会遗漏顶点附近的某些点.在这种情况下,重复相同的过程循环遍历所有三个顶点就足够了.

在代码中,这可以是:

将 numpy 导入为 np将 raster_geometry 导入为 rgdef full_triangle(a, b, c):ab = rg.bresenham_line(a, b, 端点=真)对于 set(ab) 中的 x:从 rg.bresenham_line(c, x, endpoint=True) 产生a, b, c = (1, 1), (3, 7), (6, 4)coords = set(full_triangle(a, b, c))打印(坐标)# {(1, 2), (6, 4), (5, 4), (3, 2), (3, 3), (5, 5), (4, 6), (4, 5),(4, 4), (1, 1), (2, 3), (4, 3), (2, 2), (3, 6), (3, 7), (2, 5), (5), 3), (3, 4), (2, 4), (3, 5)}arr = rg.render_at((10, 10), 坐标)打印(arr.astype(int))# [[0 0 0 0 0 0 0 0 0 0]# [0 1 1 0 0 0 0 0 0 0]# [0 0 1 1 1 1 0 0 0 0]# [0 0 1 1 1 1 1 1 0 0]# [0 0 0 1 1 1 1 0 0 0]# [0 0 0 1 1 1 0 0 0 0]# [0 0 0 0 1 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]# [0 0 0 0 0 0 0 0 0 0]]

<小时>

请注意,虽然示例是二维的,但它们适用于 N-dim.例如,您可以使用以下命令生成 3D 三角形:

x1 = (10, 20, 30)x2 = (21, 15, 34)x3 = (33, 1, 62)coords = set(full_triangle(x1, x2, x3))arr = rg.render_at((48, 32, 64), 坐标)

Let's say I have a 3D-array (rectangular cuboid) with some shape, for example (48, 32, 64). I have 3 points in this cube with some coordinates. x1 = (10, 20, 30) x2 = (21, 15, 34) x3 = (33, 1, 62)

I need to draw filled plane in this 3D-array limited by this points, e.g. draw triangle in 3D array. In 2D case we can do it with openCV: Triangle Filling in opencv

import numpy as np
a = np.zeros((48, 32, 64), dtype=np.uint8)
x1 = (10, 20, 30)
x2 = (21, 15, 34)
x3 = (33, 1, 62)
a = draw_3D_triangle(a, x1, x2, x3)

But what is the easiest way to do so in 3D case?

解决方案

(EDITED: I had previously forgot to include the code for full_triangle()).


Assume you have an algorithm that draws lines, like Bresenham's Algorithm and assume it can be generalized to the N-dim case.

Luckily the raster-geometry package has such an N-dim implementation of the Bresenham algorithm.

(Disclaimer: I am the main author of the package.)

Let A, B, C be the coordinate of the vertices of the triangle ABC.


If you need to draw only the outer shape, you can just use the algorithm using the various combinations of the points to form the lines: AB, BC, CA.

In code, that would simply be:

import numpy as np
import raster_geometry as rg


a, b, c = (1, 1), (3, 7), (6, 4)
coords = set(rg.bresenham_lines((a, b, c), closed=True))
print(coords)
# {(1, 2), (6, 4), (3, 2), (4, 6), (5, 5), (2, 2), (2, 3), (3, 6), (2, 4), (4, 3), (3, 7), (2, 5), (1, 1), (5, 3)}
arr = rg.render_at((10, 10), coords)
print(arr.astype(int))
# [[0 0 0 0 0 0 0 0 0 0]
#  [0 1 1 0 0 0 0 0 0 0]
#  [0 0 1 1 1 1 0 0 0 0]
#  [0 0 1 0 0 0 1 1 0 0]
#  [0 0 0 1 0 0 1 0 0 0]
#  [0 0 0 1 0 1 0 0 0 0]
#  [0 0 0 0 1 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]]

If you need to draw a full triangle, you can do the following:

  • draw a line from point A to point B
  • draw a line from C to a point in the AB line, for every point in the line.

Although this may not be the most efficient approach, it will work reasonably well. It is possible that some points near the vertices may be missed. In that case, it is sufficient to repeat the same procedure looping through all three vertices.

In code, this could read:

import numpy as np
import raster_geometry as rg


def full_triangle(a, b, c):
    ab = rg.bresenham_line(a, b, endpoint=True)
    for x in set(ab):
        yield from rg.bresenham_line(c, x, endpoint=True)


a, b, c = (1, 1), (3, 7), (6, 4)
coords = set(full_triangle(a, b, c))
print(coords)
# {(1, 2), (6, 4), (5, 4), (3, 2), (3, 3), (5, 5), (4, 6), (4, 5), (4, 4), (1, 1), (2, 3), (4, 3), (2, 2), (3, 6), (3, 7), (2, 5), (5, 3), (3, 4), (2, 4), (3, 5)}
arr = rg.render_at((10, 10), coords)
print(arr.astype(int))
# [[0 0 0 0 0 0 0 0 0 0]
#  [0 1 1 0 0 0 0 0 0 0]
#  [0 0 1 1 1 1 0 0 0 0]
#  [0 0 1 1 1 1 1 1 0 0]
#  [0 0 0 1 1 1 1 0 0 0]
#  [0 0 0 1 1 1 0 0 0 0]
#  [0 0 0 0 1 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0]]


Note that while the examples are in 2D, they work for N-dim. For example, the 3D triangle you are after can be generated with:

x1 = (10, 20, 30)
x2 = (21, 15, 34)
x3 = (33, 1, 62)
coords = set(full_triangle(x1, x2, x3))
arr = rg.render_at((48, 32, 64), coords)

这篇关于在python中的3D numpy数组中绘制三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆