-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotsu_thresholding.py
36 lines (27 loc) · 1.11 KB
/
otsu_thresholding.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
# Python program to illustrate
# Otsu thresholding type on an image
# organizing imports
import os
import cv2
import numpy as np
FILE_PATH = os.path.dirname(__file__)
# path to input image is specified and
# image is loaded with imread command
for i in ['input1.jpg', 'input_book_image.jpg']:
image1 = cv2.imread(os.path.join(FILE_PATH, i))
# cv2.cvtColor is applied over the
# image input with applied parameters
# to convert the image in grayscale
img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
# applying Otsu thresholding
# as an extra flag in binary
# thresholding
ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
# the window showing output image
# with the corresponding thresholding
# techniques applied to the input image
cv2.imshow('Otsu Threshold', thresh1)
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()