20 morphological operations

Source: Internet
Author: User
Introduction

Morphological operations are actuallyChange the shape of an objectFor example, corrosion is "thin", expansion is "fat", you can see it

Morphological operations generally apply to binarization graphs to connect adjacent elements or separate them into independent elements.Corrosion and expansion are for the white part in the picture!

It is mainly used in binary image analysis, and gray images are also supported.

It can be seen that expansion expands the white area, and corrosion expands the black area. 1. Expansion and corrosion related functions

 

def getStructuringElement(shape, ksize, anchor=None): # real signature unknown; restored from __doc__

 

Shape: the shape of the kernel. Three shapes are available.

Rectangle: morph_rect; crossover: morph_corss; elliptical: morph_ellipse;

The second parameter, ksize, is the kernel size (N, N)

 

The third parameter anchor: the position of the anchor.

The getstructuringelement function returns the structural elements of the specified shape and size.

 

1. erode Corrosion

The effect of corrosion is to make the image thin. The principle is to take the local minimum value in the residential area of the source image. Because it is a binarization image, There is only 0 and 255, so a pixel in the residential area is 0 and the pixel is 0, so that the edge of the source image will become 0, to achieve the goal of slimming.

Used in opencvcv2.erode()The function performs corrosion by specifying the size of the core:

IMG = cv2.imread('j.bmp ', 0) kernel = NP. Ones (5, 5), NP. uint8) Erosion = cv2.erode (IMG, kernel) # Corrosion

This core is also called a structural element, because morphological operations are actually implemented by convolution. The structure element can be a rectangle, an elliptical shape, or a cross shape.cv2.getStructuringElement()To generate structural elements of different shapes, such:

Kernel = cv2.getstructuringelement (cv2.morph _ rect, (5, 5) # rectangular structure kernel = cv2.getstructuringelement (cv2.morph _ ellipse, (5, 5 )) # elliptical structure kernel = cv2.getstructuringelement (cv2.morph _ cross, (5, 5) # Cross Structure
Code Implementation
1 import cv2 as CV 2 Import numpy as NP 3 4 def erode_image (image): 5 print (image. shape) 6 gray = CV. cvtcolor (image, cv. color_bgr2gray) 7 ret, binary = CV. threshold (Gray, 0,255, cv. thresh_binary | cv. thresh_otsu) # obtain the binary image with greater law 8. imshow ('binary ', binary) 9 kernel = CV. getstructuringelement (cv. morph_rect, (3, 3) # The size of the convolution core can be modified to increase the corrosion effect. The larger the corrosion, the stronger the corrosion. 10 DST = CV. erode (binary, kernel) 11 cv. imshow ('erode _ demo', DST) 12 13 IMG = cv.imread('1.jpg ') 14 cv. imshow ('input image', IMG) 15 erode_image (IMG) 16 cv. waitkeyex (0) 17 cv. destroyallwindows ()

2. Expanded dilate

Expansion is opposite to corrosion. The local maximum value is used, and the effect is to make the image "fat ":

Dilation = cv2.dilate (IMG, kernel) # Expansion
Code Implementation
1 import cv2 as CV 2 Import numpy as NP 3 4 # expansion is the white part becomes more 5 def dilate_image (image): 6 print (image. shape) 7 gray = CV. cvtcolor (image, cv. color_bgr2gray) 8 ret, binary = CV. threshold (Gray, 0,255, cv. thresh_binary | cv. thresh_otsu) # obtain the binary image with greater Law 9. imshow ('binary ', binary) 10 kernel = CV. getstructuringelement (cv. morph_rect, (3, 3) # The size of the convolution core can be modified to increase the corrosion effect. The larger the size, the stronger the expansion. 11 DST = CV. dilate (binary, kernel) 12 cv. imshow ('dilate _ demo', DST) 13 14 IMG = cv.imread('1.jpg ') 15 cv. imshow ('input image', IMG) 16 dilate_image (IMG) 17 cv. waitkeyex (0) 18 cv. destroyallwindows ()

3. color images can be expanded without grayscale processing (1)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))dst = cv.dilate(src,kernel)cv.imshow("result",dst)

(2) Corrosion
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))dst = cv.erode(src,kernel)cv.imshow("result",dst)

Ii. Open and closed operations 1. Open

First corrosion and then expansion is called the open operation (because first corrosion will separate objects, so it is easy to remember), its role is: to separate objects, eliminate small areas.

Features: eliminate noise and remove small interference blocks without affecting the original image.

This type of morphological operation is usedcv2.morphologyEx()Function implementation:

Kernel = cv2.getstructuringelement (cv2.morph _ rect, (5, 5) # define the structure element IMG = comment ', 0) Opening = cv2.morphologyex (IMG, cv2.morph _ open, kernel) # Open Operation
Code Implementation
1 import cv2 as CV 2 Import numpy as NP 3 4 def camp (val1, val2): 5 Pv = val1 + val2 6 if PV> 255: 7 return 255 8 Elif PV <0: 9 return 010 else: 11 return pv12 13 def open_demo (image): 14 gray = CV. cvtcolor (image, cv. color_bgr2gray) # (H, W) = (576,1024) 15 print (Gray. shape) 16 for I in range (1000): 17 h = NP. random. random_integers (0, Gray. shape [0]-1) # because the return value is a random integer in the closed interval and H starts from 0, we need to subtract 118 W = NP. random. random_integers (0, Gray. shape [1]-1) 19 value = NP. random. random_integers (0,255) # randomly added pixel values 20 gray [h, w] = camp (gray [h, w], value) 21 ret, binary = CV. threshold (Gray, 0,255, cv. thresh_binary | cv. thresh_otsu) 22 cv. imshow ('binary ', binary) 23 kernel = CV. getstructuringelement (cv. morph_rect, (3, 3) 24 open = CV. morphologyex (binary, cv. morph_open, kernel) # on the operation, first corrosion and then expansion, will eliminate some of the 1 white noise 25 CV. imshow ('Open _ demo', binary) 26 27 IMG = cv.imread('1.jpg ') 28 cv. imshow ('input image', IMG) 29 open_demo (IMG) 30 CV. waitkey (0) 31 cv. destroyallwindows ()

2. Close

The opposite is the closed operation: First Expansion and then corrosion (first expansion will make the white part expand, so that the elimination of the small black hole in the/"closed" object, so it is called the Closed Operation)

IMG = cv2.imread('j_noise_in.bmp ', 0) Closing = cv2.morphologyex (IMG, cv2.morph _ close, kernel) # Closed Operation
Code Implementation

 

 

If our target object has many irrelevant small areas, we can use the open operation to remove them. If there are many small black holes inside the object, we can use the closed operation to fill it out.

 

 

20 morphological operations

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.