Skip to content

Commit 9b658e5

Browse files
committed
Add gradient/top-hat sections to morphology user guide notebook (#1025)
1 parent 77d78a4 commit 9b658e5

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

examples/user_guide/17_Morphological_Operators.ipynb

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,12 @@
33
{
44
"cell_type": "markdown",
55
"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."
117
},
128
{
139
"cell_type": "markdown",
1410
"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",
23-
"\n",
24-
"![Morphological operators preview](images/morphological_operators_preview.png)\n",
25-
"\n",
26-
"[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![Morphological operators preview](images/morphological_operators_preview.png)\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)"
2812
},
2913
{
3014
"cell_type": "markdown",
@@ -45,25 +29,7 @@
4529
}
4630
},
4731
"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)"
6733
},
6834
{
6935
"cell_type": "markdown",
@@ -488,6 +454,23 @@
488454
"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."
489455
]
490456
},
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.",
460+
"metadata": {}
461+
},
462+
{
463+
"cell_type": "code",
464+
"source": "### References\n\n- [Erosion (morphology)](https://en.wikipedia.org/wiki/Erosion_(morphology)), Wikipedia\n- [Dilation (morphology)](https://en.wikipedia.org/wiki/Dilation_(morphology)), Wikipedia\n- [Opening (morphology)](https://en.wikipedia.org/wiki/Opening_(morphology)), Wikipedia\n- [Closing (morphology)](https://en.wikipedia.org/wiki/Closing_(morphology)), Wikipedia\n- [Morphological gradient](https://en.wikipedia.org/wiki/Morphological_gradient), Wikipedia\n- [Top-hat transform](https://en.wikipedia.org/wiki/Top-hat_transform), Wikipedia\n- [Mathematical morphology](https://en.wikipedia.org/wiki/Mathematical_morphology), Wikipedia\n- [Structuring element](https://en.wikipedia.org/wiki/Structuring_element), Wikipedia",
465+
"metadata": {},
466+
"execution_count": null,
467+
"outputs": []
468+
},
469+
{
470+
"cell_type": "markdown",
471+
"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.",
472+
"metadata": {}
473+
},
491474
{
492475
"cell_type": "code",
493476
"execution_count": null,
@@ -567,4 +550,4 @@
567550
},
568551
"nbformat": 4,
569552
"nbformat_minor": 4
570-
}
553+
}

0 commit comments

Comments
 (0)