forked from kennethinfante/Practical-Python-and-OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27thresholding_otsu.py
executable file
·35 lines (28 loc) · 1.01 KB
/
27thresholding_otsu.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
# Otsu's Method assumes that there are two peaks in the grayscale histogram of the image. It then tries to find an optimal value to separate these two peaks - thus our value of T
# This is Luis Pedro Coelho implementation in the mahotas package
import numpy as np
import argparse
import mahotas
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image, (5,5), 0)
cv2.imshow("Image", image)
T = mahotas.thresholding.otsu(blurred)
print "Otsu's threshold: %d" % (T)
thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Otsu", thresh)
T = mahotas.thresholding.rc(blurred)
print "Riddler-Calvard: %d" % (T)
thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Riddler-Calvard", thresh)
cv2.waitKey(0)