-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_grad_edges.py
180 lines (148 loc) · 3.18 KB
/
test_grad_edges.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
import cv2
import numpy as np
import glob as gb
from book_hough import *
import newfcns as nf
import matplotlib.pyplot as plt
'''
Test some new functions for
Find books in a bookshelf image
'''
def draw_gaps(img, gaps, gsc=1):
i=0
for g in gaps:
i+=1
x1 = g[0]*gsc
x2 = g[1]*gsc
col = (0,255,0)
y1 = 0
y2 = img_height*gsc
y3 = int(gsc*img_height/2)+10*(i%5)
cv2.line(img, (x1,y1),(x1,y2), col ,2)
cv2.line(img, (x2,y1),(x2,y2), col ,2)
cv2.line(img, (x1,y3),(x2,y3), col ,8)
#
def drawv3(img, cross):
for c in cross:
if c < 0: # start of a book
col = (255,255,255)
c = -c
else:
col = (0,255,0)
x1 = c
x2 = c
y1 = 0
y2 = img_height
cv2.line(img, (x1,y1),(x2,y2), col ,2)
#
def drawv2(img, xvals):
for x in xvals:
x1 = x
x2 = x
y1 = 0
y2 = img_height
cv2.line(img, (x1,y1),(x2,y2), (255,255,255),2)
#
#
# start of test
##
#
# read in the image
#
pic_filename='tiny/topshelf001.jpg'
pic_filename='tiny/target.jpg'
img = cv2.imread(pic_filename, cv2.IMREAD_COLOR)
ish = img.shape
#
# scale the image
#
# scale factor imported from newfcns.py
#
img_width = int(ish[1]/nf.scale)
img_height = int(ish[0]/nf.scale)
####################
#
# save time - read line of labels (K-means)
#
l1 = []
f = open('ctrans_labels.csv','r')
for row in f:
y = row.split(',')[1]
l1.append(float(y))
line = np.array(l1)
f = open('metadata.txt','r')
for l in f:
txt, val = l.split(',')
f.close()
black_label=int(val)
#
# plot a graph of original and gradient
x = range(len(line))
fig, ax = plt.subplots()
#print(':::: sline: ',type(sline), np.shape(sline))
ax = plt.plot(x,line)
#
# Get derivative of labels
#
# deriv_win_size in newfcns
#
win = int(nf.deriv_win_size/nf.scale)
if win%2==0:
win+= 1
for w in [win]:
print('estimating derivative of {} labels using {} window'.format(len(line),w))
grad = nf.Est_derivative(line, w)
gs = []
for g in grad:
gs.append( g)
ax = plt.plot(x,gs)
#
# Get the Zero Crossings of label deriv.
#
cross = nf.Find_crossings(gs)
#
# Plot + and - edges on curve
#
no = -2
yes = 8
xm = []
ym = []
x0 = 0
y0 = no
for c in cross:
if c < 0: ## start
c = -c
y1 = yes
else:
y1 = no
x1 = c
x2 = c
xm.extend([x0, x1, x2])
ym.extend([y0, y0, y1])
x0 = x1
y0 = y1
ax = plt.plot(xm,ym)
plt.title('line and its gradients')
plt.grid(True)
plt.show()
#
# study gaps btwn edges for likely books
#
#
# identify black (background)
# and skinny gaps
# "6" = black
gaplist = nf.Gen_gaplist(cross)
print('There are {} gaps'.format(len(gaplist)))
print (gaplist)
cands = nf.Gap_filter(gaplist, img, tmin=int(40/nf.scale), blacklabel=black_label)
print('After filtering:')
print('There are {} gaps left'.format(len(cands)))
############################################################
#
# Display book boundaries on image
#
draw_gaps(img,cands,nf.scale)
cv2.imshow("New Book Boundaries", img)
cv2.waitKey()
cv2.destroyAllWindows()