[OpenCV-Python] 17 Morphological transformation

OpenCV-Python: IV Image Processing in OpenCV

17 Morphological transformation

Goals
  • Learn different morphological operations, such as corrosion, expansion, open operation, close operation, etc.
  • The functions we need to learn are: cv2.erode(), cv2.dilate(), cv2.morphologyEx(),
etc.

Principle
  Morphological operation is a simple operation based on the shape of the image. The operation performed on the binarized image in general. Two parameters need to be input, one is the original image, and the second is called the structuring element or kernel, which is used to determine the nature of the operation. The two basic morphological operations are corrosion and expansion. Their variants constitute opening operations, closing operations, gradients, etc. We will introduce them one by one in the following figure as an example.

img

17.1 Corrosion

Just like soil erosion, this operation will corrode the boundaries of foreground objects (but the foreground is still white). How is this done? The convolution kernel slides along the image. If all the pixel values ​​of the original image corresponding to the convolution kernel are 1, then the central element keeps the original pixel value, otherwise it becomes zero.

What impact will this have? According to the size of the convolution kernel, all pixels close to the foreground will be corroded (turned to 0), so the foreground object will become smaller and the white area of ​​the entire image will be reduced. This is useful for removing white noise, and can also be used to disconnect two objects that are connected together.
Here we have an example, using a 5x5 convolution kernel, in which all values ​​are in. Let's see how he works:

import cv2
import numpy as np

img = cv2.imread('j.png',0)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)

result:

Erosion

17.2 Expansion

Contrary to corrosion, as long as one of the pixel values ​​of the original image corresponding to the convolution kernel is 1, the pixel value of the central element is 1. So this operation will increase the white area (foreground) in the image. Generally, corrosion is used first and then expansion is used to remove noise. Because corrosion removes the white noise, it also makes the foreground objects smaller. So we inflate him again. At this time the noise has been removed and will not come back, but the foreground is still and will increase. Expansion can also be used to connect two separate objects.

dilation = cv2.dilate(img,kernel,iterations = 1)

result:

img

17.3 Open operation

Corrosion of advanced nature and then expansion is called open operation. As we introduced above, it is used to remove noise. The function we use here is cv2.morphologyEx().

opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

result:

Opening

17.4 Close arithmetic

Expand first and then corrode. It is often used to fill small holes in foreground objects, or small black spots on foreground objects.

closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

result:

img

17.5 Morphological gradient

In fact, it is the difference between expansion and erosion of an image.
The result looks like the outline of the foreground object.

gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)

result:

img

17.6 Top hat

The difference between the original image and the image obtained after the open operation. The following example is the result of a top hat operation with a 9x9 core.

tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

result:

img

17.7 Black Hat

The difference between the image obtained after the closing operation and the original image.

tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

result:

img

17.8 The relationship between morphological operations

We list the relationships between the above centralized morphological operations for your reference:

Structured element
  In the previous example, we used Numpy to build the structured element, which is square. But sometimes we need to build an oval/circular core. In order to achieve this requirement, the OpenCV function cv2.getStructuringElement() is provided. You just need to tell him the shape and size of the nucleus you need.

# Rectangular Kernel
>>> cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], dtype=uint8)
# Elliptical Kernel
>>> cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
array([[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0]], dtype=uint8)
# Cross-shaped Kernel
>>> cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
array([[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]], dtype=uint8)

For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113421033