Detailed explanation of python Digital Image Processing advanced filter code, python Digital Image Processing

Source: Internet
Author: User

Detailed explanation of python Digital Image Processing advanced filter code, python Digital Image Processing

This article provides many filtering methods, which are placed in the filters. rank submodule.

These methods require you to set the shape and size of the filter, so you need to import the morphology module to set.

1. autolevel

This word is translated into automatic color order in photoshop, and the image is filtered and graded using a local histogram.

The filter partially stretches the histogram of the gray pixel value to cover the entire pixel value range.

Format: skimage. filters. rank. autolevel (image, selem)

Selem represents a structured element used to set a filter.

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () auto = sfr. autolevel (img, disk (5) # plt, a circular filter with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (auto, plt. cm. gray)

2. bottomhat and tophat

Bottomhat: this filter first calculates the morphological close operation of the image, and then subtract the result value from the original image, a bit like a black hat operation.

Bottomhat: this filter first calculates the Morphological Open Operation of the image, and then subtract the result value from the original image, a bit like a white hat operation.

Format:

Skimage. filters. rank. bottomhat (image, selem)

Skimage. filters. rank. tophat (image, selem)

Selem represents a structured element used to set a filter.

The following is an example of bottomhat Filtering:

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () auto = sfr. bottomhat (img, disk (5) # plt, a circular filter with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (auto, plt. cm. gray)

3. enhance_contrast

Contrast enhancement. Find the maximum and minimum values of the local area, and check whether the pixel value of the current point is the closest to the maximum or minimum value, and then replace it with the maximum or minimum value.

Function: enhance_contrast (image, selem)

Selem represents a structured element used to set a filter.

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () auto = sfr. enhance_contrast (img, disk (5) # circular filter plt with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (auto, plt. cm. gray)

4. entropy

Partial entropy is calculated using the logarithm of base 2. This function performs binary encoding on the gray value distribution in the local area and returns the minimum value of the encoding.

Function Format: entropy (image, selem)

Selem represents a structured element used to set a filter.

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () dst = sfr. entropy (img, disk (5) # plt, a circular filter with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (dst, plt. cm. gray)

5. equalize

Balanced filtering. Use the local histogram to perform balanced filtering on the image.

Function Format: equalize (image, selem)

Selem represents a structured element used to set a filter.

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () dst = sfr. equalize (img, disk (5) # plt, a circular filter with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (dst, plt. cm. gray)

6. gradient

Returns the partial gradient value (for example, the maximum-Minimum value) of the image. This gradient value is used to replace all the pixel values in the region.

Function Format: gradient (image, selem)

Selem represents a structured element used to set a filter.

From skimage import data, colorimport matplotlib. pyplot as pltfrom skimage. morphology import diskimport skimage. filters. rank as sfrimg = color. rgb2gray (data. lena () dst = sfr. gradient (img, disk (5) # plt, a circular filter with a radius of 5. figure ('filters ', figsize = (8, 8) plt. subplot (121) plt. title ('origin image') plt. imshow (img, plt. cm. gray) plt. subplot (122) plt. title ('filted image') plt. imshow (dst, plt. cm. gray)

7. Other filters

There are many filtering methods. I will not explain them in detail here, but only the core code is provided. All function calling methods are the same.

Maximum Filter (maximum): returns the maximum value of the image's local region. This maximum value is used to replace all pixels in the region.

dst =sfr.maximum(img, disk(5))

Minimum Filter: returns the minimum value in the local area of the image. This minimum value is used to replace all pixels in the area.

dst =sfr.minimum(img, disk(5))

Mean Filter (mean): return the mean value in the image's local region. Use this mean value to replace all pixels in the region.

dst =sfr.mean(img, disk(5))

Median: returns the median value in the local area of the image. The median value replaces all pixels in the area.

dst =sfr.median(img, disk(5))

Modal filter (modal): return the modal value in the partial area of the image. Use this value to replace all pixels in the area.

dst =sfr.modal(img, disk(5))

Otsu threshold filtering (otsu): returns the otsu threshold value in the image's local region. This value is used to replace all pixels in the region.

dst =sfr.otsu(img, disk(5))

Threshold filtering: Compare each pixel value in the image's local region with the mean value. If the value is greater than the threshold value, the value is 1. If the value is less than the threshold value is 0, a two-value image is obtained.

dst =sfr.threshold(img, disk(5))

Subtract_mean: subtract the mean value from each pixel in the local area.

dst =sfr.subtract_mean(img, disk(5))

Sum: calculates the sum of pixels in a local region. This value is used to replace all pixels in the region.

dst =sfr.sum(img, disk(5))

Summary

The above is all the details about the advanced filter code for python digital image processing. I hope it will be helpful to you. Interested friends can continue to refer to this site:

Example of turtle plotting in Python

Python uses opencv to cut images in batches

Python fun project-pornographic image recognition code sharing

If you have any shortcomings, please leave a message.

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.