Previous portal:

“Python Image Processing OpenCV (1) : Getting Started”

“Python Image Processing OpenCV (2) : Pixel Processing with Numpy manipulation and Matplotlib Display images”

“Python Image Processing OpenCV (3) : Image Properties, ROI Regions of Interest and Channel Processing”

“Python Image Processing OpenCV (4) : Image Arithmetic and Color Space Modification”

“Python Image Processing OpenCV (5) : Geometric Transformations of Images”

“Python Image Processing OpenCV (6) : Image Threshold Processing”

“Python Image Processing OpenCV (7) : Image Smoothing (Filtering) Processing”

“Python Image Processing OpenCV (8) : Image Decay and Image Bloat”

“Python Image Processing OpenCV (9) : Image processing Morphology open, Closed, and gradient operations”

The introduction

Today is the last of our series on graph processing morphology, we cover top hat and black hat operations.

It is recommended to read the previous two articles on image processing:

“Python Image Processing OpenCV (8) : Image Decay and Image Bloat”

“Python Image Processing OpenCV (9) : Image processing Morphology open, Closed, and gradient operations”

Top hat operation of morphology

Image processing top hat operation is an operation to obtain image noise, which is obtained by subtracting the original image from the image open operation:

Top hat operation = original image - open operationCopy the code

Image tophat operation also uses the morphologic extension function morphologyEx(), whose parameter is MORPH_TOPHAT, as shown in the following example:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# fetch image
source = cv.imread("demo_noise_white.jpg", cv.IMREAD_GRAYSCALE)

Set the convolution kernel
kernel = np.ones((5.5), np.uint8)

# open operation
open = cv.morphologyEx(source, cv.MORPH_OPEN, kernel)

# Top hat operation
dst = cv.morphologyEx(source, cv.MORPH_TOPHAT, kernel)

# display result
titles = ['Source Img'.'Open Img'.'Tophat Img']
images = [source, open, dst]

# matplotlib drawing
for i in range(3):
   plt.subplot(1.3, i+1), plt.imshow(images[i],'gray')
   plt.title(titles[i])
   plt.xticks([]),plt.yticks([])

plt.show()
Copy the code

The black hat operation of morphology

Image processing top hat operation is an operation to obtain the small holes inside the image, or the small black spots in the foreground color.

It is the operation of the image closure minus the original image:

Black hat operation = closed operation image - original imageCopy the code

The image tophat operation also uses the morphologic extension function morphologyEx(), which takes MORPH_BLACKHAT, as shown in the following example:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# fetch image
source = cv.imread("demo_noise_black.jpg", cv.IMREAD_GRAYSCALE)

Set the convolution kernel
kernel = np.ones((5.5), np.uint8)

# Black hat operation
dst = cv.morphologyEx(source, cv.MORPH_BLACKHAT, kernel)

Construct the display result array
titles = ['Source Img'.'Black Img']
images = [source, dst]

# matplotlib drawing
for i in range(2):
   plt.subplot(1.2, i+1), plt.imshow(images[i],'gray')
   plt.title(titles[i])
   plt.xticks([]),plt.yticks([])

plt.show()
Copy the code

Today’s content is relatively short, so far, several basic operations of image morphology have been introduced, I hope you can understand the principle of these operations, rather than just know a few parameters or a few method calls.

The sample code

If you need to get the source code, you can reply “OpenCV” on the public account to get it.