-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2_image_processing.py
225 lines (192 loc) · 6.57 KB
/
d2_image_processing.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Image Processing before CNN training
import cv2
import numpy as np
import os
import glob
np.warnings.filterwarnings('ignore')
# NMS slow ver.
def non_max_suppression_slow(boxes, overlapThresh=0.5):
# if there are no boxes, return an empty list
if len(boxes) == 0:
return []
# initialize the list of picked indexes
pick = []
# grab the coordinates of the bounding boxes
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
# compute the area of the bounding boxes and sort the bounding
# boxes by the bottom-right y-coordinate of the bounding box
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = y2
# keep looping while some indexes still remain in the indexes
# list
while len(idxs) > 0:
# grab the last index in the indexes list, add the index
# value to the list of picked indexes, then initialize
# the suppression list (i.e. indexes that will be deleted)
# using the last index
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
suppress = [last]
# loop over all indexes in the indexes list
for pos in range(0, last):
# grab the current index
j = idxs[pos]
# find the largest (x, y) coordinates for the start of
# the bounding box and the smallest (x, y) coordinates
# for the end of the bounding box
xx1 = max(x1[i], x1[j])
yy1 = max(y1[i], y1[j])
xx2 = min(x2[i], x2[j])
yy2 = min(y2[i], y2[j])
# compute the width and height of the bounding box
w = max(0, xx2 - xx1 + 1)
h = max(0, yy2 - yy1 + 1)
# compute the ratio of overlap between the computed
# bounding box and the bounding box in the area list
overlap = float(w * h) / area[j]
# if there is sufficient overlap, suppress the
# current bounding box
if overlap > overlapThresh:
suppress.append(pos)
# delete all indexes from the index list that are in the
# suppression list
idxs = np.delete(idxs, suppress)
# return only the bounding boxes that were picked
return boxes[pick]
# Malisiewicz et al.
# NMS
def non_max_suppression_fast(boxes, overlapThresh=0.5):
# if there are no boxes, return an empty list
if len(boxes) == 0:
return []
# if the bounding boxes integers, convert them to floats --
# this is important since we'll be doing a bunch of divisions
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
# initialize the list of picked indexes
pick = []
# grab the coordinates of the bounding boxes
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
# compute the area of the bounding boxes and sort the bounding
# boxes by the bottom-right y-coordinate of the bounding box
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
# keep looping while some indexes still remain in the indexes
# list
while len(idxs) > 0:
# grab the last index in the indexes list and add the
# index value to the list of picked indexes
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
# find the largest (x, y) coordinates for the start of
# the bounding box and the smallest (x, y) coordinates
# for the end of the bounding box
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
# compute the width and height of the bounding box
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
# compute the ratio of overlap
overlap = (w * h) / area[idxs[:last]]
# delete all indexes from the index list that have
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
# return only the bounding boxes that were picked using the
# integer data type
return boxes[pick].astype("int")
idx = 0
path = 'F:/semigradpro/pic_pre/*'
images = glob.glob(path)
for fname in images:
print(fname+'\n')
keep = []
boxes = []
vec = []
threshold = 100
minLength = 80
lineGap = 5
rho = 1
limit = 150
# load image
img = cv2.imread(fname,cv2.IMREAD_COLOR)
try:
height, width = img.shape[:2]
except:
print('no shape')
break
# img = cv2.resize(img,(512,512),interpolation=cv2.INTER_CUBIC)
#graysacle
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# gray = cv2.bitwise_not(gray)
# Thresholding
# gray_pin = 196
# ret, thresh = cv2.threshold(denoised, gray_pin, 255, cv2.THRESH_BINARY)
kernel = np.ones((2,2), np.uint8)
close_kernel = np.ones((9, 5), np.uint8)
gradient = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)
mean_thres = cv2.adaptiveThreshold(gradient, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 3, 12)
close = cv2.morphologyEx(mean_thres,cv2.MORPH_CLOSE,close_kernel)
lines = cv2.HoughLinesP(close,rho,np.pi/180,threshold,minLength, lineGap)
try:
if(len(lines)>0):
for i in range(0,len(lines)):
vec = lines[i][0]
pt1 = (vec[0],vec[1])
pt2 = (vec[2],vec[3])
gapY = abs(vec[3]-vec[1])
gapX = abs(vec[2]-vec[0])
if(gapY>limit and limit >0):
cv2.line(close, pt1, pt2, (0,0,0), 10)
except:
print('no lines')
contours,hierarchy = cv2.findContours(close,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
boxes.append([x, y, w, h])
boxes = np.array(boxes)
keep = non_max_suppression_fast(boxes)
# keep = non_max_suppression_slow(boxes)
# print(len(boxes))
# print(len(keep))
# for b in boxes:
# x,y,w,h = b
# if (w>40 and h>10) and (w < width/2 and h < height/2):
# img1 = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
idx2 = 0
for k in keep:
x,y,w,h = k
if w>40 and h>10 and w < width/2 and h < height/2:
img2 = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
idx+=1
idx2+=1
new_img = img[y:y+h,x:x+w]
cv2.imwrite('./preprocessing/'+str(idx) + '.png', new_img)
cv2.imwrite('./preprocessing/whole'+str(idx2)+'.png',img)
# check contour image boxes
# cv2.imshow('1',img1)
# cv2.imshow('2',img2)
# try:
# cv2.imwrite('./temp_pre/'+str(idx)+'.png',img2)
# idx+=1
# except:
# print('no rectangle')
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# for cnt in contours:
# x, y, w, h = cv2.boundingRect(cnt)
# cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# if w>50 and h>50 and w < width/2 and h < height/2:
# idx+=1
# new_img=gradient[y:y+h,x:x+w]
# cv2.imwrite('./preprocessing/'+str(idx) + '.png', new_img)
print('Done')