This article introduces in detail “python How to realize image automatic threshold segmentation ”, Detailed content , The steps are clear , The details are handled properly , Hope this article “python How to realize image automatic threshold segmentation ” The article can help you solve your doubts , Let's follow Xiaobian's ideas and go deeper slowly , Let's learn new knowledge together .
Image 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 .
be 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 :
The same as above :
thresh = filters.threshold_yen(image)
Return threshold is 198, The division is shown in the following figure :
The same as above :
thresh = filters.threshold_li(image)
Return threshold 64.5, The division is shown in the following figure :
Threshold 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 .
The 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 :
Read here , This article “python How to realize image automatic threshold segmentation ” The article has been introduced , If you want to master the knowledge points of this article, you need to practice and use it yourself to understand , If you want to know more about this article , Welcome to the Yisu cloud industry information channel .