使用卷积窗(Python)隔离沿海网格 [英] Isolating coastal grids using convolving window (python)

查看:121
本文介绍了使用卷积窗(Python)隔离沿海网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据集中隔离沿海网格.我可以通过单独检查陆地网格的每个邻居来做到这一点,如果8个邻居之一是海洋网格,那么我将保存网格.这是我编写的函数:

I'm trying to isolate coastal grids from a dataset. I am able to do it by individually checking each neighbor of a land grid, and if one of the 8 neighbors is an ocean grid, then I'm saving the grid. Here's the function I've written:

def coastalGrids(ds):
grid=~out[0,:,:].mask #out is a 3D masked array (time,lat,lon)

#At each grid where there is land, check all its 8 neighbors, 
#and if any of them are ocean, save the grid to the dataset
coastline=np.zeros(grid.shape, dtype=bool)
for i in range(1,len(lat)-1):
    for j in range(1,len(lon)-1):
        if grid[i,j]==True:
            if (grid[i+1,j+1]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i+1,j]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i+1,j-1]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i,j+1]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i,j-1]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i-1,j+1]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i-1,j]!=True):
                coastline[i,j]= grid[i,j]
            elif (grid[i-1,j-1]!=True):
                coastline[i,j]= grid[i,j]
return coastline

我想知道是否:

  1. 使用scipy的卷积Windows函数可以做到这一点.
  2. 扩展这种功能,有没有办法将海岸线与海岸线之间的半径隔开10个网格?

谢谢!

推荐答案

使用图像形态运算符

您当前正在执行的操作等同于原始布尔数组与其二进制腐蚀.

这与形态梯度密切相关,但定义略有不同.

This is closely related to the morphological gradient, but it's a slightly different definition.

无论如何,我们有一个非常简单的岛屿:

At any rate, let's say we have a very simple island:

import numpy as np
import matplotlib.pyplot as plt

y, x = np.mgrid[-10:10:20j, -10:10:20j]
land = np.hypot(x, y) < 7

fig, ax = plt.subplots()
ax.pcolormesh(land, cmap='gray', edgecolor='gray', antialiased=True)
plt.show()

我们可以通过侵蚀岛屿来计算您定义的海岸线:

We can calculate the coastline as you've defined it by eroding the island:

import scipy.ndimage
erosion = scipy.ndimage.binary_erosion(land)

然后查看有什么区别:

coast = land != erosion

默认情况下,这使用方形"连接.换句话说,它不会将对角线视为触摸.默认结构(也称为足迹")如下所示:

By default, this uses "square" connectivity. In other words, it doesn't count diagonals as touching. The default structure (a.k.a. "footprint") looks like:

[[0, 1, 0],
 [1, 1, 1],
 [0, 1, 0]]

在您的代码中,您假设对角线接触.在这种情况下,您需要完全"的连接,并且结构看起来像:

In your code, you're assuming diagonals are touching. In that case, you want "full" connectivity, and a structure that looks like:

[[1, 1, 1],
 [1, 1, 1],
 [1, 1, 1]]

为此,我们将指定类似于以下内容的内容:

To do that, we'd specify something similar to:

erosion = scipy.ndimage.binary_erosion(land, structure=np.ones((3,3)))

作为一个完整的例子:

import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage

y, x = np.mgrid[-10:10:20j, -10:10:20j]
land = np.hypot(x, y) < 7

erosion = scipy.ndimage.binary_erosion(land, structure=np.ones((3,3)))
coast = land != erosion

fig, ax = plt.subplots()
ax.pcolormesh(coast, cmap='gray', edgecolor='gray', antialiased=True)
plt.show()

您还可以考虑使用形态梯度运算符.对于给定的输入和连接范围,这是二进制膨胀和二进制腐蚀之间的差异.

You might also consider using the morphological gradient operator. It's the difference between the binary dilation and the binary erosion for a given input and connectivity footprint.

在您的情况下,它还将包括与陆地接壤的海像素以及与海洋接界的海像素.有效地,它将为您提供更粗的边框.

In your case, it would also include the sea pixels that border land, as well as the land pixels that border sea. Effectively, it will give you thicker borders.

例如:

import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage

y, x = np.mgrid[-10:10:20j, -10:10:20j]
land = np.hypot(x, y) < 7

coast = scipy.ndimage.morphological_gradient(land,
                                            footprint=[[0, 1, 0],
                                                       [1, 1, 1],
                                                       [0, 1, 0]])

fig, ax = plt.subplots()
ax.pcolormesh(coast, cmap='gray', edgecolor='gray', antialiased=True)
plt.show()

这篇关于使用卷积窗(Python)隔离沿海网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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