Opencv visual eye Python image processing Xi: Top Hat operation and black hat operation of Opencv image morphological processing

This column mainly introduces that if image processing is carried out through OpenCv python, the function processing prototype of OpenCv Python is understood through the principle. In specific cases, different levels and methods of processing are carried out for different images, so as to achieve a series of operations such as image denoising and sharpening. At the same time, I hope those who watch this column can understand the powerful image processing of OpenCv. If you reprint it, please indicate the source (original link and author's signature). Thank you!

Previous reference:
OpenCv visual eye Python image processing 1: introduction to OpenCv Python and python environment construction
Opencv visual eye Python image processing II: Opencv image reading, display, saving basic function prototype and use
Opencv visual eye Python image processing III: Opencv image attributes, ROI region acquisition and channel processing
Opencv visual eye Python image processing IV: four methods and principles of Opencv image gray processing
Opencv visual eye Python image processing 5: mean filter, box filter, median filter and Gaussian filter for Opencv image denoising
Opencv visual eye Python image processing VI: principle and implementation of Opencv image Fourier transform and inverse Fourier transform
Opencv visual eye Python image processing 7: high pass filtering and low pass filtering principle and structure of Opencv image processing
Opencv visual eye Python image processing 8: image thresholding principle and function of Opencv image processing
Python image processing of Opencv visual eye IX: image corrosion and expansion principle and method of Opencv image morphological processing
Opencv visual eye Python image processing 10: Principles and methods of open operation, close operation and gradient operation of Opencv image morphological processing

The last blog explained the opening operation, closing operation and gradient operation of image morphology processing. This is not only a necessary step of image morphology processing, but also a step that needs to be understood.

In this blog, we will explain the top hat and black hat operation of image morphological processing. Understand the function through the principle. Let's learn together!

1, Top hat operation

  • Image top hat operation is usually used to solve the problem of image segmentation error due to uneven illumination
  • Extract line noise in the image (such as line noise and curve noise, which has little or no effect on point noise extraction)

1. Top hat operation principle

1) . principle
The image top hat (or image hat) operation is the result of subtracting the image operation from the original image to obtain the image noise
2) The principle formula is as follows:
Top hat operation (img) = original image (img) - open operation (img)

2. Construction of top hat operation function

1) Function definition

def morph_tophat(img,size):
    #Constructing convolution kernel
    #Set convolution kernel
    kernel = np.ones((size,size), np.uint8)
    #Call opencv to open the operation library function
    img1=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    img2=img-img1#The operation result of image top cap is obtained by principle formula
    return img2

2) Read the image, call the image top hat operation function, and perform image top hat operation processing

import cv2
import numpy as np
#np.set_printoptions(threshold=np.inf)  #Print all the contents of the array
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("d.jpg")
img1=morph_tophat(img,3)#Call image top hat operation function
#Display image
titles = ['Original drawing', 'Top hat operation']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i],"gray")  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


The above function construction principle is combined with the operation library function. For the operation principle, partners can check the previous blog and write it. This time, they can set the top hat operation principle directly through the operation library function!

It can be seen that the line noise extraction effect of the above image is obvious, but if we replace it with image point noise, it will not have much effect. If we do not adjust the size of convolution kernel, it will have no effect

The extraction effect of top hat operation on salt and pepper noise is as follows:

So how do we deal with the extraction of salt and pepper noise? Only by adjusting the size of the convolution kernel can we extract it. When we set the size of the convolution kernel to 7x7, the following results are obtained:

The effect of top hat operation on linear noise extraction is as follows:

3. Use of OpenCV top hat operation Library

In OpenCV, a library function for direct top hat operation is also provided, which is mainly implemented by calling morphologyEx(), in which the parameter CV2 MORPH_ Tophat means that the top cap treatment is described below
1) . function prototype: result = CV2 morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

  • img: the image that needs image top hat operation
  • cv2.MORPH_TOPHAT: image top hat operation parameter (fixed parameter, please input this parameter if performing top hat operation)
  • Kernel: customize the convolution kernel of all 1, for example: kernel = NP ones((5,5), np. uint8)

2) Usage of top hat operation library function

