-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsoc.py
64 lines (53 loc) · 1.73 KB
/
gsoc.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
import numpy as np
import cv2
from collections import deque
#solve function takes input image as the parameter ,
#performs BFS on the image to detect nearest edge distance for every cell
#and stores this distance in the res matrix which is finally returned as output.
def solve(A):
q = deque()
r = len(A)
c = len(A[0])
map = {}
res = []
for i in range(r):
t = [-1]*c
res.append(t)
for i in range(r):
for j in range(c):
if A[i][j]==255:
res[i][j]=0
q.append((i,j))
while q:
src = q.popleft()
i , j = src[0],src[1]
if not (src in map):
map[src]=1
d = res[i][j]
if i>0 and res[i-1][j]==-1 :
res[i-1][j] = d+1
q.append((i-1,j))
if i<r-1 and res[i+1][j]==-1 :
res[i+1][j] = d+1
q.append((i+1,j))
if j>0 and res[i][j-1]==-1 :
res[i][j-1] = d+1
q.append((i,j-1))
if j<c-1 and res[i][j+1]==-1 :
res[i][j+1] = d+1
q.append((i,j+1))
return res
#Reading the image
img=cv2.imread('Building.jpg',0)
print(img.shape)
#Peforming inbuilt canny edge detection
canny=cv2.Canny(img,100,200)
cv2.imshow("cannyedge",canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
res=solve(canny)
#Asking for input
val1 = input("Enter your value of x coordinate: ")
val2=input("Enter the value of y coordinate: ")
#Displaying the distance of nearest edge from the specified coordinate.
print(res[int(val1)][int(val2)])