-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_opencv.py
186 lines (133 loc) · 3.73 KB
/
test_opencv.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# test_opencv.py
# 11-Feb-2018
#
# author: Hernando Vidal, Jr., Tesseract Tech
# for American Museum of Natural History 'Hack the Deep' Hackathon
# 10-11 Feb-2018
#####################################################################
import sys
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
from scipy import signal
####################################################################
# dumpJson(sFile, sOut, dX, dY, aOffsets)
#
def dumpJson(sFile,sOut,dX,dY,aOffsets):
print ("{\n"
" file:\"%s\",\n"
" out:\"%s\",\n"
" width:%d,\n"
" height:%d,\n"
% (sFile,sOut,dX, dY))
sys.stdout.write(" offsets:[")
if len(aOffsets):
for i in range(len(aOffsets)-1):
sys.stdout.write ("%d," % aOffsets[i])
sys.stdout.write ("%d" % aOffsets[-1])
print "]\n}"
return
####################################################################
#
def gammaCorrect(img, fCor):
img = img / 255.0
img = cv2.pow(img,fCor)
return np.uint8(img * 255)
####################################################################
# main()
# args: <inputFile> [-n]
#
def main():
if len(sys.argv) < 2:
print "Failure"
exit()
else:
sImg = sys.argv[1]
bLoud = False
if len(sys.argv) > 2:
bLoud = True if sys.argv[2] == '-l' else False
if bLoud:
print "Image is: ", sImg
# test for output directory
# if not os.path.isdir("out"):
# print "Error, output path \"../out\" does not exist"
# exit()
imgIn = cv2.imread(sImg)
if not imgIn.any():
print "Could not open image"
exit()
img = cv2.cvtColor(imgIn,cv2.COLOR_BGR2GRAY)
dY, dX = img.shape[:2]
nP = dX * dY
if bLoud:
cv2.imshow(sImg,img)
cv2.waitKey(0)
# establish rough histogram to determine how to scale in brightness/contrast
hist, bins = np.histogram(img.ravel(),256)
# sum bins from top to mid in order to determine rough brightness
nX = 0
for i in range(255, 127, -1):
nX += hist[i]
if bLoud:
print "Ratio of bright to total:", float(nX) / nP
img = gammaCorrect(img, 2.0)
if bLoud:
cv2.imshow("gamma", img)
cv2.waitKey(0)
if dX > dY:
fS = 800.0 / dX
else:
fS = 800.0 / dY
imgCopy = img.copy()
dXsample = 10
dX2 = (dX / 2) - dXsample
accum = np.zeros(dY)
line = np.zeros(dY)
if bLoud:
print "midStart, x:", dX2, " end:", dX2 + dXsample
for iX in range(dX2-dXsample,dX2+dXsample):
for iY in range(dY):
x = img[iY,iX]
line[iY] = x
accum[iY] += x
# plt.plot(line)
# plt.show()
cv2.line(imgCopy, (iX, 0), (iX, dY), (255, 255, 255), 1)
if bLoud:
cv2.imshow("w line", imgCopy)
plt.plot(accum)
plt.title("Raw accum")
plt.show()
accum /= (2 * dXsample)
if bLoud:
plt.title("Scaled back")
plt.plot(accum)
plt.draw()
# plt.show()
# finds negative peaks in the data
accum = accum * -1
peak = signal.find_peaks_cwt(accum,np.arange(1, 25))
for i in peak:
if bLoud:
print "at offset:", i, " value:", accum[i]
ii = accum[i] * -1
if ii < 64:
cv2.line(imgCopy, (0, i), (dX, i), (255, 0, 0), 1)
else:
if bLoud:
print "Point no good"
if bLoud:
cv2.imshow('test', imgCopy)
plt.show()
cv2.waitKey(0)
h, t = os.path.split(sImg)
sOut = t + ".out.jpg"
cv2.imwrite(sOut, imgCopy)
if bLoud:
print peak
dumpJson(sImg, sOut,dX, dY,peak)
# print "at x,y", iX, " ", iY, " ", img[iY, iX]
return
if __name__ == "__main__":
main()