0%

OpenCV(八)-形态学

一、形态学概述

image-20241108100813837

image-20241108100932318

二、图像全局二值化

image-20241108101058616

image-20241108101138773

image-20241108101208207

1
2
3
4
5
6
7
(function) def threshold(
src: MatLike,
thresh: float,
maxval: float,
type: int,
dst: MatLike | None = ...
) -> tuple[float, MatLike]
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

img = cv2.imread('./dog.jpeg')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, dst = cv2.threshold(img1, 180, 255, cv2.THRESH_BINARY)

cv2.imshow('img', img)
cv2.imshow('gray', img1)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108101825835

三、阈值类型

image-20241108101927671

四、自适应阈值二值化

image-20241108102137919

image-20241108102255811

image-20241108102325448

image-20241108102339303

1
2
3
4
5
6
7
8
9
(function) def adaptiveThreshold(
src: MatLike,
maxValue: float,
adaptiveMethod: int,
thresholdType: int,
blockSize: int,
C: float,
dst: MatLike | None = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

img = cv2.imread('./math.png')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

dst = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 0)

cv2.imshow('img', img)
cv2.imshow('gray', img1)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108102910292

五、OpenCV腐蚀

image-20241108110047014

image-20241108110102095

image-20241108110108512

image-20241108110119499

1
2
3
4
5
6
7
8
9
(function) def erode(
src: MatLike,
kernel: MatLike, # 卷积核
dst: MatLike | None = ...,
anchor: Point = ...,
iterations: int = ..., # 腐蚀次数
borderType: int = ...,
borderValue: Scalar = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
import cv2
import numpy as np

img = cv2.imread('./j.png')

kernal = np.ones((3,3), np.uint8)
dst = cv2.erode(img, kernal, iterations=1)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108105932875

六、获取形态学卷积核

image-20241108110238512

image-20241108110320460

1
2
3
4
5
(function) def getStructuringElement(
shape: int,
ksize: Size,
anchor: Point = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

img = cv2.imread('./j.png')

# kernal = np.ones((3,3), np.uint8)
kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
dst = cv2.erode(img, kernal, iterations=1)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108110525043

七、OpenCV膨胀

image-20241108110736620

image-20241108110800618

1
2
3
4
5
6
7
8
9
(function) def dilate(
src: MatLike,
kernel: MatLike,
dst: MatLike | None = ...,
anchor: Point = ...,
iterations: int = ...,
borderType: int = ...,
borderValue: Scalar = ...
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
import cv2
import numpy as np

img = cv2.imread('./j.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
dst = cv2.dilate(img, kernal, iterations=1)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108110930616

八、开运算

image-20241108111116269

image-20241108111125042

image-20241108111321708

1
2
3
4
5
6
7
8
9
10
11
12
import cv2
import numpy as np

img = cv2.imread('./dotj.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernal)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108111926047

九、闭运算

image-20241108111953146

image-20241108112013649

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cv2
import numpy as np

img = cv2.imread('./dotinj.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))

# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernal)
# 闭运算
dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernal)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108112331993

十、形态学梯度

image-20241108112409580

image-20241108112418921

image-20241108112430232

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import cv2
import numpy as np

# img = cv2.imread('./dotinj.png')
img = cv2.imread('./j.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))

# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernal)
# 闭运算
# dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernal)

# 梯度
dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernal)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108112717909

十一、顶帽运算

image-20241108112749408

image-20241108112811275

只留下小的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import numpy as np

img = cv2.imread('./tophat.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (19,19))

# 顶帽运算
dst = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernal)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108113039248

十二、黑帽运算

image-20241108130854118

image-20241108130918268

image-20241108130927066

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import numpy as np

img = cv2.imread('./dotinj.png')

kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

# 黑帽运算
dst = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernal)

cv2.imshow('img', img)
cv2.imshow('dst', dst)

cv2.waitKey(0)

image-20241108131148212

-------------本文结束感谢您的阅读-------------