-
Notifications
You must be signed in to change notification settings - Fork 0
/
demoEdgeDetector.py
219 lines (184 loc) · 7.74 KB
/
demoEdgeDetector.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
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
import pdb
def isMoreWhite(edges, row, col, colMax):
for i in range (col, colMax):
if edges[row][i] == 255:
return True
return False
# Funciton for finding last index of white
def findLastIndexOfWhite(edges, row, col, colMax):
lastIndex = 0
for i in range (col, colMax):
if edges[row][i] == 255:
lastIndex = i
return (lastIndex)
def findLastIndexOfUsefulBlack(edges, row, col, colMax, contourthickness):
lastIndex = 0
for i in range (col, colMax):
if edges[row][i] == 255:
lastIndex = i
if not isMoreWhite(edges, row, i, colMax):
break
return lastIndex-contourthickness
def ifLineHasWhite(edges, row, col = 0):
colMax = edges.shape[1]
whereFound = []
for i in range (col, colMax):
if edges[row][i] == 255:
whereFound.append(i)
return whereFound
# Resize while maintaining the ratio
def resizeImage(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the original Image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
else:
# calculate the ratio of the width and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
def ifLineHas (edges, row, col=0, returnSoon = False, condition=230):
for i in range(0, edges.shape[1]):
if edges[row][i][0] == condition or edges[row][i][2] == condition or edges[row][i][2] == condition:
print (edges[row][i])
#print (edges[row][i])
tempfiles = os.listdir("DemoResult/DemoEdgeDetectionTests/Originals")
files = []
for eachfile in tempfiles:
if eachfile.endswith(".jpg") or eachfile.endswith(".jpeg") or eachfile.endswith(".png"):
files.append(eachfile)
print(files)
for eachfile in files:
image = cv2.imread("DemoResult/DemoEdgeDetectionTests/Originals/" + eachfile)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = resizeImage(image, height=500)
blur = cv2.bilateralFilter(image,27,25,25)
edges = cv2.Canny(blur, 27, 27)
"""
# Normalize white within the boundary
for i in range (0, edges.shape[0]):
firstwhitefound = False
lastIndex = 0
for j in range (0, edges.shape[1]):
# If first white pixel is found, find out the last index inthe row where white is present
if edges[i][j] == 255 and not firstwhitefound:
firstwhitefound = True
lastIndex = findLastIndexOfWhite(edges, i, j, edges.shape[1])
# Convert to white as long as the line is within range
if not edges[i][j] == 255 and firstwhitefound and j <=lastIndex:
edges[i][j] = 255
"""
# transpose and do the same
edges = edges.transpose()
for i in range (0, edges.shape[0]):
firstwhitefound = False
lastIndex = 0
for j in range (0, edges.shape[1]):
# If first white pixel is found, find out the last index inthe row where white is present
if edges[i][j] == 255 and not firstwhitefound:
firstwhitefound = True
lastIndex = findLastIndexOfWhite(edges, i, j, edges.shape[1])
# Convert to white as long as the line is within range
if not edges[i][j] == 255 and firstwhitefound and j <=lastIndex:
edges[i][j] = 255
edges = edges.transpose()
contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Extracting max area
areas = []
for contour in contours:
ar = cv2.contourArea(contour)
areas.append(ar)
# Setting up final contour
max_area = max(areas)
max_area_index = areas.index(max_area)
finalContour = contours[max_area_index]
edges = np.zeros((edges.shape), np.uint8)
contourthickness = 6
edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGRA)
blur = cv2.cvtColor(blur, cv2.COLOR_GRAY2BGRA)
cv2.drawContours(edges, [finalContour], 0, (0, 0, 230), contourthickness, maxLevel = 0)
for i in range (0, edges.shape[0]):
for j in range (0, edges.shape[1]):
if edges[i][j][0] == 0 and edges[i][j][1] == 0 and edges[i][j][2] == 0:
edges[i][j][3] = 0
else:
edges[i][j][3] = 255
numpy_horizontal_concat = np.concatenate((blur, edges), axis=1)
newImageName = eachfile[:eachfile.index(".")] + "stepped.png"
cv2.imwrite("DemoResult/DemoEdgeDetectionTests/" + newImageName, numpy_horizontal_concat)
print ("Saved file: " + newImageName)
# Older code. Might be useful later
"""
# get contours
contours, hierarchy = cv2.findContours(closing,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow("Test", closing)
cv2.waitKey(0)
# Extracting max area
areas = []
for contour in contours:
ar = cv2.contourArea(contour)
areas.append(ar)
# Setting up final contour
max_area = max(areas)
max_area_index = areas.index(max_area)
finalContour = contours[max_area_index]
finalImage = np.empty((imageGrey.shape))
cv2.drawContours(finalImage, [finalContour], 0, (255, 255, 255), 10, maxLevel = 0)
finalImage = (255-finalImage)
# Just checking
numpy_horizontal_concat = np.concatenate((imageGrey, closing), axis=1)
numpy_horizontal_concat = np.concatenate((numpy_horizontal_concat, finalImage), axis=1)
newImageName = eachfile[:eachfile.index(".")] + "stepped.png"
cv2.imwrite(newImageName, numpy_horizontal_concat)
"""
"""
closing1 = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) #this is for further removing small noises and holes in the image
print (type(closing))
# Work with pixels
for j in range (5, closing.shape[0]-5):
blackfound = False
whitefound = False
blackOutlineDone = False
for k in range (5, closing.shape[1]-5):
# If found black
if closing[j][k] == 0:
blackfound = True
whitefound = False
else:
whitefound = True
blackfound = False
# If black is found
if blackfound:
if closing[j][k-5] == 0:
blackOutlineDone = True
elif closing[j][k+5] == 255:
blackOutlineDone = False
if blackOutlineDone:
closing[j][k] = 255
for k in range (5, closing.shape[1]-5):
continue
# To show next to each other
#print (image.shape)
#print (grey_3_channel.shape)
grey_3_channel1 = cv2.cvtColor(closing1, cv2.COLOR_GRAY2BGR)
grey_3_channel2 = cv2.cvtColor(closing, cv2.COLOR_GRAY2BGR)
numpy_horizontal_concat = np.concatenate((image, grey_3_channel1), axis=1)
numpy_horizontal_concat = np.concatenate((numpy_horizontal_concat, grey_3_channel2), axis=1)
# Writing image
newImageName = eachfile[:eachfile.index(".")] + "stepped.png"
cv2.imwrite(newImageName, numpy_horizontal_concat)
"""