'''
Image top hat operation-OpenCV How to use library functions
'''
import cv2
import numpy as np
#np.set_printoptions(threshold=np.inf)  #Print all the contents of the array
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("d.jpg")
#Set convolution kernel
kernel = np.ones((3,3), np.uint8)
#Call the image top hat operation library function
img1=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
#Display image
titles = ['Original drawing', 'Top hat operation']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i],"gray")  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


At the beginning of image top hat operation, we said that image top hat operation is usually used to solve the problem of image segmentation error due to uneven illumination. How to understand this is shown in the figure below:

It can be seen that the above is a rice grain image with uneven illumination. We need to call the image top hat operation to solve the problem of uneven illumination. We adjust the convolution kernel to the size of 10x10, and the top hat operation results are as follows:

2, Black hat operation

  • Image black hat operation is usually used to obtain small holes in the image or black spots in the foreground color
  • It is also commonly used to solve the problem of image segmentation error due to uneven illumination

1. Black hat operation principle

1) . principle
Image black hat operation is the result of subtracting the original image from the image close operation to obtain the small hole in the image or the small black spot in the foreground color
2) The principle formula is as follows:
Black hat operation (img) = closed operation image (img) - original image (img)

2. Construction of black hat operation function

1) Construction of black hat operation function

'''
Image black hat operation
'''
def morph_blackhat(img,size):
    #Set convolution kernel
    kernel = np.ones((size,size), np.uint8)
    #Call opencv to open the operation library function
    img1=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    img2=img1-img#Calculate the image black hat operation result through the principle formula
    return img2

2) Read the image and call the black hat operation function to perform the black hat operation of the image

import cv2
import numpy as np
#np.set_printoptions(threshold=np.inf)  #Print all the contents of the array
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("c.jpg")
img1=morph_blackhat(img,7)#Call the image black hat operation function and set the convolution kernel to 7x7 size
#Display image
titles = ['Original drawing', 'Black hat operation']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i],"gray")  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


Image black hat operation is usually used to extract the internal noise points of images. Different images extract the internal noise points of images by adjusting the size of convolution kernel.

The above functions are constructed with the help of opencv library functions of closed operation. If you need to understand the construction principle of closed operation, please refer to other blog tutorials in the previous chapters!

3. Use of OpenCV black hat operation Library

OpenCV also has library functions for black hat operation, which are introduced as follows
1) . function prototype: result = CV2 morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)

  • img: the image that needs image top hat operation
  • cv2.MORPH_BLACKHAT: image black hat operation parameter (fixed parameter, please input this parameter if black hat operation is performed)
  • Kernel: customize the convolution kernel of all 1, for example: kernel = NP ones((5,5), np. uint8)

2) Usage of black hat operation library function

'''
Image black hat operation-OpenCV How to use library functions
'''
import cv2
import numpy as np
#np.set_printoptions(threshold=np.inf)  #Print all the contents of the array
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("c.jpg")
#Set convolution kernel
kernel = np.ones((7,7), np.uint8)
#Call the image top hat operation library function
img1=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
#Display image
titles = ['Original drawing', 'Black hat operation']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i],"gray")  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


Similarly, the black hat operation mentioned at the beginning of the black hat operation is also commonly used to solve the problem of image segmentation error due to uneven illumination. Set the convolution kernel size to 10x10, as shown below:

Image top hat operation and black hat operation are rarely used in image processing, but they will also be used in specific occasions, such as detecting defects in textile layout. They are very useful, but if they are only for ordinary image processing, they are not used much, just understand the principle!

The above is the whole content of this blog. If you encounter problems, remember to leave a message and comment. The senior will answer it for you. This senior is not too cold!

These things happen to everyone. Walk a stranger in place. Listen to a talk at the top of the mountain. Take a look at the bones at the bottom of the sea. Think about a night's future on the sofa. This is a silent film. Only God can give you subtitles. Friends can't watch it with you, but they will wait for you to leave at the door, and then go to a new place with a giggle - run away with you with a smile all the way

Another programming year in January^

Tags: Python Computer Vision

Posted by djumaka on Tue, 24 May 2022 06:27:11 +0300