程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Example of image automatic threshold segmentation in Python digital image processing

編輯:Python

Catalog

introduction

1、threshold_otsu

2、threshold_yen

3、threshold_li

4、threshold_isodata

5、threshold_adaptive

introduction

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 .

1、threshold_otsu

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 :

2、threshold_yen

The same as above :

thresh = filters.threshold_yen(image)

Return threshold is 198, The division is shown in the following figure :

3、threshold_li

The same as above :

thresh = filters.threshold_li(image)

Return threshold 64.5, The division is shown in the following figure :

4、threshold_isodata

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 .

5、threshold_adaptive

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 :

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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved