You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/user_guide/17_Morphological_Operators.ipynb
+21-38Lines changed: 21 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,12 @@
3
3
{
4
4
"cell_type": "markdown",
5
5
"metadata": {},
6
-
"source": [
7
-
"# Xarray-Spatial Morphology: Erosion, dilation, opening, and closing\n",
8
-
"\n",
9
-
"Morphological operators filter rasters by sliding a structuring element (kernel) across the surface and picking the local minimum or maximum at each cell. They show up everywhere from cleaning noisy classification masks to smoothing elevation surfaces before further analysis. This notebook walks through the four operations in `xrspatial.morphology` on both continuous terrain and binary masks."
10
-
]
6
+
"source": "# Xarray-Spatial Morphology\n\nMorphological operators filter rasters by sliding a structuring element (kernel) across the surface and picking the local minimum or maximum at each cell. They show up everywhere from cleaning noisy classification masks to smoothing elevation surfaces before further analysis. This notebook walks through the seven operations in `xrspatial.morphology` on both continuous terrain and binary masks."
11
7
},
12
8
{
13
9
"cell_type": "markdown",
14
10
"metadata": {},
15
-
"source": [
16
-
"### What you'll build\n",
17
-
"\n",
18
-
"1. Generate synthetic terrain and a hillshade base layer\n",
19
-
"2. Apply erosion and dilation to see how local min/max reshape the surface\n",
20
-
"3. Use opening and closing to selectively remove noise\n",
21
-
"4. Clean up a noisy binary classification mask\n",
22
-
"5. Compare square and circular structuring elements\n",
"[Erosion and dilation](#Erosion-and-dilation) · [Opening and closing](#Opening-and-closing) · [Binary mask cleanup](#Binary-mask-cleanup) · [Circular structuring element](#Circular-structuring-element)"
27
-
]
11
+
"source": "### What you'll build\n\n1. Generate synthetic terrain and a hillshade base layer\n2. Apply erosion and dilation to see how local min/max reshape the surface\n3. Use opening and closing to selectively remove noise\n4. Clean up a noisy binary classification mask\n5. Compare square and circular structuring elements\n6. Use gradient, white top-hat, and black top-hat to extract features\n\n\n\n[Erosion and dilation](#Erosion-and-dilation) · [Opening and closing](#Opening-and-closing) · [Binary mask cleanup](#Binary-mask-cleanup) · [Circular structuring element](#Circular-structuring-element) · [Gradient, white top-hat, black top-hat](#Gradient,-white-top-hat,-and-black-top-hat)"
28
12
},
29
13
{
30
14
"cell_type": "markdown",
@@ -45,25 +29,7 @@
45
29
}
46
30
},
47
31
"outputs": [],
48
-
"source": [
49
-
"%matplotlib inline\n",
50
-
"import numpy as np\n",
51
-
"import pandas as pd\n",
52
-
"import xarray as xr\n",
53
-
"\n",
54
-
"import matplotlib.pyplot as plt\n",
55
-
"from matplotlib.colors import ListedColormap\n",
56
-
"from matplotlib.patches import Patch\n",
57
-
"\n",
58
-
"import xrspatial\n",
59
-
"from xrspatial.morphology import (\n",
60
-
" _circle_kernel,\n",
61
-
" morph_closing,\n",
62
-
" morph_dilate,\n",
63
-
" morph_erode,\n",
64
-
" morph_opening,\n",
65
-
")"
66
-
]
32
+
"source": "%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport xarray as xr\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import ListedColormap\nfrom matplotlib.patches import Patch\n\nimport xrspatial\nfrom xrspatial.morphology import (\n _circle_kernel,\n morph_black_tophat,\n morph_closing,\n morph_dilate,\n morph_erode,\n morph_gradient,\n morph_opening,\n morph_white_tophat,\n)"
67
33
},
68
34
{
69
35
"cell_type": "markdown",
@@ -488,6 +454,23 @@
488
454
"The square kernel reaches farther into the diagonal corners than the circular one, so it produces a slightly deeper erosion along ridges that run diagonally. The difference map and cross-section show that the disagreement concentrates on steep slopes where the extra corner pixels pull the minimum lower."
489
455
]
490
456
},
457
+
{
458
+
"cell_type": "markdown",
459
+
"source": "The gradient lights up wherever the terrain has steep transitions, making it a useful edge detector. The white top-hat pulls out peaks and ridges that are narrower than the 15x15 kernel, while the black top-hat does the same for small valleys and depressions. The bottom-right panel (white minus black) is positive on peaks and negative in valleys, giving a signed feature-size map.\n\nAll three results are non-negative by construction. The gradient equals the sum of the two top-hats (dilate - erode = (orig - opening) + (closing - orig) when opening <= orig <= closing), which you can verify in the cross-section.",
"source": "## Gradient, white top-hat, and black top-hat\n\nThese three operations are derived from the primitives above:\n\n| Operation | Formula | What it extracts |\n|-----------|---------|-----------------|\n| **Gradient** | dilate - erode | Edges and transitions |\n| **White top-hat** | original - opening | Bright features smaller than the kernel |\n| **Black top-hat** | closing - original | Dark features smaller than the kernel |\n\nEach is a single subtraction of two morphological results, so all four backends are supported automatically.",
0 commit comments