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

**Download address: Remember to like it O(∩_∩)O**

- https://github.com/eastmountyxz/Python-zero2one
- Open source 600+ page ebook: [https://github.com/eastmountyxz/HWCloudImageRecognition]

**Appreciation of the previous article:**

Part 1 Basic Grammar

- [Python from zero to one] 1. Why do we need to learn Python and basic grammar
- [Python from zero to one] 2. Conditional statements, loop statements and functions based on grammar
- [Python from zero to one] 3. Syntax-based file operations, CSV file reading and writing and object-oriented

Part II Web Crawler

- [Python from zero to one] 4. The basics of web crawler entry and the case of regular expression crawling blog
- [Python from zero to one] 5. Detailed explanation of the basic grammar of BeautifulSoup of web crawlers
- [Python from zero to one] 6. Detailed explanation of the web crawler BeautifulSoup crawling Douban TOP250 movies
- [Python from zero to one] 7. Requests of web crawler to crawl Douban movie TOP250 and CSV storage
- [Python from zero to one] 8. Detailed explanation of MySQL basic knowledge and operation of database
- [Python from zero to one] 9. Detailed explanation of Selenium basic technology of web crawler (positioning elements, common methods, keyboard and mouse operations)
- [Python from zero to one] 10. Selenium of web crawler crawls online encyclopedia knowledge detailed explanation (NLP corpus construction necessary skills)

Part 3 Data Analysis and Machine Learning

- [Python from zero to one] Eleven. Numpy, Pandas, Matplotlib and Sklearn introductory knowledge for data analysis
- [Python from zero to one] 12. Regression analysis of machine learning 4D summary of the whole network first release (linear regression, polynomial regression, logistic regression)
- [Python from zero to one] Thirteen. The 4D summary of cluster analysis of machine learning is first published on the whole network (K-Means, BIRCH, hierarchical clustering, tree clustering)
- [Python from zero to one] 14. A 30,000-word summary of the classification algorithm of machine learning.
- [Python from zero to one] 15. Data preprocessing, Jieba tools and text clustering for text mining
- [Python from zero to one] 16. Word cloud hotspots of text mining and LDA topic distribution analysis
- [Python from zero to one] 17. Detailed introduction to Matplotlib, Pandas, and Echarts for visual analysis
- [Python from zero to one] 18. Detailed introduction to the Basemap map package for visual analysis
- [Python from zero to one] 19. Detailed explanation of heat map and box plot drawing and application of visual analysis
- [Python from zero to one] 20. Detailed explanation of Seaborn drawing of visual analysis
- [Python from zero to one] 21. Detailed explanation of Pyechart drawing of visual analysis
- [Python from zero to one] 22. Detailed explanation of OpenGL drawing for visual analysis
- [Python from zero to one] 23. Detailed explanation of decision tree classification analysis of top ten machine learning algorithms (1)
- [Python from zero to one] 24. Detailed explanation of KMeans cluster analysis of the top ten machine learning algorithms (2)
- [Python from zero to one] 25. KNN algorithm and detailed image classification of the top ten machine learning algorithms (3)
- [Python from zero to one] 26. Naive Bayesian Algorithm and Text Classification of Top Ten Machine Learning Algorithms (4)
- [Python from zero to one] 27. Detailed analysis of the linear regression algorithm of the top ten machine learning algorithms (5)
- [Python from zero to one] 28. Detailed analysis of the SVM algorithm of the top ten machine learning algorithms (6)
- [Python from zero to one] 29. Detailed analysis of the random forest algorithm of the top ten machine learning algorithms (7)
- [Python from zero to one] 30. Logistic regression algorithm of top ten machine learning algorithms and detailed application of malicious request detection (8)
- [Python from zero to one] 31. Detailed application of Boosting and AdaBoost of the top ten machine learning algorithms (9)
- [Python from zero to one] Thirty-two. Detailed application of hierarchical clustering and dendrogram clustering of top ten machine learning algorithms (10)

Part 4 Python Image Processing Basics

- [Python from zero to one] Thirty-three. What is image processing and OpenCV configuration in the basics of image processing
- [Python from zero to one] Thirty-four. OpenCV introductory details – display read, modify and save images
- [Python from zero to one] thirty-five. Image processing basics OpenCV draws various geometric figures
- [Python from zero to one] Thirty-six. Detailed explanation of image arithmetic and logical operations in the basics of image processing
- [Python from zero to one] Thirty-seven. Image fusion processing and ROI area drawing in the basics of image processing
- [Python from zero to one] Thirty-eight. Image geometric transformation of image processing basics (translation, scaling and rotation)
- [Python from zero to one] Thirty-nine. Image geometric transformation of image processing basics (mirror affine perspective)
- [Python from zero to one] Forty. Image quantization processing in the basics of image processing
- [Python from zero to one] forty-one. Image processing basics of image sampling processing
- [Python from zero to one] Forty-two. Image processing basics of image pyramid upsampling and downsampling

Part 5 Python Image Operations and Image Enhancement

- [Python from zero to one] Forty-three. Image point operation and image grayscale processing in image enhancement and operation
- [Python from zero to one] 44. Detailed explanation of image grayscale linear transformation in image enhancement and operation
- [Python from zero to one] 45. Detailed explanation of image grayscale nonlinear transformation in image enhancement and operation
- [Python from zero to one] 46. Image Thresholding in Image Enhancement and Operation
- [Python from zero to one] Forty-seven. Detailed explanation of corrosion and expansion of image enhancement and operation
- [Python from zero to one] Forty-eight. Morphological opening operation, closing operation and gradient operation of image enhancement and operation
- [Python from zero to one] Forty-nine. Top hat operation and bottom hat operation of image enhancement and operation

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 #read image src = cv2.imread('test01.png', cv2.IMREAD_UNCHANGED) #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 #read image img = cv.imread( "test02.png" ) 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 #read image src = cv2.imread('test02.png', cv2.IMREAD_UNCHANGED) #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.