Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threshold_local returns a different result than threshold_adaptive in scikit-image

I'm reproducing this example from the documentation:

import matplotlib.pyplot as plt

from skimage import data
from skimage.filters import threshold_otsu, threshold_adaptive


image = data.page()

global_thresh = threshold_otsu(image)
binary_global = image > global_thresh

block_size = 35
binary_adaptive = threshold_adaptive(image, block_size, offset=10)

fig, axes = plt.subplots(nrows=3, figsize=(7, 8))
ax0, ax1, ax2 = axes
plt.gray()

ax0.imshow(image)
ax0.set_title('Image')

ax1.imshow(binary_global)
ax1.set_title('Global thresholding')

ax2.imshow(binary_adaptive)
ax2.set_title('Adaptive thresholding')

for ax in axes:
    ax.axis('off')

plt.show()

There threshold_adaptive, but it raises a warning:

enter image description here

"UserWarning: The return value of threshold_local is a threshold image, while threshold_adaptive returned the thresholded image"

But when I use threshold_adaptive the result is different:

enter image description here

like image 366
Luis Ramon Ramirez Rodriguez Avatar asked Jan 28 '23 10:01

Luis Ramon Ramirez Rodriguez


1 Answers

If we look at the documentation for threshold_adaptive, we see it has been deprecated in favor of a new function threshold_local. Unfortunately, that one doesn't seem to be documented.

We can still look at older documentation to find out what threshold_adaptive does: it applies an adaptive threshold, yielding a binary output image.

The undocumented threshold_local, in contrast, doesn't return a binary image, as you found out. Here is an example for how to use it:

block_size = 35
adaptive_thresh = threshold_local(image, block_size, offset=10)
binary_adaptive = image > adaptive_thresh

What is happening?

The function computes, for each pixel, a threshold. But instead of directly applying that threshold, it returns an image containing all these thresholds. Comparing the original image with the threshold image is the way to apply the threshold, yielding a binary image.

like image 111
Cris Luengo Avatar answered Jan 31 '23 00:01

Cris Luengo