Python Digital Image Processing (18): Advanced Morphological processing

Source: Internet
Author: User

Morphological processing, in addition to the most basic expansion, corrosion, open/close operation, black/white hat processing, there are some more advanced applications, such as convex hull, connected area marker, delete Small block area.

1. Convex bag

Convex hull refers to a convex polygon, which contains all the white pixels in the image.

The functions are:

skimage.morphology. Convex_hull_image (image)

Input is a two value image, output a logical binary image. The point inside the convex hull is true, otherwise false

Cases:

Import Matplotlib.pyplot as Pltfrom skimage import data,color,morphology# generate a two value test image img=Color.rgb2gray ( Data.horse ()) img= (img<0.5) *chull = morphology.convex_hull_image (img) #绘制轮廓fig, axes = plt.subplots (1, 2, Figsize= (8,8)) ax0, ax1= axes.ravel () ax0.imshow (Img,plt.cm.gray) ax0.set_title (' original image ') Ax1.imshow (Chull,plt.cm.gray) ax1.set_title (' convex_hull image ')       

Convex_hull_image () is to treat all the objects in the picture as a whole, so that only one of the smallest convex polygons is computed. If you have more than one target object in the diagram, each object needs to calculate a minimum convex polygon, you need to use the Convex_hull_object () function.

function format:skimage.morphology. Convex_hull_object (image, neighbors=8)

Input parameters image is a binary image, neighbors indicates whether it is 4 connected or 8 connected, and the default is 8 connectivity.

Cases:

Import Matplotlib.pyplot as Pltfrom skimage import data,color,morphology,feature# generate two value test image img=  Color.rgb2gray (Data.coins ()) #检测canny边缘, get two value picture Edgs=feature.canny (IMG, sigma=3, low_threshold=10, high_threshold=50 ) Chull = morphology.convex_hull_object (EDGs) #绘制轮廓fig, axes = Plt.subplots (1,2,figsize= (8,8)) ax0, ax1= Axes.ravel () ax0.imshow (Edgs,plt.cm.gray) Ax0.set_title (' Many objects ') ax1.imshow (Chull,plt.cm.gray) Ax1.set_title (' convex_hull image ') plt.show ()        

2. Connected area marking

In a binary image, if the two pixels are adjacent and the values are the same (0 or the same as 1), then the two pixels are considered to be in an interconnected area. All pixels in the same connected area are labeled with the same value, which is called the connected area marker. When judging whether two pixels are contiguous, we usually use 4-connected or 8-connected judgments. In the image, the smallest units are pixels, with 8 contiguous pixels around each pixel, and there are 2 common adjacency relationships: 4 adjacency and 8 adjacency. 4 adjacency altogether 4 points, i.e. up and down, as shown in the figure on the left. 8 The adjacent points have a total of 8 points, including the point at the diagonal, as shown in the image on the right.

In the Skimage package, we use the label () function under the measure sub-module to implement the connected area tag.

function format:

skimage.measure. label (image,connectivity=none)

The image in the parameter represents the two-value images that need to be processed, connectivity represents the mode of the connection, 1 represents 4 adjacency, and 2 represents 8 adjacency.

Prints an array of tokens (labels), starting with the 0 tag.

ImportNumPy as NPImportScipy.ndimage as Ndi fromSkimageImportMeasure,colorImportMatplotlib.pyplot as Plt#write a function to generate the original binary imagedefMicrostructure (l=256): N= 5x, y= Np.ogrid[0:l, 0:l]#Build NetworkMask =Np.zeros (L, L)) generator= Np.random.RandomState (1)#random number seedPoints = L * Generator.rand (2, n**2) mask[(Points[0]). Astype (Np.int), (points[1]). Astype (np.int)] = 1Mask= Ndi.gaussian_filter (Mask, sigma=l/(4.*n))#Gaussian filter    returnMask >Mask.mean () data= Microstructure (l=128) * *#generate a test pictureLabels=measure.label (data,connectivity=2)#8 connected Area markersDst=color.label2rgb (labels)#display different colors according to different markersPrint('Regions Number:', Labels.max () +1)#show number of connected blocks (starting from 0)Fig, (Ax1, AX2)= Plt.subplots (1, 2, figsize= (8, 4) ) ax1.imshow (data, Plt.cm.gray, interpolation='Nearest') Ax1.axis ('off') ax2.imshow (dst,interpolation='Nearest') Ax2.axis ('if') Fig.tight_layout () plt.show ()

In code, where you multiply by 1, you can quickly convert a bool array to an int array.

The result has 10 connected regions, labeled 0-9

3, delete the small block area

Sometimes we just need some big chunks, those scattered, small chunks, we need to delete them, we can use the remove_small_objects () function of the morphology submodule.

function format:skimage.morphology. remove_small_objects (ar, min_size=64, connectivity=1, in_place=false)

Parameters:

AR: An array of bool types to be manipulated.

Min_size: The minimum connected area size, which is smaller than the size, will be removed. The default is 64.

Connectivity: adjacency mode, 1 means 4 adjacency, 2 means 8 adjacency

The In_place:bool type value, if true, indicates that the small area is deleted directly in the input image, otherwise it is copied and then deleted. The default is False.

Returns a two-value image that has deleted a small chunk area.

ImportNumPy as NPImportScipy.ndimage as Ndi fromSkimageImportmorphologyImportMatplotlib.pyplot as Plt#write a function to generate the original binary imagedefMicrostructure (l=256): N= 5x, y= Np.ogrid[0:l, 0:l]#Build NetworkMask =Np.zeros (L, L)) generator= Np.random.RandomState (1)#random number seedPoints = L * Generator.rand (2, n**2) mask[(Points[0]). Astype (Np.int), (points[1]). Astype (np.int)] = 1Mask= Ndi.gaussian_filter (Mask, sigma=l/(4.*n))#Gaussian filter    returnMask >Mask.mean () data= Microstructure (l=128)#generate a test pictureDST=morphology.remove_small_objects (Data,min_size=300,connectivity=1) FIG, (Ax1, AX2)= Plt.subplots (1, 2, figsize= (8, 4) ) ax1.imshow (data, Plt.cm.gray, interpolation='Nearest') ax2.imshow (dst,plt.cm.gray,interpolation='Nearest') Fig.tight_layout () plt.show ()

In this example, we delete a small area of less than 300 (from 1 to 0), and the results are as follows:

Python Digital Image Processing (18): Advanced Morphological processing

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.