首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从深度图计算视野

从深度图计算视野
EN

Stack Overflow用户
提问于 2020-09-29 16:58:59
回答 1查看 101关注 0票数 0

我有一个深度数字n*n数组。其中每个单元格的值表示该单元格的高度。我想计算一下视场。换句话说:

代码语言:javascript
复制
if Pixel_height > viewer_height:
    pixels_behind = -1

有什么简单的方法可以做到这一点吗?

编辑:为了清楚,我想创建一个位置矩阵,类似于这个绿色蒙版,知道我们有一个高矩阵n*m enter image description here

EN

回答 1

Stack Overflow用户

发布于 2020-09-30 19:59:49

我想出了这个函数,它并不是很完美,但它的功能与您所说的类似。它有一个resolution参数,表示要考虑的圆周内有多少个“角度步长”。我不确定这是不是解决这个问题的“正确”方法,我也觉得应该有一种更聪明的方法来做一些操作,但不管怎样。

代码语言:javascript
复制
import numpy as np

def view_field(height_map, viewer_height, viewer_pos, resolution=360):
    height_map = np.asarray(height_map)
    h, w = height_map.shape
    vi, vj = viewer_pos
    # Find locations higher than viewer
    m = height_map > viewer_height
    # Find angle of each pixel relative to viewer
    ii, jj = np.ogrid[-vi:h - vi, -vj:w - vj]
    a = np.arctan2(ii, jj)
    # Distance of each pixel to viewer
    d2 = np.square(ii) + np.square(jj)
    d = np.sqrt(d2)
    # Find angle range "behind" each pixel
    pix_size = 0.5
    ad = np.arccos(d / np.sqrt(d2 + np.square(pix_size)))
    # Minimum and maximum angle encompassed by each pixel
    amin = a - ad
    amax = a + ad
    # Define angle "bins"
    ar = np.linspace(-np.pi, np.pi, resolution + 1)
    # Find the bin corresponding to each pixel
    b = np.digitize(a, ar) % resolution
    bmin = np.digitize(amin, ar) % resolution
    bmax = np.digitize(amax, ar) % resolution
    # Find the closest distance to a high pixel for each angle bin
    angdist = np.full_like(ar, np.inf)
    np.minimum.at(angdist, bmin[m], d[m])
    np.minimum.at(angdist, bmax[m], d[m])
    # Visibility is true if the pixel distance is less than the
    # visibility distance for its angle bin
    return d <= angdist[b]

下面是它的运行方式:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# A wavy function for testing
def ackley(x, y):
    return (-20 * np.exp(-0.2 * np.sqrt(0.5 * (np.square(x) + np.square(y))))
            - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
            + np.e + 20)

# Make input data
x, y = np.ogrid[-4:4:100j, -4:4:100j]
height_map = ackley(x, y)
viewer_height = 7.5
# Viewer coordinates are in "pixel space"
viewer_pos = (50, 70)
# Compute visibility
f = view_field(height_map, viewer_height, viewer_pos)
# Plot height map and result
plt.figure(figsize=(8, 4))
ax = plt.subplot(121, projection='3d')
ax.plot_surface(np.arange(100)[:, np.newaxis], np.arange(100),
                height_map, color='b', alpha=0.6)
ax.scatter3D(*viewer_pos, viewer_height, s=5, c='r', label='Viewer')
plt.title('Height map')
plt.legend()
plt.subplot(122)
plt.imshow(f)
plt.title('Visibility map')
plt.tight_layout()

以及由此产生的图:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64116207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档