[200 OpenCV routines of youcans] 140. Grayscale bottom hat transformation to correct lighting

Welcome to the series of "200 OpenCV Routines of Youcans"
. Welcome to the series of "OpenCV Learning Courses of Youcans" , which is being updated continuously.


[200 OpenCV routines of youcans] 140. Grayscale bottom hat transformation to correct lighting


Grayscale morphology extends morphological operations from binary images to grayscale images. Gray morphological processing also has operations such as erosion, expansion, opening operation, closing operation, top hat operation, and low hat operation, which can realize image smoothing, image enhancement, and image segmentation functions.

The gray value of the image pixel is regarded as the height, and different gray levels represent different heights. The whole image is like a undulating topographic map. A bright area (large gray value) is equivalent to a mountain, a dark area (small gray value) is equivalent to a deep valley, and the edge area, that is, the junction of light and darkness, is equivalent to a cliff.


5.3 Grayscale top hat transformation and grayscale bottom hat operation

Image subtraction is combined with opening and closing operations to obtain top hat transformation and bottom hat transformation.

The low-hat transformation of a grayscale image f is defined as the result of the image closure operation minus the original image:

B h a t ( f ) = f ∙ b − f = ( f ⊕ b ) ⊖ b − f B_{hat}(f) = f \bullet b - f = (f \oplus b) \ominus b - fBhat(f)=fbf=(fb)bf

The closing operation can delete the dark area under the bright background, and the bottom hat transformation can get the dark area in the original image, so it is also called the black bottom hat transformation.

The function cv.morphologyEx in OpenCV can realize the top hat operation and bottom hat operation of the image, and the parameter op should be set to MORPH_TOPHAT and MORPH_BLACKHAT respectively.


Routine 10.31: Grayscale Bottom Hat Transform Correcting Lighting

Uniform lighting is important for extracting objects from the background. Similarly, an important use of the bottom hat transformation is to correct the effects of uneven lighting, which can be used to remove dark areas against a bright background, resulting in dark areas in the original image.

    # 10.31: 灰度底帽变换
    imgGray = cv2.imread("../images/imgHat01.png", flags=0)  # flags=0 灰度图像

    # 直接用 Otsu 最优阈值处理方法进行二值化处理
    ret, imgBin = cv2.threshold(imgGray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # 二值化处理,白色背景

    # 底帽变换后再用 Otsu 最优阈值处理方法进行二值化处理
    element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (55, 55))  # 圆形结构元
    imgBhat = cv2.morphologyEx(imgGray, cv2.MORPH_BLACKHAT, element)  # 底帽运算
    ret, imgBhatBin = cv2.threshold(imgBhat, 50, 255, cv2.THRESH_BINARY |cv2.THRESH_OTSU)  # 二值化处理,白色背景
    # 圆环表面有很多黑色噪点,通过闭操作去除
    element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 圆形结构元
    imgSegment = cv2.morphologyEx(imgBhatBin, cv2.MORPH_CLOSE, element)  # 闭运算

    plt.figure(figsize=(9, 6))
    plt.subplot(221), plt.title("Origin"), plt.axis('off')
    plt.imshow(imgGray, cmap='gray', vmin=0, vmax=255)
    plt.subplot(222), plt.title("Otsu segment"), plt.axis('off')
    plt.imshow(imgBin, cmap='gray', vmin=0, vmax=255)
    plt.subplot(223), plt.title("BlackHat image"), plt.axis('off')
    plt.imshow(imgBhat, cmap='gray', vmin=0, vmax=255)
    plt.subplot(224), plt.title("Tophat segment"), plt.axis('off')
    plt.imshow(imgSegment, cmap='gray', vmin=0, vmax=255)
    plt.tight_layout()
    plt.show()

insert image description here


(End of this section)


Copyright Notice:

