Image threshold segmentation is a widely used segmentation technology, using the image to extract target area and background on the gray characteristic difference, the image is regarded as two types with different grayscale area (target region and background region) combinations, select a reasonable threshold, in order to determine each pixel in the image should belong to the target region and background region, Thus generating the corresponding binary image.

In the Skimage library, the threshold segmentation function is placed in the filters module.

We can manually specify a threshold to achieve segmentation. You can also have the system automatically generate a threshold. The following methods are used to automatically generate a threshold.

1, threshold_otsu

Threshold segmentation method based on Otsu, function call format:

skimage.filters.threshold_otsu(imagenbins=256)

The parameter image refers to a grayscale image and returns a threshold.

from skimage import data,filters import matplotlib.pyplot as plt image = data.camera() thresh = Threshold_otsu (image) # threshold_otsu(image) # DST =(image <= thresh)*1.0 # plt.subplot(121) plt.title('original image') plt.imshow(image,plt.cm.gray) plt.subplot(122) plt.title('binary image') plt.imshow(dst,plt.cm.gray) plt.show()Copy the code

The return threshold is 87, and the following figure is segmented according to 87:

2, threshold_yen

Use the same as above:

thresh = filters.threshold_yen(image) Copy the code

The return threshold is 198 and the segmentation is shown as follows:

3, threshold_li

Use the same as above:

thresh = filters.threshold_li(image)Copy the code

Return the threshold of 64.5, segmented as follows:

4, threshold_isodata

Threshold calculation method:

Threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0

Use the same as above:

thresh = filters.threshold_isodata(image)Copy the code

The return threshold is 87, so the segmentation is the same as threshold_otsu.

5, threshold_adaptive

The call function is:

Skimage. Filters. Threshold_adaptive (image, block_size method = ‘gaussian’)

Block_size: block size, the size of the adjacent area of the current pixel, usually an odd number (e.g. 3,5,7…)

Method: The method used to determine the adaptive threshold, including ‘mean’, ‘generic’, ‘Gaussian’ and ‘median’. The value is gaussian by default

This function directly accesses the image after a threshold, not the threshold.

from skimage import data,filters import matplotlib.pyplot as plt image = data.camera() dst =filters.threshold_adaptive(image, Plt.figure ('thresh',figsize=(8,8)) plt.subplot(121) plt.title('original image') plt.imshow(image,plt.cm.gray) plt.subplot(122) plt.title('binary image') plt.imshow(dst,plt.cm.gray) plt.show()Copy the code

You can modify block_size and method values to see more effects. Such as:

dst1 =filters.threshold_adaptive(image,31,'mean') 
dst2 =filters.threshold_adaptive(image,5,'median')Copy the code

The two effects are as follows: