当前位置:网站首页>Morphological operation of OpenCV image processing
Morphological operation of OpenCV image processing
2022-04-22 04:47:00 【weixin_ thirty-eight million eight hundred and seventy-one thou】
Opencv Morphological operation of image processing
Morphological operation : corrosion , inflation , Open close operation , Top hat and black hat , And the relationship between different operations
Connectivity
Binary figure :
Pixels 4 Neighborhood ,D Neighborhood ,8 Neighborhood

Connectivity , Meet specific similarity criteria
4 connected ,8 connected

M connected
Same value , Four connections have no intersection

Corrosion and expansion
corrosion :
Use structure B Every pixel and structure in the element A Covered pixels do “ And ” operation , If it's all for 1, Then the pixel is 1, Otherwise 0. As shown in the figure below , structure A Quilt structure B After corrosion :
Corrosion results
API:
cv.erode(img,kernel,iterations)
Parameters :
img: The image to be processed
kernel: Nuclear structure
iterations: The number of corrosions , The default is 1
inflation
Scan each pixel in the image with a structural element , Use each pixel in the structure element and the pixel it covers “ And ” operation , If it's all for 0, Then the pixel is 0, Otherwise 1

API:
cv.dilate(img,kernel,iterations)
Parameters :
img: The image to be processed
kernel: Nuclear structure
iterations: The number of corrosions , The default is 1
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 Read images
img = cv.imread("./shouxieshuzi001.jpg")
# 2 Create core structure
kernel = np.ones((5, 5), np.uint8)
# 3 Image corrosion and expansion
erosion = cv.erode(img, kernel) # corrosion
dilate = cv.dilate(img,kernel) # inflation
# 4 Image display
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(img)
axes[0].set_title(" Original picture ")
axes[1].imshow(erosion)
axes[1].set_title(" Results after corrosion ")
axes[2].imshow(dilate)
axes[2].set_title(" The result of expansion ")
plt.show()

Open close operation
Open and close operations are to treat corrosion and expansion in a certain order .
Open operation , It is considered as corrosion before expansion , Eliminate noise , Does not affect the original image .
Eliminate external noise
Closed operation , Expand first and then corrode , The function is to eliminate /“ closed ” Holes in objects
Eliminate internal holes

cv.morphologyEx(img, op, kernel)
Parameters :
img: The image to be processed
op: Processing mode : If you do an open operation , Set to cv.MORPH_OPEN, If you do a closed operation , Set to cv.MORPH_CLOSE
Kernel: Nuclear structure
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 Read images
img1 = cv.imread("./shouxieshuzi002.jpg")
img2 = cv.imread("./shouxieshuzi002.jpg")
# 2 Create core structure
kernel = np.ones((10, 10), np.uint8)
# 3 Image opening and closing operation
cvOpen = cv.morphologyEx(img1,cv.MORPH_OPEN,kernel) # Open operation
cvClose = cv.morphologyEx(img2,cv.MORPH_CLOSE,kernel)# Closed operation
# 4 Image display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title(" Original picture ")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title(" The result of the open operation is ")
axes[1,0].imshow(img2)
axes[1,0].set_title(" Original picture ")
axes[1,1].imshow(cvClose)
axes[1,1].set_title(" Closed operation result ")
plt.show()


Top hat and black hat
Top hat arithmetic
The original image and “ Open operation “ The difference between the results of
Black hat operation
by ” Closed operation “ The difference between the result image and the original image
cv.morphologyEx(img, op, kernel)
Parameters :
img: The image to be processed
op: Processing mode :

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 Read images
img1 = cv.imread("./0055.png")
img2 = cv.imread("./0055.png")
# 2 Create core structure
kernel = np.ones((10, 10), np.uint8)
# 3 Top hat and black hat operation of image
cvOpen = cv.morphologyEx(img1,cv.MORPH_TOPHAT,kernel) # Top hat arithmetic
cvClose = cv.morphologyEx(img2,cv.MORPH_BLACKHAT,kernel)# Black hat operation
# 4 Image display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title(" Original picture ")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title(" Top hat calculation result ")
axes[1,0].imshow(img2)
axes[1,0].set_title(" Original picture ")
axes[1,1].imshow(cvClose)
axes[1,1].set_title(" Black hat operation result ")
plt.show()

版权声明
本文为[weixin_ thirty-eight million eight hundred and seventy-one thou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210804320374.html
边栏推荐
- How to combine acrobat Pro DC with other files to create a single PDF file?
- JVM tuning notes
- Peer interview sharing Lenovo WinForm direction 20220420
- 菜鸟的逆袭之路
- [chestnut sugar GIS] SuperMap - how to create hyperlinks for data
- crypto-js加密算法库【安装教程、缓存加密】
- 使用指定显卡
- JVM——》CMS
- C-handwritten 10
- AT32 MCU F435/437 DFU DEMO
猜你喜欢

vue项目 npm run build 打包项目时为css、js文件加时间戳版本号,防止浏览器缓存

C-handwritten 10

The website is linked to the gambling dark chain

JVM - G1 garbage collector

2022a special equipment related management (elevator) test question simulation test question bank simulation test platform operation

Small bug -- hidden problem of Guide Package

C-随手写10

kaggle实战4.1--时间序列预测问题

Peer interview sharing Lenovo WinForm direction 20220420

2022T电梯修理考试试题及在线模拟考试
随机推荐
Error encountered when installing opencv
JVM——》CMS
Use specified graphics card
Article 1 of linked list
I'll test the timing again
12. Libevent loop function and exit test
Paper reading (48): a library of optimization algorithms for organizational design
企业数据防泄露方案分享
2022p cylinder filling test question bank and simulation test
C-随手写10
创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材
kaggle實戰4.1--時間序列預測問題
Leetcode refers to offer 43 Number of occurrences of 1 in integers 1 ~ n***
Leetcode refers to offer 65 No addition, subtraction, multiplication and division***
[chestnut sugar GIS] ArcMap - Model Builder - batch clipping grid data
goland汉化解决方法(下载插件失败的情况下)
【板栗糖GIS】supermap—如何為數據制造超鏈接
[untitled] timed
Carina 的根基與誕生背景|深入了解 Carina 系列 第一期
[selenium] basic application of unittest testing framework