Morphological dilation and erosion image conversion opening and closing operation of edge detection implemented opencv

A official document:

dilate (expansion):


Function prototype ↑
 

Parameter Description ↑
 

    cv.erode (corrosion) and other similar use.


Second test: expansion and corrosion

. 1  # Writer: [email protected] 
2  # a Date: 2020.3.22 
. 3  Import CV2 AS CV
 . 4  Import numpy AS NP
 . 5  
. 6 Image cv.imread = ( " ../paojie.jpg " )
 . 7 Image = cv.cvtColor ( image, cv.COLOR_RGB2GRAY)
 . 8  # obtain a binary image defined threshold is 127 
. 9  # RET, Thresh1 = cv.threshold (image, 127,255, cv.THRESH_BINARY) 
10  # to obtain an appropriate binary image is automatically calculated threshold 
. 11 RET, Thresh1 = cv.threshold (Image, 0,255, cv.THRESH_BINARY + cv.THRESH_OTSU)
 12 is Black_and_White =thresh1.copy ()
 13 is  
14  # obtain corrosion image, 3 * 3 convolution kernel size is 
15 erode_result = cv.erode (Thresh1, (3,3 ))
 16  # obtain expanded image, 3 * 3 convolution kernel size is 
17 = cv.dilate dilate_result (Thresh1, (3,3 ))
 18 is  
. 19 cv.imshow ( ' Black_and_White ' , Black_and_White)
 20 is cv.imshow ( " erode_result " , erode_result)
 21 is cv.imshow ( " dilate_result " , dilate_result)
 22 is  CV .waitKey (0)
 23 cv.destroyAllWindows ()

 


Three experimental output:


Original (left), the image expansion (middle), corrosion image (right) ↑
 

Four experiments: opening and closing operation

. 1  # Writer: [email protected] 
2  # a Date: 2020.3.22 
. 3  Import CV2 AS CV
 . 4  Import numpy AS NP
 . 5  
. 6 Image cv.imread = ( " ../paojie.jpg " )
 . 7 Image = cv.cvtColor ( image, cv.COLOR_RGB2GRAY)
 . 8  # obtain a binary image defined threshold is 127 
. 9  # RET, Thresh1 = cv.threshold (image, 127,255, cv.THRESH_BINARY) 
10  # to obtain an appropriate binary image is automatically calculated threshold 
. 11 RET, Thresh1 = cv.threshold (Image, 0,255, cv.THRESH_BINARY + cv.THRESH_OTSU)
 12 is Black_and_White = thresh1.copy()
13 
14 # 闭运算
15 closing = cv.morphologyEx(thresh1, cv.MORPH_CLOSE, kernel=(3,3))
16 # 开运算
17 opening = cv.morphologyEx(thresh1, cv.MORPH_OPEN,  kernel=(3,3))
18 
19 cv.imshow('Black_and_White',Black_and_White)
20 cv.imshow("closing", closing)
21 cv.imshow("opening", opening)
22 cv.waitKey(0)
23 cv.destroyAllWindows()

 


Five experimental output:


Original (left), and close operations (in), the opening operation (right) ↑
 

Six. Morphological Gradient for detecting an image edge morphological gradient, to give the outline image

Corrosion and expansion difference obtained: Edge

函数:gradient = cv.morphologyEx(img, cv.MORPH_GRADIENT, kernel)


Seven results:


Expansion - = Corrosion image edge (contour image) ↑
 

Eight Written in the last words:

    Writing is not easy, if this article helpful to you, remember to point like oh!


Nine Copyright:

    Without the author's permission Please do not reprint plagiarism, plagiarism serious cases, the authors consider pursue its legal responsibility, the creation is not easy, thank you for your understanding and cooperation!

Guess you like

Origin www.cnblogs.com/wojianxin/p/12545642.html