# [Python from zero to one] Forty-nine. Top hat operation and bottom hat operation of image enhancement and operation

Welcome to “Python from Zero to One”, where I will share about 200 Python series articles, take everyone to learn and play together, and see the interesting world of Python. All articles will be explained based on cases, codes and the author’s experience. I really want to share my nearly ten years of programming experience with you. I hope it will be helpful to you. Please also ask Haihan for the inadequacies in the article. The overall framework of the Python series includes 10 articles on basic grammar, 30 articles on web crawlers, 10 articles on visual analysis, 20 articles on machine learning, 20 articles on big data analysis, 30 articles on image recognition, 40 articles on artificial intelligence, 20 articles on Python security, and 10 articles on other skills . Your attention, likes and retweets are the greatest support for Xiu Zhang. Knowledge is priceless and people have affection. I hope we can all be happy and grow together on the road of life.

This series of articles mainly explains the knowledge of Python OpenCV [image processing] and image recognition. In the early stage, it mainly explained the basic knowledge of image processing, the basic usage of OpenCV, common image rendering methods, image geometric transformation, etc. In the middle stage, various operations of image processing, including image point operations, Morphological processing, image sharpening, image enhancement, image smoothing, etc., post-study image recognition, image segmentation, image classification, image special effects processing and image processing related applications.

The second part will explain image operations and image enhancement. The previous article introduced opening operations, closing operations, and gradient operations. This article will continue to introduce top hat operations and bottom hat operations. Mathematical Morphology is a new method applied in the field of image processing and pattern recognition. Mathematical morphology (also called image algebra) represents a mathematical tool for analyzing images based on morphology. . I hope the article is helpful to you, if there are any deficiencies, please also ask Haihan.

### Article directory

Appreciation of the previous article:

Part 1 Basic Grammar

Part II Web Crawler

Part 3 Data Analysis and Machine Learning

Part 4 Python Image Processing Basics

Part 5 Python Image Operations and Image Enhancement

Part 6 Python Image Recognition and Image Advanced Cases

Part 7 NLP and Text Mining

Part 8 Introduction to Artificial Intelligence

Part IX Network Attack and Defense and AI Security

Part 10 Knowledge Graph Construction Practice

Expand some artificial intelligence advanced cases

The author’s new “Nazhang AI Security Home” will focus on Python and security technology, mainly sharing Web penetration, system security, artificial intelligence, big data analysis, image recognition, malicious code detection, CVE recurrence, threat intelligence analysis, etc. article. Although the author is a technical novice, he will ensure that every article will be written with great care. I hope these basic articles will be helpful to you and make progress with you on the road of Python and security.

# 1. Image top hat operation

Image top-hat transformation, also known as image top-hat transformation, is the result of subtracting the image opening operation from the original image, and is often used to solve the problem of image segmentation errors due to uneven illumination. Its formula is defined as follows:

The image top-hat operation uses a structuring element to remove objects from an image through the open operation. The top-hat operation is used for bright objects on a dark background. One of its important uses is to correct the effects of uneven lighting. The effect diagram is shown in Figure 1.

In Python, the image top-hat operation is mainly implemented by calling morphologyEx(), where the parameter cv2.MORPH_TOPHAT represents the top-hat processing, and the function prototype is as follows:

dst = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)

• src represents the original image
• cv2.MORPH_TOPHAT represents the image top hat operation
• kernel represents the convolution kernel, which can be constructed with the numpy.ones() function

Assuming that there is an image of rice grains with uneven lighting, as shown in Figure 2, we need to call the image top hat operation to solve the problem of uneven lighting.

The Python code for the image top hat operation is as follows:

```# -*- coding: utf-8 -*-
# By：Eastmount
import cv2
import numpy as np

#Set up convolution kernel
kernel = np.ones((10,10), np.uint8)

#image top hat operation
result = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)

#Show image
cv2.imshow( "src" , src)
cv2.imshow("result", result)

#waiting for display
cv2.waitKey(0)
cv2.destroyAllWindows()```

The result of its operation is shown in Figure 3.

The figure below shows the rendering of the “Rice Grain” top-hat operation. You can see that the image after the top-hat operation removes most of the non-uniform background and separates the rice grains from the background.

Why does the image top hat operation eliminate the effect of uneven lighting?
The algorithm can usually be explained using a grayscale three-dimensional image. The grayscale 3D image is mainly implemented by calling the Axes3D package. The code to draw the grayscale 3D image to the original image is as follows:

