Python Digital Image Processing (11): Automatic Image threshold segmentation

Source: Internet
Author: User

Image Thresholding Segmentation is a widely used segmentation technique, which uses the difference between the target region and its background in the image, and considers the image as a combination of two kinds of regions (target region and background region) with different gray levels, and chooses a reasonable threshold value. To determine whether each pixel in the image should belong to the target area or the background area, resulting in a corresponding two-value image.

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

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

1, Threshold_otsu

Threshold Segmentation method based on Otsu, function call format:

skimage.filters. Threshold_otsu (image, nbins=256)

The parameter image refers to a grayscale image, which returns a threshold value.

 fromSkimageImportdata,filtersImportMatplotlib.pyplot as Pltimage=Data.camera () Thresh= Filters.threshold_otsu (image)#returns a threshold valueDST = (Image <= Thresh) *1.0#segmentation based on threshold valuesPlt.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 ()

The return threshold is 87 and is split according to 87:

2, Threshold_yen

Use the same method as above:

The return threshold is 198, divided as:

3, Threshold_li

Use the same method as above:

Thresh = Filters.threshold_li (image)

Returns the threshold value of 64.5, divided as:

4, Threshold_isodata

How to calculate the threshold value:

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

Use the same method as above:

Thresh = Filters.threshold_isodata (image)

The return threshold value is 87, so the split effect is the same as Threshold_otsu.

5, Threshold_adaptive

The calling function is:

skimage.filters. threshold_adaptive (image, block_size, method= ' Gaussian ')

Block_size: Block size, which refers to the size of the adjacent area of the current pixel, usually an odd number (such as 3,5,7 ... )

Method: Methods used to determine adaptive thresholds, with ' mean ', ' generic ', ' Gaussian ' and ' median '. Default to Gaussian when omitted

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

 fromSkimageImportdata,filtersImportMatplotlib.pyplot as Pltimage=Data.camera () DST=filters.threshold_adaptive (image, 15)#returns a threshold imagePlt.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 change the size and method values of the block_size to see more effects. Such as:

Dst1 =filters.threshold_adaptive (image,31,'mean'=filters.threshold_adaptive (image,5,'median')

These two effects are as follows:

Python Digital Image Processing (11): Automatic Image threshold segmentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.