Youcans@xupt original works, reprints must be marked with the original link: (https://blog.csdn.net/youcans/article/details/123565844)

Copyright 2022 youcans, XUPT
Crated:2022-3-24


Welcome to the series of "200 OpenCV Routines of Youcans"
. Welcome to the series of "OpenCV Learning Courses of Youcans" , which is being updated continuously.

[Youcans's OpenCV routines 200] 01. Image reading (cv2.imread)
[youcans's OpenCV routines 200] 02. Image saving (cv2.imwrite)
[youcans's OpenCV routines 200] 03. Image display (cv2.imshow)
[youcans' OpenCV routines 200] 04. Display images with matplotlib (plt.imshow)
[youcans's OpenCV routines 200] 05. Image attributes (np.shape)
[youcans' 200 OpenCV routines] 06. Pixel editing (img.itemset)
[youcans's OpenCV routines 200] 07. Image creation (np.zeros)
[youcans's OpenCV routines 200] 08. Image duplication ( np.copy)
[Youcans's OpenCV routines 200] 09. Image cropping (cv2.selectROI)
[youcans's OpenCV routines 200] 10. Image stitching (np.hstack)
[youcans's OpenCV routines 200 ] 11. Splitting of image channels (cv2.split)
[200 OpenCV routines of youcans] 12. Merging of image channels (cv2.merge)
[200 OpenCV routines of youcans] 13. Image addition (cv2 .add )
[200 OpenCV routines of youcans] 14. Image and scalar addition (cv2.add)
[200 OpenCV routines of youcans] 15. Weighted addition of images (cv2.addWeight)
[200 OpenCV routines of youcans] 16. Image addition of different sizes
[200 OpenCV routines of youcans] 17. Two images Gradient switching
[youcans's OpenCV routines 200] 18. Image mask addition
[youcans's OpenCV routines 200] 19. Image circular mask
[youcans's OpenCV routines 200] 20. Image press Bit operation
[youcans's OpenCV routines 200] 21. Image overlay
[youcans's OpenCV routines 200] 22. Image adding non-Chinese text
[youcans's OpenCV routines 200] 23. Image adding Chinese text
[youcans's 200 OpenCV routines] 24. Affine transformation of images
[youcans's OpenCV routines 200] 25. Image translation
[youcans's OpenCV routines 200] 26. Image rotation (centered on the origin)
[youcans's 200 OpenCV routines] 27. Image rotation (centered on any point)
[youcans's OpenCV routines 200] 28. Image rotation (right-angle rotation)
[youcans's OpenCV routines 200] 29. Image flip (cv2.flip)
[200 OpenCV routines of youcans] 30. Image scaling (cv2.resize)
[Youcans's OpenCV routines 200] 31. Image pyramid (cv2.pyrDown)
[youcans's OpenCV routines 200] 32. Image distortion (miscut)
[youcans's OpenCV routines 200] 33. Image Compound transformation
[youcans's OpenCV routines 200] 34. Image projection transformation
[youcans's OpenCV routines 200] 35. Image projection transformation (boundary filling)
[youcans's OpenCV routines 200] 36. Cartesian coordinates and Polar coordinate conversion
[youcans's OpenCV routines 200] 37. Image grayscale processing and binarization
[youcans's OpenCV routines 200] 38. Image inverse color transformation (image inversion)
[youcans's OpenCV routines 200 articles] 39. Linear transformation of image grayscale
[youcans's OpenCV routines 200 articles] 40. Image piecewise linear grayscale transformation
[youcans's OpenCV routines 200 articles] 41. Image grayscale transformation (gray Degree level layering)
[youcans's OpenCV routines 200] 42. Image grayscale transformation (bit plane layering)
[youcans's OpenCV routines 200] 43. Image grayscale transformation (logarithmic transformation)
[youcans 200 articles of OpenCV routines] 44. Grayscale transformation of images (gamma transformation)
[200 articles of OpenCV routines of youcans] 45. Grayscale histogram of images
[200 articles of OpenCV routines of youcans] 46. Histogram equalization change
[Youcans's OpenCV routines 200 articles] 47. Image enhancement - histogram matching
[youcans's OpenCV routines 200 articles] 48. Image enhancement - color histogram matching
[youcans's OpenCV routines 200 articles] 49. Image enhancement - local Histogram processing
[youcans's OpenCV routines 200 articles] 50. Image enhancement - histogram statistics image enhancement
[youcans's OpenCV routines 200 articles] 51. Image enhancement - histogram reverse tracking
[youcans's OpenCV routines 200 articles ] ] 52. Image correlation and convolution operations
[youcans's OpenCV routines 200] 53. Scipy implements two-dimensional image convolution
[youcans's OpenCV routines 200] 54. OpenCV implements image two-dimensional convolution
[youcans's OpenCV 200 routines] 55. Separable convolution kernel
[youcans's OpenCV routines 200] 56. Low-pass box filter
[youcans's OpenCV routines 200] 57. Low-pass Gaussian filter
[youcans's OpenCV example ] Cheng 200 articles] 58. Nonlinear filtering - median filtering
[youcans's OpenCV routines 200 articles] 59. Non-linear filtering - bilateral filtering
[youcans's OpenCV routines 200 articles] 60. Nonlinear filtering - joint bilateral filtering
[youcans 200 articles of OpenCV routines] 61. Guided filter
[200 articles of OpenCV routines of youcans] 62. Image sharpening - passivation masking
[Youcans's OpenCV routines 200] 63. Image sharpening - Laplacian operator
[youcans's OpenCV routines 200] 64. Image sharpening - Sobel operator
[youcans's OpenCV routines 200] 65. Image Sharpening - Scharr operator
[youcans's OpenCV routines 200] 66. Image filtering low-pass/high-pass/band-stop/band-pass
[youcans's OpenCV routines 200] 67. Comprehensive application of spatial domain image enhancement
[ Youcans's OpenCV routines 200 articles] 68. Comprehensive application of spatial domain image enhancement
[youcans's OpenCV routines 200 articles] 69. Fourier coefficients of continuous non-periodic signals
[youcans's OpenCV routines 200 articles] 70. One-dimensional continuous function Fourier transform of
[youcans's OpenCV routines 200] 71. Sampling of continuous functions
[youcans's OpenCV routines 200] 72. One-dimensional discrete Fourier transform
[youcans's OpenCV routines 200] 73. Two Dimensional continuous Fourier transform
[youcans's OpenCV routines 200] 74. Image anti-aliasing
[youcans's OpenCV routines 200] 75. Numpy implements image Fourier transform
[youcans's OpenCV routines 200] 76 . OpenCV implements image Fourier transform
[youcans's OpenCV routines 200] 77. OpenCV implements fast Fourier transform
[Youcans's OpenCV routines 200 articles] 78. Basics of frequency domain image filtering
[youcans's OpenCV routines 200 articles] 79. Basic steps of frequency domain image filtering
[youcans's OpenCV routines 200 articles] 80. Frequency domain image filtering in detail Steps
[youcans's OpenCV routines 200] 81. Frequency domain Gaussian low-pass filter
[youcans's OpenCV routines 200] 82. Frequency domain Butterworth low-pass filter
[youcans's OpenCV routines 200] 83. Frequency domain low-pass filtering: repairing printed text characters
[youcans's OpenCV routines 200] 84. Obtaining high-pass filters from low-pass filters
[youcans's OpenCV routines 200] 85. Application of frequency-domain high-pass filters
[youcans 200 articles of OpenCV routines] 86. Application of frequency domain filtering: fingerprint image processing
[200 articles of OpenCV routines of youcans] 87. Passivation masking of frequency domain
[200 articles of OpenCV routines of youcans] 88. Laplacian of frequency domain High-pass filtering
[youcans' OpenCV routines 200] 89. Transfer function of band-stop filter
[youcans's OpenCV routines 200] 90. Frequency domain notch filter
[youcans's OpenCV routines 200] 91. Gaussian noise , Rayleigh noise, Irish noise
[youcans's OpenCV routines 200] 92. Exponential noise, uniform noise, salt and pepper noise
[youcans's OpenCV routines 200] 93. Histogram of the noise model
[Youcans's OpenCV routines 200] 94. Arithmetic mean filter
[youcans's OpenCV routines 200] 95. Geometric mean filter
[youcans's OpenCV routines 200] 96. Harmonic averaging filter
[youcans's OpenCV 200 routines] 97. Inverse harmonic averaging filter
[youcans's OpenCV routines 200] 98. Statistical sorting filter
[youcans's OpenCV routines 200] 99. Corrected alpha mean filter
[youcans's OpenCV routines ] 200 articles] 100. Adaptive local noise reduction filter
[youcans's OpenCV routines 200 articles] 101. Adaptive median filter
[youcans's OpenCV routines 200 articles] 102. The transfer function of the notch band-stop filter
[ Youcans's OpenCV routines 200] 103. Notch band-stop filter to eliminate periodic noise interference
[youcans's OpenCV routines 200] 104. Motion blur degradation model
[youcans's OpenCV routines 200] 105. Turbulence blur degradation model
[Youcans's OpenCV routines 200] 106. Inverse filtering of degraded images
[youcans's OpenCV routines 200] 107. Wiener filtering of degraded images
[youcans's OpenCV routines 200] 108. Constrained least squares filtering
[200 OpenCV routines of youcans] 109. Geometric mean filtering
[Youcans's OpenCV routines 200 articles] 110. Projection and Raiden transform
[youcans's OpenCV routines 200 articles] 111. Raiden transform back-projection reconstructed images
[youcans's OpenCV routines 200 articles] 112. Filtered back-projection reconstructed images
[200 OpenCV routines by youcans] 113. Corrosion of morphological operations
[200 OpenCV routines by youcans] 114.
Dilation of morphological operations [200 OpenCV routines by youcans] 115. Open operation of morphological operations
[ Youcans's OpenCV routines 200 articles] 116. The closed operation of morphological operations
[youcans's OpenCV routines 200 articles] 117. The top hat operation of morphological operations
[youcans's OpenCV routines 200 articles] 118. The bottom of morphological operations Hat operation
[youcans's OpenCV routines 200 articles] 119. Morphological gradients of images
[youcans's OpenCV routines 200 articles] 120. hit-hit-miss transformation
[youcans's OpenCV routines 200 articles] 121. hit- Missed for feature recognition
[youcans' OpenCV routines 200] 122. Boundary extraction of morphological algorithms
[youcans's OpenCV routines 200] 123. Hole filling of morphological algorithms
[youcans's OpenCV routines 200] 124. Flooding algorithm for hole filling
[200 OpenCV routines of youcans] 125. Extraction of connected components by morphological algorithm
[Youcans's OpenCV routines 200] 126. Convex hull of morphological algorithms
[youcans's OpenCV routines 200] 127. Refinement of morphological algorithms
[youcans's OpenCV routines 200] 128. The skeleton of the morphological algorithm (skimage)
[Youcans's OpenCV routines 200] 129. The skeleton of the morphological algorithm (reconstruction operation)
[youcans's OpenCV routines 200] 130. Morphological extraction of horizontal and vertical lines
[youcans's OpenCV routines 200] 131. Vertical line character extraction of morphological reconstruction
[youcans's OpenCV routines 200] 132. Morphological reconstruction of the hole filling algorithm
[youcans's OpenCV routines 200] 133. Morphological reconstruction of the boundary clearing
[youcans's OpenCV routines 200 ] Articles] 134. Cell Counting of Morphological Reconstruction
[200 OpenCV Routines of Youcans] 135. Granularity Determination of Morphological Reconstruction
[200 OpenCV Routines of Youcans] 136. Grayscale Erosion and Grayscale
Dilation [OpenCV Example of Youcans] Program 200 articles] 137. Principle of grayscale open operation and grayscale closed operation
[youcans's OpenCV routines 200 articles] 138. Grayscale open operation and grayscale closed operation
[youcans's OpenCV routines 200 articles] 139. Grayscale top Hat transformation to correct shadows
[youcans's OpenCV routines 200 articles] 140. Grayscale bottom hat transformation to correct lighting

Guess you like

Origin blog.csdn.net/youcans/article/details/123565844