-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_14.py
78 lines (58 loc) · 2.25 KB
/
tutorial_14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
图像二值化
"""
import cv2 as cv
import numpy as np
# 图像二值化 - 全局阈值
def threshold_demo(image):
# 灰度处理
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 调用二值化方法:
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_TRUNC) # 截断
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY) # 自己指定阈值
# ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
print("threshold_value %s" % ret)
cv.imshow("binary", binary)
def local_threshold(image):
# 灰度处理
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 调用二值化方法:
# dst = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10)
dst = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10) # 高斯
cv.imshow("local_threshold", dst)
def custom_threshold(image):
# 灰度处理
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
h, w = gray.shape[:2]
m = np.reshape(gray, [1, w*h])
mean = m.sum() / (w*h)
print("mean", mean)
ret, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
cv.imshow("binary", binary)
# 超大图像二值化
def big_image_binary(image):
print(image.shape)
cw = 256
ch = 256
h, w = image.shape[:2]
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for row in range(0, h, ch):
for col in range(0, w, cw):
roi = gray[row:row+ch, col:cw+col]
print(np.std(roi), np.mean(roi))
dev = np.std(roi)
# 小于15过滤
if dev < 15:
gray[row:row + ch, col:col + cw] = 255
else:
ret, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
gray[row:row + ch, col:cw + col] = dst
cv.imwrite("./result_1.png", gray)
print("----------Hello OpenCV----------")
src = cv.imread(r"./demo1.jpg")
# cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
# cv.imshow("input image", src)
big_image_binary(src)
cv.waitKey(0)
cv.destroyAllWindows()