In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor will share with you an example analysis of Python OpenCV threshold processing. I hope you will get something after reading this article. Let's discuss it together.
Preface
Image segmentation is a key processing step in many computer vision applications, which is usually used to divide the image into different regions, which often correspond to real-world objects. Therefore, image segmentation is an important step in image recognition and content analysis. Image threshold is a simple and effective method for image segmentation, in which pixels are divided according to their intensity values. In this article, we will introduce the main threshold techniques provided by OpenCV, which can be used as a key part of image segmentation in computer vision applications.
Brief introduction of threshold Technology
Threshold processing is a simple and effective method to divide an image into foreground and background. Image segmentation is usually used to extract objects from the background based on some attributes of the object, such as color, edge, or histogram. The simplest threshold method uses a predefined constant (threshold), replacing it with black pixels if the pixel intensity is less than the threshold, and white pixels if the pixel intensity is greater than the threshold. OpenCV provides the cv2.threshold () function to perform threshold processing on the image.
To test the cv2.threshold () function, create the test image for the first time, which contains areas of the same size filled with different gray tones, and use the build_sample_image () function to build the test image:
Def build_sample_image (): create an area of the same size filled with different shades of gray As a test image "" # define different regions tones = np.arange (start=50, stop=300, step=50) # initialize result = np.zeros ((50,50,3), dtype= "uint8") for tone in tones: img = np.ones ((50,50,3), dtype= "uint8") * tone # join the array result = np.concatenate ((result, img), axis=1) return result along the axis
The cv2.threshold () function is then called with different predefined thresholds: 0,50,100,150,200,250 to see how different predefined thresholds affect the threshold image. For example, use the threshold thresh = 50 to threshold the image:
Ret1, thresh2 = cv2.threshold (gray_image, 50,255, cv2.THRESH_BINARY)
Where thresh2 is a threshold image that contains only black and white. In the source image gray_image, the pixels with gray intensity less than 50 are black, and the pixels with intensity greater than 50 are white.
Threshold processing of the image using several different thresholds:
# Visual function def show_img_with_matplotlib (color_img, title, pos): img_RGB = color_img [:,:,:-1] ax = plt.subplot (7,1, pos) plt.imshow (img_RGB) plt.title (title, fontsize=8) plt.axis ('off') # use build_sample_image () function to build test image image = build_sample_image () gray_image = cv2.cvtColor (image Cv2.COLOR_BGR2GRAY) for i in range (6): # threshold processing for images using multiple different thresholds ret, thresh = cv2.threshold (gray_image, 50 * I, 255, cv2.THRESH_BINARY) # Visualization show_img_with_matplotlib (cv2.cvtColor (thresh, cv2.COLOR_GRAY2BGR), "threshold = {}" .format (I * 50) I + 2) # Visual test image show_img_with_matplotlib (cv2.cvtColor (gray_image, cv2.COLOR_GRAY2BGR), "img with tones of gray-left to right: (0Magne50100150200250)", 1) # image after threshold processing A common output is a black-and-white image # so, for better visualization, modify the background color fig.patch.set_facecolor ('silver') plt.show ()
From the above picture, we can see the change of the black-and-white image generated after threshold processing according to the difference between the threshold and the grayscale value of the sample image.
Simple threshold technique
In the previous section, we briefly introduced cv2.threshold (), a simple threshold handling function provided in OpenCV, which is used as follows:
Cv2.threshold (src, thresh, maxval, type, dst=None)-> retval, dst
The cv2.threshold () function applies the threshold set by the predefined constant thresh to the src input array (which can be single-channel or multi-channel images); the type parameter is used to set the threshold type, and the optional values for the threshold type are as follows: cv2.THRESH_BINARY, cv2.THRESH_BINARY_INV, cv2.THRESH_TRUNC, cv2.THRESH_TOZERO, cv2.THRESH_TOZERO_INV, cv2.THRESH_OTSU and cv2.THRESH_TRIANGLE.
The maxval parameter is used to set the maximum value, which is valid only if the threshold types are cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV; note that when the threshold types are cv2.THRESH_OTSU and cv2.THRESH_TRIANGLE, the input image src should be a single channel.
Threshold type
In order to better understand the different types of threshold operations, the specific formulas of each threshold type are given. Symbol description: src is the source (original) image, and dst corresponds to the threshold target (result) image, so src (x, y) corresponds to the intensity at the source image pixel (x, y), while dst (x, y) corresponds to the intensity at the target image pixel (x, y).
The threshold type cv2.THRESH_BINARY formula is as follows:
It indicates that if the intensity of the pixel src (XMagi y) is higher than that of thresh, then the pixel intensity of the target image dst (XMague y) will be set to maxval; otherwise, set to 0.
The threshold type cv2.THRESH_BINARY_INV formula is as follows:
It indicates that if the intensity of the pixel src (XMagy) is higher than that of thresh, the pixel intensity of the target image dst (XMagi y) will be set to 0; otherwise, it will be set to maxval.
The threshold type cv2.THRESH_TRUNC formula is as follows:
It indicates that if the intensity of the pixel src (x, y) is higher than that of thresh, the pixel intensity of the target image is set to threshold; otherwise, it is set to src (x, y).
The threshold type cv2.THRESH_TOZERO formula is as follows:
It indicates that if the intensity of the pixel src (x, y) is higher than that of thresh, the pixel value of the target image will be set to src (x, y); otherwise, it will be set to 0.
The threshold type cv2.THRESH_TOZERO_INV formula is as follows:
It indicates that if the intensity of the pixel src (x, y) is greater than thresh, the pixel value of the target image will be set to 0; otherwise, it will be set to src (x, y).
Cv2.THRESH_OTSU and cv2.THRESH_TRIANGLE belong to special threshold types, which can be combined with the above threshold types (cv2.THRESH_BINARY, cv2.THRESH_BINARY_INV, cv2.THRESH_TRUNC, cv2.THRESH_TOZERO and cv2.THRESH_TOZERO_INV). When combined, the threshold processing function cv2.threshold () can only process single-channel images and calculates and returns the best threshold instead of specifying it.
Then different threshold types are used to process the same test image, and the effects of different threshold processing are observed.
Ret1, thresh2 = cv2.threshold (gray_image, 100,255, cv2.THRESH_BINARY) ret2, thresh3 = cv2.threshold (gray_image, 100,220, cv2.THRESH_BINARY) ret3, thresh4 = cv2.threshold (gray_image, 100,255, cv2.THRESH_BINARY_INV) ret4, thresh5 = cv2.threshold (gray_image, 100,220, cv2.THRESH_BINARY_INV) ret5, thresh6 = cv2.threshold (gray_image, 100,255,cv2.THRESH_TRUNC) ret6, thresh7 = cv2.threshold (gray_image, 100,255) Cv2.THRESH_TOZERO) ret7, thresh7 = cv2.threshold (gray_image,100,255, cv2.THRESH_TOZERO_INV) # Visualization show_img_with_matplotlib (cv2.cvtColor (thresh2, cv2.COLOR_GRAY2BGR), "THRESH_BINARY-thresh = 100 & maxValue = 255", 2) show_img_with_matplotlib (cv2.cvtColor (thresh3, cv2.COLOR_GRAY2BGR), "THRESH_BINARY-thresh = 100 & maxValue = 220", 3) show_img_with_matplotlib (cv2.cvtColor (thresh4, cv2.COLOR_GRAY2BGR) "THRESH_BINARY_INV-thresh = 100", 4) # other image visualization methods are similar I'm not going to repeat #.
As shown in the figure above, the maxval parameter is valid only when using the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV threshold types, and in the above example, the maxval values for the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV types are set to 255,220 to see how the threshold image changes in both cases.
Practical Application of simple threshold Technology
After understanding how the different parameters of cv2.threshold () work, we apply cv2.threshold () to the real image and use different thresholds:
# load image image = cv2.imread ('example.png') gray_image = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) # draw grayscale image show_img_with_matplotlib (cv2.cvtColor (gray_image, cv2.COLOR_GRAY2BGR), "img", 1) # call cv2.threshold () with different thresholds and visualize for i in range (8): ret, thresh = cv2.threshold (gray_image, 130 + I * 10,255) Cv2.THRESH_BINARY) show_img_with_matplotlib (cv2.cvtColor (thresh, cv2.COLOR_GRAY2BGR), "threshold = {}" .format (130 + I * 10), I + 2)
After reading this article, I believe you have some understanding of "sample Analysis of Python OpenCV threshold processing". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.