# opencv image processing - image morphology

Posted by goldlikesnow on Sat, 23 Oct 2021 16:19:21 +0200

Mathematical morphology is an important tool for image processing. It can be used to obtain image boundary, extract skeleton, remove noise and detect corners
Character recognition, visual detection and medical image processing.

Morphological transformation is a simple operation based on image shape. Morphological processing is mainly aimed at binary images (the gray value of any pixel in the image is only 0 or 255)

The two basic morphological operators are corrosion and expansion.

## Structural element (kernel)

When investigating the relationship between the parts of the target image, it is necessary to design a "probe" to collect information, that is, structural elements.

• By constantly moving the structural elements in the image, we can investigate the relationship between the parts in the image.

• The shapes of structural elements include rectangle, cross, ellipse and diamond. If the length and width of structural elements are equal, rectangle and ellipse will degenerate into square and circle.

• Generally speaking, the size of structural elements should be significantly smaller than the size of the target image. Selecting structural elements with different shapes and sizes can extract different features in the image.

• The structure element itself is also an image set. For the structure element, you can specify an origin as the reference point for the structure element to participate in morphological operation. The origin may or may not be included in the structure element.

## Corrosion operation

Principle:

Corrosion is the most basic mathematical morphology operation. For a given target image X and structural element S, if S is moved on the image, there is a difference for each current position X That is, the result of S corrosion to X is the set of origin positions of S when S is completely contained in X.

A pixel in the original image (whether 1 or 0) is considered as 1 only when all pixels under the kernel are 1, otherwise it will be eroded (become 0).
Depending on the size of the kernel, all pixels near the boundary are discarded.
Therefore, the thickness or size of the foreground object decreases, or only the white area in the image decreases. It helps to remove small white noise and separate two connected objects.

code:

getStructuringElement() and erode() functions are used in OpenCV to realize the setting of structural elements and image corrosion operation

cv2.getStructuringElement(eleType,ksize,point)

• eleType: structural element type. OpenCV defines three basic structural element shapes

``` · MORPH_RECT  rectangle
. MORPH_CROSS  cross
. MORPH_ELLIPSE ellipse
```
• ksize: structure element size

• Point: location of anchor point (center point of structural element)

dst=cv2.erode(src,kernel,iterations)

• Kernel: convolution kernel
• Iterations: number of iterations. The default value is 1
```import cv2 as cv
import numpy as np
# Set the structure element through getStructuringElement()
kernel=cv.getStructuringElement(cv.MORPH_RECT,(3,3),(1,1))
# You can also set the convolution kernel through the np.ones() function
# kernel=np.ones((3,3),np.uint8)
# corrosion
erosion=cv.erode(img,kernel,iterations=2)
cv.imshow('original',img)
cv.imshow('erosion',erosion)
cv.waitKey(0)
cv.destroyAllWindows()
```

Processing results: Compared with the processing results, we found that the white noise in the original image was corroded.

## Expansion operation

Principle:

For a given target image X and structural element S, if S is moved on the image, there is a difference for each current position X That is, the set obtained by expanding X with S is S v S^{v} The set of origin positions of S when the translation of Sv intersects at least one common non-zero element of X

( S v S^{v} Sv is the mapping of S to the origin)

Contrary to the etching operation (although etching eliminates white noise, it also compresses the image), but it is not an inverse operation,
If at least one pixel under the kernel is "1", the pixel element is "1", and the image will expand. Therefore, it increases the white area in the image or increases the size of the foreground object.

code:

dst=cv.dilate(src,kernel,iterations)

• Kernel: convolution kernel
• Iterations: the number of iterations. The default value is 1. Expand once
```import cv2 as cv
import numpy as np
# Generating convolution kernel using numpy Library
kernel=np.ones((5,5),np.uint8)
erosion=cv.erode(img,kernel,iterations=2)# corrosion
cv.imshow('erosion',erosion)
# expand
k=np.ones((3,3),np.uint8) # Use 3 * 3 convolution kernel
dliate=cv.dilate(erosion,k)
cv.imshow('dilate',dliate)
cv.waitKey(0)
cv.destroyAllWindows()
```

Processing results: Here, we expand the binary image after corrosion

Since corrosion and expansion are not inverse operations, they can be used together. Based on the two basic operations of corrosion and expansion, a morphological operation cluster can be constructed

## Open operation

First corrode the image and then expand the result, which is called open operation. It is useful for eliminating noise

code:

```import cv2 as cv
import numpy as np
# Open operation
k=np.ones((5,5),np.uint8)
erode1=cv.erode(pic2,k,iterations=1)
dilate1=cv.dilate(erode1,k)
cv.imshow('original',pic2)
cv.imshow('open',dilate1)
cv.waitKey(0)
cv.destroyAllWindows()
```

Or open= cv.morphologyEx(img, cv.MORPH_OPEN, kernel)

Processing results: ## Closed operation

Expanding the image first and then eroding the result is called closed operation.

Useful when closing small holes inside foreground objects or small black spots on objects.

code:

```import cv2 as cv
import numpy as np
k=np.ones((5,5),np.uint8)
# Closed operation
dilate2=cv.dilate(pic1,k)
erode2=cv.erode(dilate2,k,iterations=1)
cv.imshow('original',pic1)
cv.imshow('close',erode2)
cv.waitKey(0)
cv.destroyAllWindows()
```

Or close = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)

Processing results: Difference between image expansion and corrosion operation

Morphological gradient can be used to highlight the edge and retain the edge contour of the object ## Top cap

Difference between original drawing and operation calculation

tophat = cv.morphologyEx(src, cv.MORPH_TOPHAT, kernel) ## Black hat

Difference between closed operation and original graph

blackhat = cv.morphologyEx(src, cv.MORPH_BLACKHAT, kernel) 