<jupyter_start><jupyter_text>Morphological Image Processing  Preliminaries1. The **reflection** of a set $B$ is defined as:$$\begin{equation}\hat{B} \triangleq \{ -b | b \in B \}\end{equation}$$2. The **translation** of a set $B$ by point $z$ is defined as:$$\begin{equation}(B)_z \triangleq \{ b + z | b \in B\}\end{equation}$$3. A **Structuring Element** is a small set or a subimage used to probe an image under study for properties of interest.  * In addition to its elements, its origin must be specified. Otherwise, reflection and translation on it are  ambiguous.  * When the SE is symmetric and its origin is the symmetric center, the origin can be omitted.  Erosion and Dilation DilationThe **dilation** of $A$ by $B$ is defined as:$$\begin{equation}A \oplus B \triangleq \{ a + b | a \in A, b \in B\}\end{equation}$$That is, the dilation of $A$ by $B$ is just the union of translations of $A$ by every point in $B$, i.e.:$$\begin{equation}A \oplus B = \bigcup_{b \in B}(A)_b\end{equation}$$Or equivalently, the dilation of $A$ by $B$ is just the union of translations of $B$ by every point in $A$, i.e.:$$ A \oplus B = \bigcup_{a \in A} (B)_a $$which can be interpreted as the result that each point in $A$ 'grows' into $B$ (with itself becoming the origin of $B$).Clearly, dilation is:* associative: $(A \oplus B) \oplus C = A \oplus (B \oplus C)$* symmetric: $A \oplus B = B \oplus A$Moreover, if we denote the set containing the origin alone by $\mathcal{O}$, then we have:* zero-element: $A \oplus \mathcal{O} = A$But generally, there is no set $B$ s.t. $A \oplus B = \mathcal{O}$ unless $A = \mathcal{O}$.<jupyter_code>square = np.zeros((20, 20), dtype=np.uint8)
square[6:14, 6:14] = 1

bimshow(square, figsize=(8, 8))
plt.show()
mask = np.ones((3, 3), dtype=np.uint8)
mask[0, 0], mask[2, 0], mask[0, 2], mask[2, 2] = 0, 0, 0, 0

bimshow(mask, ticklabels='off', figsize=(1, 1))
from morphology import bimage2set
mask_set = bimage2set(mask, origin=(1, 1))
mask_set
square_set = bimage2set(square)
square_set.shape
square_set_ext = np.expand_dims(square_set, axis=0)  # (1, 64, 2)
mask_set_ext = np.expand_dims(mask_set, axis=1)  # (9, 1, 2)

dilation = (square_set_ext + mask_set_ext).reshape((-1, 2))
dilation.shape
dilated = np.zeros_like(square, dtype=np.uint8)

dilated[dilation[:, 0], dilation[:, 1]] = 1

bimshow(dilated, figsize=(8, 8))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1579388353581902, 'fraction_numerical': 0.02346041055718475, 'mean_word_length': 4.566433566433567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '173272', 'license': 'MIT', 'n_tokens_mistral': 898, 'n_tokens_neox': 836, 'n_words': 315, 'path': 'chapter09/chapter09.ipynb', 'repo_name': 'DeepWalter/DIP', 'size': '2387'}