zhiqingstudy

Be a young person with knowledge and content

The corrosion in Opencv refers to the concept of corrosion in nature and the principle of opencv corrosion. The corroded convolution kernel is a matrix of all 1.

Opencv image erosion and expansion operation
腐蚀的api:eroce(img,knernel,iterations=1)
Img: pictures to be corroded
Knernel: the size of convolution core, the bigger the convolution core, the more obvious the corrosion effect
Iterations: corrosion times
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=np.ones((7,7),np.uint8)
# 腐蚀图片
dst=cv2.erode(img,kernel=kernel,iterations=5)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Expansion operation

膨胀api:dilate(img,kernel,iterations=1)
Img: pictures to be expanded
Knernel: the size of the convolution kernel. The larger the convolution kernel, the more obvious the expansion effect
Iterations: expansion times
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
# 膨胀图片
dst=cv2.dilate(img,kernel=kernel,iterations=1)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Open operation

开运算=腐蚀+膨胀
The open operation requires two steps, first corrosion and then expansion. We only need to call the open operation api to complete it at one time. The biggest use of open operation is noise reduction.
Open operation api: morphologyEx (img, MORPH_OPEN, kernel)
Img: pictures requiring open operation
MORPH_ OPEN: an open operation representing morphology
Kernel: convolution kernel. If the noise is large, you can select the convolution kernel of large points
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 开运算
dst=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Closed operation

闭运算=膨胀+腐蚀
The main problem of closed operation is that if there is a lot of noise in the image, the closed operation can eliminate the noise inside.
Closed operation api: morphologyEx (img, MORPH_CL.OSE, kernel)
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 闭运算
dst=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Morphological gradient

梯度=原图-腐蚀
The result of gradient calculation is often the edge of a figure.
Opencv image erosion and expansion operation
Gradient api: morphologyEx (img, MORPH_GRADIENT, kernel)
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
# 梯度
dst=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Top hat operation

顶帽运算=原图-开运算
The role of top hat operation: If there is a large object and some small objects in the original image, you can use top hat operation to get small objects.
Top hat operation api: morphogyEx (img, MORPH_TOPHAT, kernel)
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(13,13))
# 顶帽运算
dst=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)

Black hat operation

黑帽运算=原图-闭运算
The main function of black hat operation is to find out the small noise points in the big picture.
Opencv image erosion and expansion operation
Black hat operations api: morphologyEx (img, MORPH_BLACKHAT, kernel)
import cv2
import numpy as np


# 导入一张图
img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg')
# 创建卷积核
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(13,13))
# 黑帽运算
dst=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)

# 显示图片
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
comment
head sculpture
Code:
Related

Why you shouldn't stay at a job for more than 2 years?

3 harsh facts long-distance relationships

how to keep your girlfriend interested in a long-distance relationship




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号