```# -*- coding: utf-8 -*-
# By：Eastmount
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
imgd = np.array(img)       #image class to numpy

# prepare data
sp = img.shape
h = int(sp[0])         #image height(rows)
w = int(sp[1])         #image width(colums) of image

#Drawing initial processing
fig = plt.figure(figsize=(16,12))
ax = fig.gca(projection="3d")

x = np.arange(0, w, 1)
y = np.arange(0, h, 1)
x, y = np.meshgrid(x,y)
z = imgd
surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm)

#custom z-axis
ax.set_zlim(-10, 255)
ax.zaxis.set_major_locator(LinearLocator(10)) #Set    the density of the z-axis grid lines

#Convert the value string of z to float and retain 2 decimal places
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Set the label and title of the axis
ax.set_xlabel('x', size=15)
ax.set_ylabel('y', size=15)
ax.set_zlabel('z', size=15)
ax.set_title("surface plot", weight='bold', size=20)

#Add the color card bar on the right
fig.colorbar(surf, shrink=0.6, aspect=8)
plt.show()```

The running result is shown in Figure 5, where x represents the width coordinate in the original image, y represents the height coordinate in the original image, and z represents the gray value of the pixel (x, y).

The trend of pixels in the image shows that the image is affected by uneven illumination of various parts, resulting in uneven background grayscale, and the depression corresponds to the area with relatively small grayscale values ​​in the image.

The three-dimensional image grayscale image after the image white hat operation is shown in Figure 6. The corresponding grayscale is more concentrated in the range of 10 to 100, which proves that the uneven background is roughly eliminated, which is conducive to the subsequent threshold segmentation or Image segmentation.

The core code of the top hat operation added to draw a three-dimensional map is as follows:

# 2. Image bottom hat operation

The bottom-hat transformation of the image is also called the black-hat operation of the image. It is the result of subtracting the original image by the closed operation of the image, so as to obtain the small holes inside the image or the black points in the foreground color. Solve the problem of wrong image segmentation due to uneven lighting. Its formula is defined as follows:

The image bottom hat operation uses a structuring element to remove objects from an image through a closing operation, and is often used to correct the effects of uneven lighting. The effect diagram is shown in Figure 8.

In Python, the image bottom hat operation is mainly implemented by calling morphologyEx(), where the parameter cv2.MORPH_BLACKHAT represents bottom hat or black hat processing, and the function prototype is as follows:

dst = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)

• src represents the original image
• cv2.MORPH_BLACKHAT represents image bottom hat or black hat operation
• kernel represents the convolution kernel, which can be constructed with the numpy.ones() function

The code for implementing the image bottom hat operation in Python is as follows:

```# -*- coding: utf-8 -*-
# By：Eastmount
import cv2
import numpy as np

#Set up convolution kernel
kernel = np.ones((10, 10), np.uint8)

#image black hat operation
result = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)

#Show image
cv2.imshow( "src" , src)
cv2.imshow("result", result)

#waiting for display
cv2.waitKey(0)
cv2.destroyAllWindows()```

The result of its operation is shown in Figure 9:

# 3. Summary

This series mainly explains the knowledge of image mathematical morphology, and combines the principles and codes to introduce operations such as image erosion, image expansion, image opening and closing operations, image top hat operations and image bottom hat operations in detail. This article introduces the top-hat operation and bottom-hat operation in detail, which will provide effective support for subsequent image segmentation and image recognition.

Thanks to the colleagues on the way to study, live up to the meeting, don’t forget the original intention. The image processing series mainly includes three parts, namely:

Happy Dragon Boat Festival everyone! I’ve been really busy recently. I look forward to writing a series of blogs after graduation. Come on!

references:

• [1] Gonzalez, translated by Ruan Qiuqi. Digital Image Processing (3rd Edition) [M]. Beijing: Electronic Industry Press, 2013.
• [2] Ruan Qiuqi. Digital Image Processing (3rd Edition) [M]. Beijing: Electronic Industry Press, 2008.
• [3] Mao Xingyun, Leng Xuefei. Introduction to OpenCV3 Programming [M]. Beijing: Electronic Industry Press, 2015.
• [4] Eastmount. [Python image processing] 8. Image erosion and image expansion [EB/OL]. (2018-10-31). /Eastmount/article/details/83581277.