-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
130 lines (100 loc) · 3.55 KB
/
util.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import cv2
import math
from numba import jit
import numpy as np
import time
def thresholdIntegral(inputMat,s,T = 0.15):
# outputMat=np.uint8(np.ones(inputMat.shape)*255)
outputMat=np.zeros(inputMat.shape)
nRows = inputMat.shape[0]
nCols = inputMat.shape[1]
S = int(max(nRows, nCols) / 8)
s2 = int(S / 4)
for i in range(nRows):
y1 = i - s2
y2 = i + s2
if (y1 < 0) :
y1 = 0
if (y2 >= nRows):
y2 = nRows - 1
for j in range(nCols):
x1 = j - s2
x2 = j + s2
if (x1 < 0) :
x1 = 0
if (x2 >= nCols):
x2 = nCols - 1
count = (x2 - x1)*(y2 - y1)
sum=s[y2][x2]-s[y2][x1]-s[y1][x2]+s[y1][x1]
if ((int)(inputMat[i][j] * count) < (int)(sum*(1.0 - T))):
outputMat[i][j] = 255
# print(i,j)
# else:
# outputMat[j][i] = 0
return outputMat
@jit(nopython=True)
def thresholdIntegral1(inputMat,s,T = 0.15):
# outputMat=np.uint8(np.ones(inputMat.shape)*255)
outputMat=np.zeros(inputMat.shape)
nRows = inputMat.shape[0]
nCols = inputMat.shape[1]
S = int(max(nRows, nCols) / 8)
s2 = int(S / 4)
for i in range(nRows):
y1 = i - s2
y2 = i + s2
if (y1 < 0) :
y1 = 0
if (y2 >= nRows):
y2 = nRows - 1
for j in range(nCols):
x1 = j - s2
x2 = j + s2
if (x1 < 0) :
x1 = 0
if (x2 >= nCols):
x2 = nCols - 1
count = (x2 - x1)*(y2 - y1)
sum=s[y2][x2]-s[y2][x1]-s[y1][x2]+s[y1][x1]
if ((int)(inputMat[i][j] * count) < (int)(sum*(1.0 - T))):
outputMat[i][j] = 255
# print(i,j)
# else:
# outputMat[j][i] = 0
return outputMat
if __name__ == '__main__':
ratio=1
image = cv2.imdecode(np.fromfile('testimage.jpg', dtype=np.uint8), 0)
img = cv2.resize(image, (int(image.shape[1] / ratio), int(image.shape[0] / ratio)), cv2.INTER_NEAREST)
retval, otsu = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
cv2.namedWindow('OTSU threshold',0)
cv2.imshow('OTSU threshold',otsu)
cv2.imwrite('otsu_results.jpg',otsu)
# thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
# retval, thresh = cv2.threshold(img, 150, 255, cv2.THRESH_OTSU)
# retval, thresh = cv2.threshold(img, retval, 255, cv2.THRESH_OTSU)
time_start = time.time()
roii = cv2.integral(img)
time_end = time.time()
print('integral cost', time_end - time_start)
# time_start = time.time()
for j in range(1):
thresh = thresholdIntegral1(img, roii)
time_end = time.time()
print('totally cost', time_end - time_start)
cv2.namedWindow('fast inergral threshold',0)
cv2.imshow('fast inergral threshold',thresh)
cv2.imwrite('results.jpg', np.uint8(thresh))
time_start = time.time()
roii = cv2.integral(img)
time_end = time.time()
print('integral cost', time_end - time_start)
# time_start = time.time()
for j in range(1):
thresh = thresholdIntegral(img, roii)
time_end = time.time()
print('totally cost', time_end - time_start)
cv2.namedWindow('integral threshold',0)
cv2.imshow('integral threshold',thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()