introduction
1、threshold_otsu
2、threshold_yen
3、threshold_li
4、threshold_isodata
5、threshold_adaptive
introductionImage threshold segmentation is a widely used segmentation technology , Using the difference in gray characteristics between the target area to be extracted and its background in the image , The image is regarded as two kinds of regions with different gray levels ( Target area and background area ) The combination of , Select a reasonable threshold , To determine whether each pixel in the image should belong to the target area or the background area , So as to generate the corresponding binary image .
stay skimage In the library , The function of threshold segmentation is to put filters Module .
We can manually specify a threshold , To achieve segmentation . You can also let the system automatically generate a threshold , The following methods are used to automatically generate thresholds .
1、threshold_otsube based on Otsu Threshold segmentation method , Function call format :
skimage.filters.threshold_otsu(image, nbins=256)
Parameters image Is a grayscale image , Return a threshold .
from skimage import data,filtersimport matplotlib.pyplot as pltimage = data.camera()thresh = filters.threshold_otsu(image) # Return a threshold dst =(image <= thresh)*1.0 # Segment according to the threshold 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()
Return threshold is 87, according to 87 The image below is divided :
2、threshold_yenThe same as above :
thresh = filters.threshold_yen(image)
Return threshold is 198, The division is shown in the following figure :
3、threshold_liThe same as above :
thresh = filters.threshold_li(image)
Return threshold 64.5, The division is shown in the following figure :
4、threshold_isodataThreshold calculation method :
threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0
The same as above :
thresh = filters.threshold_isodata(image)
Return threshold is 87, So the segmentation effect and threshold_otsu equally .
5、threshold_adaptiveThe call function is :
skimage.filters.threshold_adaptive(image, block_size, method='gaussian')
block_size: Block size , Refers to the size of the adjacent area of the current pixel , It's usually odd ( Such as 3,5,7...)
method: The method used to determine the adaptive threshold , Yes 'mean', 'generic', 'gaussian' and 'median'.
When omitted, the default is gaussian
This function directly accesses the image after a threshold , Not the threshold .
from skimage import data,filtersimport matplotlib.pyplot as pltimage = data.camera()dst =filters.threshold_adaptive(image, 15) # Returns a threshold 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()
You can modify block_size The size and method Value to see more effects . Such as :
dst1 =filters.threshold_adaptive(image,31,'mean') dst2 =filters.threshold_adaptive(image,5,'median')
The two effects are as follows :
That's all python Details of an example of automatic image threshold segmentation for digital image processing , More about python For information on automatic threshold segmentation of digital images, please pay attention to other relevant articles on the software development network !