使用Scipy.ndimage迭代遍历高程数组。

huangapple go评论88阅读模式
英文:

Using Scipy.ndimage to iterate over Elevation Array

问题

我有一个来自.tif激光雷达表面的高程数组。以下是示例数组。

Existing_example_arrayV0 = [[ 0, 0, 1, 0, 0, 0, 0], 
                            [ 0, 1, 1, 1, 0, 0, 0], 
                            [ 0, 1, 1, 1, 1, 0, 0], 
                            [ 1, 1, 1, 1, 1, 0, 0], 
                            [ 0, 1, 1, 1, 1, 0, 0], 
                            [ 0, 1, 1, 0, 0, 0, 0]]

我尝试使用scipy.ndimage来迭代该数组并将100添加到数组中。

Existing_example_arrayV0[binary_erosion(Existing_example_arrayV0 >= 1, structure=[[1,1,1]])] += 100

以生成第一次迭代。

Existing_example_arrayV1 = [[ 0,  0,   1,   0,  0, 0, 0], 
                            [ 0,  1,  100,  1,  0, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 1, 100, 100, 100, 1, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 0,  1,   1,   0,  0, 0, 0]]

我对二值侵蚀不熟悉,所以很难进行第二次迭代:

Existing_example_arrayV2 = [[ 0,  0,   1,   0,  0, 0, 0], 
                            [ 0,  1,  100,  1,  0, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 1, 100, 200, 100, 1, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 0,  1,   1,   0,  0, 0, 0]]

我尝试创建一个for循环来生成下一步,但在使其正确运行方面遇到了问题。

import scipy.ndimage as binary_erosion
for i in range(0,1):
    binary_erosion(Existing_example_arrayV1 >= 1, structure=[[1,1,1]], iterations = i + 1)

我认为range只会运行下一个迭代以生成Existing_example_arrayV2,但实际情况并非如此。我正在尝试在已完成的numpy数组上添加一个迭代(V0 -> V1,V1 -> V2等),以继续运行所需的迭代次数。

英文:

I have an elevation array from a .tif LiDAR surface. Example array below.

Existing_example_arrayV0 = [[ 0, 0, 1, 0, 0, 0, 0], 
                            [ 0, 1, 1, 1, 0, 0, 0], 
                            [ 0, 1, 1, 1, 1, 0, 0], 
                            [ 1, 1, 1, 1, 1, 0, 0], 
                            [ 0, 1, 1, 1, 1, 0, 0], 
                            [ 0, 1, 1, 0, 0, 0, 0]]

I am attempting to use scipy.ndimage to iterate over the array and add 100 to the array.

Existing_example_arrayV0[binary_erosion(Existing_example_arrayV0 >=1, structure=[[1,1,1]])] += 100

to produce the first iteration.

Existing_example_arrayV1 = [[ 0,  0,   1,   0,  0, 0, 0], 
                            [ 0,  1,  100,  1,  0, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 1, 100, 100, 100, 1, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 0,  1,   1,   0,  0, 0, 0]]

I'm not familiar with Binary Erosion so I am having a difficult time iterating to create the second iteration:

Existing_example_arrayV2 = [[ 0,  0,   1,   0,  0, 0, 0], 
                            [ 0,  1,  100,  1,  0, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 1, 100, 200, 100, 1, 0, 0], 
                            [ 0,  1,  100, 100, 1, 0, 0], 
                            [ 0,  1,   1,   0,  0, 0, 0]]

I attempted to create a for loop that would create the next step but am having issues getting it to run correctly.

import scipy.ndimage as binary_erosion
for i in range(0,1):
    binary_erosion(Existing_example_arrayV1>=1, structure=[[1,1,1]], iterations = i + 1)

I thought the range would just run the next iteration to produce Existing_example_arrayV2, but it is not.
I am trying to add an iteration on top of the finished numpy array (V0 -> V1, V1 -> V2 etc.) to continue the cycle for however many iterations I need to run.

答案1

得分: 1

你在循环中没有使用二值侵蚀的结果来修改你的原始数组。我们希望使用侵蚀后的图像来将100添加到原始数组的某些元素。

以下是你的循环的改进版本:

import numpy as np
from scipy.ndimage import binary_erosion

Existing_example_arrayV0 = np.array([[ 0, 0, 1, 0, 0, 0, 0], 
                                     [ 0, 1, 1, 1, 0, 0, 0], 
                                     [ 0, 1, 1, 1, 1, 0, 0], 
                                     [ 1, 1, 1, 1, 1, 0, 0], 
                                     [ 0, 1, 1, 1, 1, 0, 0], 
                                     [ 0, 1, 1, 0, 0, 0, 0]])

for i in range(1, 3):  # 根据需要更改迭代次数
    erosion_mask = binary_erosion(Existing_example_arrayV0 >= 100, structure=np.ones((3, 3)), iterations=i)
    Existing_example_arrayV0[erosion_mask] += 100

这是你的代码的改进版本。

英文:

You have not used the result of binary erosion to modify your original array in the loop. We want to use the eroded image to add 100 to certain elements in your original array.

Here is a better version of your loop:

import numpy as np
from scipy.ndimage import binary_erosion

Existing_example_arrayV0 = np.array([[ 0, 0, 1, 0, 0, 0, 0], 
                                     [ 0, 1, 1, 1, 0, 0, 0], 
                                     [ 0, 1, 1, 1, 1, 0, 0], 
                                     [ 1, 1, 1, 1, 1, 0, 0], 
                                     [ 0, 1, 1, 1, 1, 0, 0], 
                                     [ 0, 1, 1, 0, 0, 0, 0]])

for i in range(1, 3):  # Change the number of iterations as needed
    erosion_mask = binary_erosion(Existing_example_arrayV0 >= 100, structure=np.ones((3, 3)), iterations=i)
    Existing_example_arrayV0[erosion_mask] += 100

答案2

得分: 1

对我来说这段代码做了你描述的事情
```python
example_V2 = example_V0.copy()
for i in range(1, 3):
    mask = binary_erosion(example_V0, iterations=i)
    example_V2[mask] += 100

每次迭代,它会给下一层添加100。

但你的示例步骤似乎不是要添加100,而是在第一步添加99。在这种情况下,你可以在最后添加一行:

example_V2[binary_erosion(example_V2)] -= 1

如果你想要创建一个完整的函数来生成第n次迭代:

def elevation_array(flat_array, n):
    out = flat_array.copy()

    for i in range(1, n+1):
        mask = binary_erosion(flat_array, iterations=i)
        out[mask] += 100

    if n > 0:
        out[binary_erosion(out)] -= 1

    return out

<details>
<summary>英文:</summary>

For me, this code does what you described:

example_V2 = example_V0.copy()
for i in range(1, 3):
mask = binary_erosion(example_V0, iterations=i)
example_V2[mask] += 100

Each iteration, it adds 100 to the next layer.

But your example steps seem that you don&#39;t want to add 100, but rather 99 for the first step. In this case, you can add a line at the end:

example_V2[binary_erosion(example_V2)] -= 1


If you wanted to do a full function which produces the `n`th iteration:

def elevation_array(flat_array, n):
out = flat_array.copy()

for i in range(1, n+1):
    mask = binary_erosion(flat_array, iterations=i)
    out[mask] += 100

if n &gt; 0:
    out[binary_erosion(out)] -= 1

return out

</details>



huangapple
  • 本文由 发表于 2023年6月13日 04:44:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定