is there something like dynamic or adaptive thresholding in OpenCV?

Viewed 48

I need to find blobs in a range of grayscale images. This seems to be an easy task with thresholding using OpenCV, so I am doing these basic steps:

# get 'gray' image from previous step ...
bias = 1.5
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
thresh, bin_img = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_, bin_img = cv2.threshold(blurred, thresh * bias, 255, cv2.THRESH_BINARY_INV)

First I calculate a "good" thresholding value using the Otsu algorithm, then I perform the final thresholding operation based on that otsu result * a custom bias value. This basically works well, howver, I noticed that the value I need for bias very much depends on the image I am processing.

Here are two examples. In the first one the otsu result is fine, so I can set bias to 1.0 - using a higher bias value removes too much of the blob:

First example

However, as you can see in this second example, here I need to set bias to 3.0, which leads to a perfect result - using a bias value below 3.0 leaves too much noise in the image:

Second example

Now, is there some way to "dynamically" adjust this bias value (using some sort of computer vision algorithm), so that I would always get that optimal result? Or is this something only the human eye+brain can do, and I would need some kind of deep learning or other "intelligent" approach for this?

For your reference, some (more) example images can be found here.

0 Answers
Related