-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel-shortest-path.py
360 lines (288 loc) · 16.5 KB
/
pixel-shortest-path.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from cv2 import cv2
import numpy as np
import sys
np.set_printoptions(threshold = sys.maxsize) #Allows the full matrix to be displayed if needed
def main():
image = input("Type file name with file extension of image you'd like to test:")
greyImage = cv2.imread(image,0) #Saves the grey image input by the user to greyImage
while(greyImage is None): #If the user inputs an incorrect file name
image = input("The file you selected does not exist. Please type a file name with extension you'd like to test:")
greyImage = cv2.imread(image, 0) #Saves the grey image input by the user to greyImage
greyImageWidth = greyImage.shape[1] #Saves the number of horizontal pixels to greyImageWidth
greyImageHeight = greyImage.shape[0] #Saves the number of vertical pixels to greyImageHeight
#Initializes the V set. User inputs numbers between 0 and 255 one at a time until 'Done' is typed
V = set([])
vCheck = 0
while(vCheck == 0):
vInput = input("Type a value between 0 and 255 to be included in set V. Type 'Done' when no more values need to be added:")
try:
vInput = int(vInput) #Converts user input into an integer
if(vInput >= 0 and vInput <= 255):
V.add(vInput)
print("Set V contains:")
print(V)
else:
print("The entered value was above 255 or less than 0. ")
except ValueError:
if(vInput == 'Done'):
vCheck = 1
else:
print("The entered character was not an integer")
#Initializes the x and y pixel coordinates for p. If user input is not within pixel space or not an integer, an error is thrown and user is asked to input again.
xpCheck = 0
while(xpCheck == 0):
xpStart = input("Enter a valid x coordinate for p:")
try:
xpStart = int(xpStart)
if(xpStart < greyImageWidth and xpStart >= 0):
xpCheck = 1
else:
print("The entered character was too large or less than 0. ")
except ValueError:
print("The entered character was not an integer. ")
ypCheck = 0
while(ypCheck == 0):
ypStart = input("Enter a valid y coordinate for p:")
try:
ypStart = int(ypStart)
if(ypStart < greyImageHeight and ypStart >= 0):
ypCheck = 1
else:
print("The entered character was too large or less than 0. ")
except ValueError:
print("The entered character was not an integer. ")
#Initializes the x and y pixel coordinates for q. If user input is not within pixel space or not an integer, an error is thrown and user is asked to input again.
xqCheck = 0
while(xqCheck == 0):
xqEnd = input("Enter a valid x coordinate for q:")
try:
xqEnd = int(xqEnd)
if(xqEnd < greyImageHeight and xqEnd >= 0):
xqCheck = 1
else:
print("The entered character was too large or less than 0. ")
except ValueError:
print("The entered character was not an integer. ")
yqCheck = 0
while(yqCheck == 0):
yqEnd = input("Enter a valid y coordinate for q:")
try:
yqEnd = int(yqEnd)
if(yqEnd < greyImageHeight and yqEnd >= 0):
yqCheck = 1
else:
print("The entered character was too large or less than 0. ")
except ValueError:
print("The entered character was not an integer. ")
pathCheck = 0
while(pathCheck == 0):
print("1 - 4-path")
print("2 - 8-path")
print("3 - m-path")
path = input("Type a number for the pixel neighbor relationship you'd like to select:")
try:
path = int(path)
if(path <= 3 and path >= 1):
pathCheck = 1
else:
print("The entered character was not a valid option. ")
except ValueError:
print("The entered character was not an integer. ")
xPosition = 0
yPosition = 0
#List used to track x and y pixel coordinates that have intensities in set V
xyList = []
matrix = np.zeros(shape=(greyImageHeight,greyImageWidth)) #Creates matrix to evaluate set of pixels with intensities contained within set V
while(xPosition < greyImageWidth): #Loops through each row
while(yPosition < greyImageHeight): #Loops through each column
centerPixel = int(greyImage[yPosition, xPosition]) #Sets centerPixel to number for the intensity for the current x and y pixel position
#If the current pixel intensity in centerPixel is not in set V, set the value at the same x and y pixel position in the matrix to 0.
#Otherwise, set the value in the matrix to 999 and add the current x and y positions to their lists.
if(centerPixel not in V):
matrix[yPosition][xPosition] = 0
else:
xyList.append([xPosition,yPosition])
matrix[yPosition][xPosition] = 999
yPosition = yPosition + 1 #Increment Y position by 1
yPosition = 0
xPosition = xPosition + 1 #Increment X position by 1
#Evaluation begins by setting the beginning pixel position to the user inputs for the x and y coordinates of p.
xp = xpStart
yp = ypStart
listLength = len(xyList)
listSearch = 0
#The following functions are used to check if the pixel locations in the matrix for p and q are valid and if p and q match.
searchComplete = 0
pqMatch = 0
if(matrix[ypStart][xpStart] == 0):
searchComplete = 1
if(matrix[yqEnd][xqEnd] == 0):
searchComplete = 1
if(xpStart == xqEnd and ypStart == yqEnd and matrix[ypStart][xpStart] == 999):
pqMatch = 1
searchComplete = 1
#Since the position for p is initialed with 0 in the matrix, stepCount is used to mark any vaild neighbors with 1 + the current pixel location value in the matrix
#if the neighbor's current value is greater than the value of stepCount
matrix[yp][xp] = 0
stepCount = 1
if(path == 1): #If the user selected '4- path'
while(searchComplete == 0):
#The following functions check to see if the neighbor is valid and the current value is greater than stepCount.
#If so, reassign the value of the neighbor to stepCount.
if(yp - 1 >= 0 and matrix[yp - 1][xp] > stepCount):
matrix[yp - 1][xp] = stepCount
if (xp + 1 < greyImageWidth and matrix[yp][xp + 1] > stepCount):
matrix[yp][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and matrix[yp + 1][xp] > stepCount):
matrix[yp + 1][xp] = stepCount
if (xp - 1 >= 0 and matrix[yp][xp - 1] > stepCount):
matrix[yp][xp - 1] = stepCount
#The following function removes the current x and y coordinates for the currrent pixel position from the xyList since its neighbors have been evaluated
removalComplete = 0
listLength = len(xyList)
listSearch = 0
while(removalComplete == 0 and listSearch < listLength):
if(xyList[listSearch][0] == xp and xyList[listSearch][1] == yp):
xyList.pop(listSearch)
removalComplete = 1
else:
listSearch = listSearch + 1
listLength = len(xyList)
listSearch = 0
#The following function evaluates the xyList to determine if the coordinates of xq and yq are still in the list. If not, the search is complete
searchComplete = 1
while(listSearch < listLength):
if(xyList[listSearch][0] == xqEnd and xyList[listSearch][1] == yqEnd):
searchComplete = 0
listSearch = listSearch + 1
#The following function is a comparison between the matrix equivalents of the coordinates remaining in the xyList and the current matrixValue
#If the evaluated matrix value in the matrix coordinate is less than the current matrixValue, that coordinate becomes the next xp and yp to be evaluated.
matrixValue = 999
listLength = len(xyList)
listSearch = 0
while (listSearch < listLength):
if (int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) < matrixValue and int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) > 0):
matrixValue = int(matrix[xyList[listSearch][1]][xyList[listSearch][0]])
xp = xyList[listSearch][0]
yp = xyList[listSearch][1]
stepCount = matrixValue + 1
listSearch = listSearch + 1
if (listSearch == listLength and matrixValue == 999): #If the list has been fully evaluated and the largest value remaining in the matrix is 999, the search is complete
searchComplete = 1
if(path == 2): #If the user selected '8-path'
while (searchComplete == 0):
# The following functions check to see if the neighbor is valid and the current value is greater than stepCount.
# If so, reassign the value of the neighbor to stepCount.
if (yp - 1 >= 0 and matrix[yp - 1][xp] > stepCount):
matrix[yp - 1][xp] = stepCount
if (xp + 1 < greyImageWidth and matrix[yp][xp + 1] > stepCount):
matrix[yp][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and matrix[yp + 1][xp] > stepCount):
matrix[yp + 1][xp] = stepCount
if (xp - 1 >= 0 and matrix[yp][xp-1] > stepCount):
matrix[yp][xp - 1] = stepCount
if (yp - 1 >= 0 and xp - 1 >= 0 and matrix[yp - 1][xp - 1] > stepCount):
matrix[yp - 1][xp - 1] = stepCount
if (yp - 1 >= 0 and xp + 1 < greyImageWidth and matrix[yp - 1][xp + 1] > stepCount):
matrix[yp - 1][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and xp + 1 < greyImageWidth and matrix[yp + 1][xp + 1] > stepCount):
matrix[yp + 1][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and xp - 1 >= 0 and matrix[yp + 1][xp - 1] > stepCount):
matrix[yp + 1][xp - 1] = stepCount
# The following function removes the current x and y coordinates for the currrent pixel position from the xyList since its neighbors have been evaluated
removalComplete = 0
listLength = len(xyList)
listSearch = 0
while(removalComplete == 0 and listSearch < listLength):
if(xyList[listSearch][0] == xp and xyList[listSearch][1] == yp):
xyList.pop(listSearch)
removalComplete = 1
else:
listSearch = listSearch + 1
listLength = len(xyList)
listSearch = 0
#The following function evaluates the xyList to determine if the coordinates of xq and yq are still in the list. If not, the search is complete
searchComplete = 1
while(listSearch < listLength):
if(xyList[listSearch][0] == xqEnd and xyList[listSearch][1] == yqEnd):
searchComplete = 0
listSearch = listSearch + 1
#The following function is a comparison between the matrix equivalents of the coordinates remaining in the xyList and the current matrixValue
#If the evaluated matrix value in the matrix coordinate is less than the current matrixValue, that coordinate becomes the next xp and yp to be evaluated.
matrixValue = 999
listLength = len(xyList)
listSearch = 0
while (listSearch < listLength):
if (int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) < matrixValue and int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) > 0):
matrixValue = int(matrix[xyList[listSearch][1]][xyList[listSearch][0]])
xp = xyList[listSearch][0]
yp = xyList[listSearch][1]
stepCount = matrixValue + 1
listSearch = listSearch + 1
if (listSearch == listLength and matrixValue == 999):
searchComplete = 1
if(path == 3): #If the user selected 'm-path'
while (searchComplete == 0):
# The following functions check to see if the neighbor is valid and the current value is greater than stepCount.
# If so, reassign the value of the neighbor to stepCount.
fourCheck = 0 #Used to check if any of the '4-path' directions have been evaluated
if (yp - 1 >= 0 and matrix[yp - 1][xp] > stepCount):
matrix[yp - 1][xp] = stepCount
fourCheck = 1
if (xp + 1 < greyImageWidth and matrix[yp][xp + 1] > stepCount):
matrix[yp][xp + 1] = stepCount
fourCheck = 1
if (yp + 1 < greyImageHeight and matrix[yp + 1][xp] > stepCount):
matrix[yp + 1][xp] = stepCount
fourCheck = 1
if (xp - 1 >= 0 and matrix[yp][xp-1] > stepCount):
matrix[yp][xp-1] = stepCount
fourCheck = 1
if (yp - 1 >= 0 and xp - 1 >= 0 and fourCheck == 0 and matrix[yp - 1][xp - 1] > stepCount):
matrix[yp - 1][xp - 1] = stepCount
if (yp - 1 >= 0 and xp + 1 < greyImageWidth and fourCheck == 0 and matrix[yp - 1][xp + 1] > stepCount):
matrix[yp - 1][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and xp + 1 < greyImageWidth and fourCheck == 0 and matrix[yp + 1][xp + 1] > stepCount):
matrix[yp + 1][xp + 1] = stepCount
if (yp + 1 < greyImageHeight and xp - 1 >= 0 and fourCheck == 0 and matrix[yp + 1][xp - 1] > stepCount):
matrix[yp + 1][xp - 1] = stepCount
# The following function removes the current x and y coordinates for the currrent pixel position from the xyList since its neighbors have been evaluated
removalComplete = 0
listLength = len(xyList)
listSearch = 0
while(removalComplete == 0 and listSearch < listLength):
if(xyList[listSearch][0] == xp and xyList[listSearch][1] == yp):
xyList.pop(listSearch)
removalComplete = 1
else:
listSearch = listSearch + 1
listLength = len(xyList)
listSearch = 0
#The following function evaluates the xyList to determine if the coordinates of xq and yq are still in the list. If not, the search is complete
searchComplete = 1
while(listSearch < listLength):
if(xyList[listSearch][0] == xqEnd and xyList[listSearch][1] == yqEnd):
searchComplete = 0
listSearch = listSearch + 1
#The following function is a comparison between the matrix equivalents of the coordinates remaining in the xyList and the current matrixValue
#If the evaluated matrix value in the matrix coordinate is less than the current matrixValue, that coordinate becomes the next xp and yp to be evaluated.
matrixValue = 999
listLength = len(xyList)
listSearch = 0
while (listSearch < listLength):
if (int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) < matrixValue and int(matrix[xyList[listSearch][1]][xyList[listSearch][0]]) > 0):
matrixValue = int(matrix[xyList[listSearch][1]][xyList[listSearch][0]])
xp = xyList[listSearch][0]
yp = xyList[listSearch][1]
stepCount = matrixValue + 1
listSearch = listSearch + 1
if (listSearch == listLength and matrixValue == 999):
searchComplete = 1
answer = int(matrix[yqEnd][xqEnd])
if(int(matrix[yqEnd][xqEnd]) == 999 or answer < 1 and pqMatch == 0):#If the value for q in the matrix is 999 or 0, q was not evaluated and there is no path between p and q
print("The path does not exist between point p and point q")
else:#If q was evaluated, the path beween p and q is the value of q in the matrix
print('The length of the shortest path is: ' + str(answer))
print(matrix)#Shows the evaluated matrix with 1 being the starting position and sequentially increasing with each neigboring pixel to q
if __name__ == "__main__":
main()