-
Notifications
You must be signed in to change notification settings - Fork 5
/
Binarization.py
34 lines (24 loc) · 989 Bytes
/
Binarization.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
import cv2
import numpy as np
from debug_utils import *
from skimage.filters import (threshold_otsu, threshold_niblack,
threshold_sauvola)
def AdaptiveThresholding(img,method=0):
w,h = img.shape[:2]
blockSize = max(w,h)
blockSize = int(blockSize *0.03)
if blockSize % 2 == 0:
blockSize += 1
debug_print("blockSize " + str(blockSize))
if(method == 0):
outputImg = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,blockSize,8)
elif(method == 1):
outputImg = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,blockSize,8)
elif(method == 2):
outputImg = img > threshold_niblack(img, blockSize, k=0.8)
elif(method == 3):
outputImg = img > threshold_sauvola(img, blockSize)
return outputImg
def GlobalThresholding(img):
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
return th2