# Morphological Operations in Image Processing
**Binary images** (consists of pixels that can have one of exactly two colors, usually black and white) may contain `numerous imperfections`.
**Morphological Operations** in Image Processing pursues the goals of removing these imperfections by accounting for the form and structure of the image.
[TOC]
# Introduction
## What is an image
* An image on film can be understood by a two dimensional light intensity function f(x,y) where
* x and y are spatial coordinates
* The value of f at any point (x,y) is proportional to brightness or gray value of the image at that point.
* It cannot be stored as such on digital computer.
* Digital image
* One in which spatial and gray scale values have been made discrete.
* Intensities measured across the regularly spaced grid in x and y directions which are sampled to 8 bits per point for black and white, 3×8 bits per pixel for color images.
* They are stored as a two-dimensional array of gray scale values. The array elements are called pixels and identified as x,y coordinates.
* Matrices are perfect tools for mapping, representation of analog images into digital images.
* The value of f(x,y) at any point is gives the pixel value at that point of an image.
* A signal can be one dimensional or two dimensional or higher dimensional signal. One dimensional signal is a signal that is measured over time. The common example is a voice signal.
* When the sunlight falls upon the object, then the amount of light reflected by that object is sensed by the sensors, and a continuous voltage signal is generated by the amount of sensed data. We can convert this data into a digital form
## MO
A collection of **non-linear** operations related to the shape or morphology of features in an image is known as **Morphological Operation** in Image Processing.
**Morphological operations** rely only on the relative ordering of pixel values, not on their numerical values, and therefore are especially suited to the processing of binary images.
These techniques can also be extended to **greyscale images** such that their light transfer functions are unknown and therefore their absolute pixel values are of no or minor interest.
# Structuring element
A small shape or template called a **structuring element**, a matrix that identifies the pixel in the image being processed and defines the neighborhood used in the processing of each pixel is used to probe an image in these Morphological techniques.
Two main characteristics that are directly related to structuring elements:
- **Shape** (For example, the structuring element can be a "ball" or a line; convex or a ring, etc.)
For $R^2$;
- B is an open disk of radius $r$, centered at the origin
- B is a 3x3 square, that is, $B = [{(-1,-1),(-1,0),(-1,1),(0,-1),(0,0),(0,1),(1,-1),(1,0),(1,1)}]$
- B is the "cross" given by: $B=[{(-1,0),(0,-1),(0,0),(0,1),(1,0)}]$
- **Size** (One structuring element can be a $3 \times 3$ square or a $21 \times 21$ square. Setting the size of the structuring element is similar to setting the observation scale, and setting the criterion to differentiate image objects or features according to size.


Commonly these structuring elements have odd dimensions and the origin defined as the centre of the matrix. These are playing in morphological image processing the same role as convolution kernels in linear image filtering.
# Dilation
## Binary

where $A_b$ is the translation of $A$ by $b$.
Suppose A is the following 11 x 11 matrix and B is the following 3 x 3 matrix

The result

Some properties:
- It is `increasing`. So, if $A \subseteq C \Rightarrow A \oplus B \subseteq C \oplus B$
- It is `commutative`.
- It is `associative`
## Grayscale
Grayscale dilation
It is common to use flat structuring elements in morphological applications. Flat structuring functions are functions b(x) in the form

So, dilation in this case:

where $sup$ denotes the **supremum**(least upper bound).
Morphological dilation makes objects more visible and fills in small holes in objects.
# Erositon
## Binary

where $A_{-b}$ is the translation of $A$ by $-b$.

Result is:

Some properties:
- It is increasing
- 
## Grayscale

Where $inf$ denotes the **infimum**(greatest lower bound).

Morphological erosion removes islands and small objects so that only substantive objects remain.
# Compound operations
Morphological operations are represented as combinations of erosion, dilation, and simple set-theoretic operations such as the complement of a binary image, intersection and union of two or more binary images.
## Opening: (A o B=(A⊖B)⊕B)
The opening operation erodes an image and then dilates the eroded image, using the same structuring element for both operations.
Morphological opening is useful for removing small objects from an image while preserving the shape and size of larger objects in the image.

## Closing (A • B=(A⊕B)⊖B)
The closing operation dilates an image and then erodes the dilated image, using the same structuring element for both operations.
Morphological closing is useful for filling small holes from an image while preserving the shape and size of the objects in the image.

## Gradient
The difference between the dilation and the erosion of the image. This is used to find boundaries or edges in an image.
It recommended to apply some filtering before calculating the gradient because it is very sensitive to noise.

## Black hat or Bottom hat
The bottom hat transform closes an image, then subtracts the original image form the closed image.
The bottom hat transform can be used to **find intensity troughs in a grayscale image**.

## Top hat
The difference of the source or input image and the opening of the source or input image. It highlights the narrow pathways between different regions.
The top hat transform can be **used to enhance contrast in a grayscale image with non uniform illumination**. The transform can also **isolate small bright objects** in an image.
