-
Notifications
You must be signed in to change notification settings - Fork 2
/
thresh.py
35 lines (26 loc) · 850 Bytes
/
thresh.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
import cv2
import numpy as np
def threshold_impl(src, thresh, maxval):
width = src.shape[1]
height = src.shape[0]
dest = np.zeros_like(src)
for j in range(height):
for i in range(width):
if src[j, i] > thresh:
dest[j, i] = maxval
else:
dest[j, i] = 0
return dest
def main():
frame = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
th = 128
thresh_my = threshold_impl(frame, th, 255)
ret, thresh_cv = cv2.threshold(frame, th, 255, cv2.THRESH_BINARY)
thresh_np = np.where(frame > th, np.uint8(255), np.uint8(0))
cv2.imshow('result', thresh_my)
cv2.imshow('result2', thresh_cv)
cv2.imshow('result3', thresh_np)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()