Python-OpenCV adaptive threshold image processing uses the adaptiveThreshold function to obtain image contours

☞ ░ to the old ape Python Bowen directory

I. Introduction

In the " OpenCV threshold processing function threshold processing 32-bit color image case " introduced the threshold function, but the threshold image threshold processing for some unevenly illuminated images, this global threshold segmentation method does not get good results.

In the image thresholding operation, we are more concerned about separating the target area and the background area from the binarized image. It is difficult to achieve the ideal segmentation effect only through a fixed threshold. The gray scale in the picture is not uniform, so usually the thresholds of different areas in the picture are different. In this way, a method is needed to perform threshold processing by calculating the local threshold according to the brightness or gray distribution of different areas of the image. This method is adaptive thresholding image processing. In fact, this can be called a local threshold method. The adaptiveThreshold in OpenCV is this method.

Two, adaptiveThreshold syntax introduction

Calling syntax:
adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)

Description:

  • src: source image, must be an 8-bit grayscale image
  • dst: The processed target image, the size and type are the same as the source image
  • maxValue: used to specify the gray value of the pixel setting that meets the condition
  • adaptiveMethod: The adaptive threshold algorithm used, there are two types of ADAPTIVE_THRESH_MEAN_C algorithm (local neighborhood block average) or ADAPTIVE_THRESH_GAUSSIAN_C (local neighborhood block Gaussian weighted sum). The calculation method of ADAPTIVE_THRESH_MEAN_C is to calculate the average value of the neighborhood and subtract the first The value of six parameters C, the calculation method of ADAPTIVE_THRESH_GAUSSIAN_C is to calculate the Gaussian uniform value of the neighborhood and subtract the value of the sixth parameter C. Use BORDER_REPLICATE | BORDER_ISOLATED mode when processing boundaries
  • thresholdType: Threshold type, which can only be one of THRESH_BINARY or THRESH_BINARY_INV. For details, please refer to the table of "Image Threshold Processing" above
  • blockSize: indicates the size of the neighborhood block, which is used to calculate the area threshold, generally 3, 5, 7...
  • C: Represents a constant, which is a constant extracted from a uniform or weighted average, usually a positive number, but it can also be a negative number or zero
  • Return value: the processed image

Three, supplementary explanation

  1. The binarization threshold of the image area with higher brightness will usually be higher, and the binarization threshold of the image area with lower brightness will be smaller.
  2. In a grayscale image, the area where the grayscale value changes obviously is often the contour of the object, so dividing the image into small blocks to calculate the threshold will often get the contour of the image. Therefore, the function adaptiveThreshold can not only binarize the grayscale image, but also perform edge extraction.
  3. The reason for edge extraction is that when the block is very small, such as block_size=3 or 5 or 7, the degree of "adaptation" is very high, that is, it is easy to appear that the pixel values ​​in the block are almost the same, so that it cannot be binarized , But can only achieve binarization where the gradient is large at the edge, and it turns out that it is an edge extraction function
  4. When blockSize is set to a relatively large value, such as blockSize=21 or 31 or 41, adaptiveThreshold is a binary function
  5. blockSize must be an odd number greater than 1 (the principle is still unclear),
  6. If the average value method is used, the average value is 180, the difference delta is 10, and maxValue is 255. Then pixels with a grayscale less than 170 are 0, pixels greater than or equal to 170 are 255, if it is reverse binarization, pixels with grayscale less than 170 are 255, and pixels greater than or equal to 170 are 0

Four, case

import cv2

img = cv2.imread(r'F:\screenpic\1.jpg',cv2.IMREAD_GRAYSCALE)
newImg = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 3, 5)
cv2.imshow('img',img)
cv2.imshow('newImg',newImg)
cv2.waitKey(60000)

Operating effect:
Source image:
Insert picture description here
The following is the result image of setting different blockSizes. The block size of the left image is 31, and the blockSize of the right image is 3: It
Insert picture description here
can be seen that the blockSize is small, the contour recognition effect is obvious, and when the blockSize is large, it is a binary image.

Paid column about the old ape

Lao Yuan’s paid column " Developing Graphical Interface Python Applications Using PyQt " specifically introduces the basic tutorials of PyQt graphical interface development based on Python, and the paid column " Moviepy Audio and Video Development Column " details the related methods and usage of moviepy audio and video editing and synthesis processing The method is used to process related editing and synthesis scenes. The two columns add up to only 19.9 yuan, which is suitable for novice readers who have a certain Python foundation but no relevant patent knowledge. These two paid columns have corresponding free columns, but the articles in the paid column are more specific, more in-depth, and more cases.

Paid column article catalog : " Moviepy audio and video development column article directory ", " Use PyQt to develop graphical interface Python application column directory ".

Regarding the content of Moviepy audio and video development, please refer to the guided introduction of " Python Audio and Video Clip Library MoviePy1.0.3 Chinese Tutorial Guide and Executable Tool Download ".

For those who lack Python foundation, you can learn Python from scratch through Lao Yuan’s free column " Column: Python Basic Tutorial Directory ".

If you are interested and willing to support the readers of Old Ape, welcome to buy paid columns.

Learn Python from the old ape!

☞ ░ to the old ape Python Bowen directory

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/108558834