-
Notifications
You must be signed in to change notification settings - Fork 22
/
threshold.py
61 lines (47 loc) · 1.82 KB
/
threshold.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
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def threshold():
# Read images
img = cv.imread('./img/gradient.jpg')
img2 = cv.imread('./img/sudoku.jpg')
_, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
_, th2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
_, th3 = cv.threshold(img, 127, 255, cv.THRESH_TRUNC)
_, th4 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO)
_, th5 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO_INV)
titles = ['Original Image', 'Binary', 'Binary Inverse',
'Trunc', 'TOZero', 'TOZero Inverse']
images = [img, th1, th2, th3, th4, th5]
for i in range(len(images)):
plt.subplot(2, 3, i+1)
plt.imshow(images[i], 'gray')
plt.title(titles[i])
# TO remove ticks from x and y axis
plt.xticks([]), plt.yticks([])
# Adaptive threshold
# th6 = cv.adaptiveThreshold(
# img2, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
# th7 = cv.adaptiveThreshold(
# img2, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
# adaptiveTitle = ["Original Image", "ADAPTIVE MEAN", "ADAPTIVE GAUSS"]
# adaptiveImages = [img2, th6, th7]
# for i in range(len(adaptiveImages)):
# plt.subplot(1, 3, i+1)
# plt.imshow(adaptiveImages[i], 'gray')
# plt.title(adaptiveTitle[i])
# # TO remove ticks from x and y axis
# plt.xticks([]), plt.yticks([])
# cv.imshow('Image', img2)
# cv.imshow('Binary', th1)
# cv.imshow('Binary Inverse', th2)
# cv.imshow('Trunc', th3)
# cv.imshow('TOZero', th4)
# cv.imshow('TOZero Inverse', th5)
# cv.imshow("ADAPTIVE MEAN", th6)
# cv.imshow("ADAPTIVE GAUSS", th7)
plt.show()
cv.waitKey()
cv.destroyAllWindows()
if __name__ == "__main__":
threshold()