-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththresholding.py
More file actions
64 lines (54 loc) · 1.5 KB
/
thresholding.py
File metadata and controls
64 lines (54 loc) · 1.5 KB
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
import cv2
img = cv2.imread("../data/rainbow.jpg")
cv2.imshow("Rainbow", img)
cv2.waitKey(0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(src=img, thresh=127, maxval=255, type=cv2.THRESH_BINARY) # 0 -> Black, 255 -> White
"""
THRESH_BINARY
-------------
if pixel > thresh -> pixel = max_val
else pixel = 0
"""
cv2.imshow("Thresh1", thresh1)
cv2.waitKey(0)
_, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
"""
THRESH_BINARY_INVERSE
--------------------
if pixel > thresh -> pixel = 0
else pixel = max_val
"""
cv2.imshow("Thresh2", thresh2)
cv2.waitKey(0)
_, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
"""
THRESH_TRUNC
------------
if pixel > thresh -> pixel = thresh
else pixel = pixel
"""
cv2.imshow("Thresh3", thresh3)
cv2.waitKey(0)
_, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
"""
THRESH_TOZERO
-------------
if pixel > thresh -> pixel = pixel
else pixel = 0
"""
cv2.imshow("Thresh4", thresh4)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("../data/crossword.jpg", 0)
cv2.imshow("Crossword", img)
cv2.waitKey(0)
_, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("Thresh1", thresh1)
cv2.waitKey(0)
thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, 35)
cv2.imshow("Adaptive Thresh_Mean_C", thresh2)
cv2.waitKey(0)
thresh3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 35, 25)
cv2.imshow("Adaptive Thresh_Gaussian_C", thresh3)
cv2.waitKey(0)