Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of binarization of python3 opencv image

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article, "how to solve the problem of binarization of python3 opencv images", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the problem of python3 opencv image binarization" article.

The figure below is as follows

We can see that the left part becomes completely black after binarization because the whole part is dark, and all the details are lost, which is obviously not the result we want.

The reason threshold function binarizes the image using a threshold, causing all pixels less than this threshold to become zero. Therefore, the binarization method using a threshold does not apply to the figure above. What's wrong with that?

Obviously, in the above picture, only the left and right areas are obviously different in brightness. The easiest way is to divide the image into two regions, each of which is binarized, that is to say, binarization requires two different thresholds. What if there are three, four or more places with different brightness? Then each region is binarized with a threshold. According to this idea, there is the cv2.adaptiveThreshold function.

Let's take a look at the effect of adaptiveThreshold binarization.

It obviously works, at least the left part is not all black.

Next, let's briefly talk about the adaptiveThreshold method.

Cv2.adaptiveThreshold (src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)

The rough meaning of this function is to take each pixel of the picture as the center of the region, and then calculate the threshold of this region to determine whether the pixel changes to 0 or 255.

Src: a grayscale image that needs binarization

MaxValue: the grayscale value that needs to be set for pixels that meet the conditions. (the grayscale value to be set)

AdaptiveMethod: adaptive threshold algorithm. Optional ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C

The binarization method provided by thresholdType:opencv can only be THRESH_BINARY or THRESH_BINARY_INV

BlockSize: the size of the area to be divided. The N value above is usually odd.

C: constant, based on the threshold calculated for each region, minus this constant as the final threshold for this region, can be negative.

Dst: output image, which can be ignored

The first two parameters are the same as the src and maxval of threshold

The third parameter, adaptiveMethod

Provide two different ways to calculate the threshold, according to the explanations of other bosses on the Internet

ADAPTIVE_THRESH_MEAN_C, which is the average value of the local neighborhood block, the algorithm is to find the mean value in the block first.

ADAPTIVE_THRESH_GAUSSIAN_C, which is the Gaussian weighted sum of local neighborhood blocks. In this algorithm, the pixels around (x, y) in the region are weighted according to their distance from the central point according to the Gaussian function.

The fourth parameter thresholdType

Only THRESH_BINARY or THRESH_BINARY_INV

The fifth parameter blockSize

When calculating the neighborhood size of the above algorithm, the general choice is 3, 5, 7. Etc.

The sixth parameter C

Each neighborhood calculates the threshold and then subtracts C as the final threshold.

Demonstrate the influence of blockSize and C on the binarization result, taking THRESH_BINARY,ADAPTIVE_THRESH_GAUSSIAN_C as an example

As you can see, when the blockSize is larger, the area involved in calculating the threshold is larger, the detail outline becomes less, and the overall outline is thicker and more obvious.

The larger the C, the smaller the threshold calculated by the NumberN neighborhood of each pixel, the greater the possibility that the center point is larger than this threshold, the greater the probability of setting it to 255, the more white pixels in the overall image, and vice versa.

This binarization is similar to canny edge detection, and it is also good for finding contours or feature points.

Import cv2import numpy as np blocksize = 3C=0def adaptive_demo (gray, blocksize, C): binary = cv2.adaptiveThreshold (gray, 255,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, C) # binary = cv2.GaussianBlur (binary, (155.15), 0) cv2.imshow ('binary', binary) def C_changed (value): global gray global blocksize global C C = value-30 print (' C) adaptive_demo (gray, blocksize) C) def blocksize_changed (value): blocksize = 2 * value + 1 print ('blocksize:', blocksize) if _ _ name__ = = "_ _ main__": image_path ='. / img/1.jpg' img = cv2.imread (image_path) gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) adaptive_demo (gray, 3,0) cv2.createTrackbar ('accounts,' binary',0, 60, C_changed) cv2.createTrackbar ('blocksize' Binary',1, 20, blocksize_changed) cv2.waitKey (0) above is the content of the article "how to solve the problem of python3 opencv Image binarization" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report