-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonMax.py
148 lines (115 loc) · 4.88 KB
/
NonMax.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
#heterocl Canny Filter
from PIL import Image
import heterocl as hcl
import numpy as np
import math
import imageio
hcl.init(init_dtype=hcl.Float())
#image path
path = "home.jpg"
img = Image.open(path)
width,height = img.size
#initialize placeholders
A = hcl.placeholder((height,width,3), "A", dtype=hcl.Float())
Gx = hcl.placeholder((3,3),"Gx",dtype=hcl.Float())
Gy = hcl.placeholder((3,3),"Gy",dtype=hcl.Float())
hcl_A = hcl.asarray(np.asarray(img))
hcl_Gx = hcl.asarray(np.array([[1,0,-1],[2,0,-2],[1,0,-1]]))
hcl_Gy = hcl.asarray(np.array([[1,2,1],[0,0,0],[-1,-2,-1]]))
#output
hcl_img = hcl.asarray(np.zeros((height,width)))
#=======================================sobel_algo============================================
def sobel(A,Gx,Gy):
B = hcl.compute((height,width), lambda x,y: A[x][y][0]+A[x][y][1]+A[x][y][2],"B",dtype=hcl.Float())
r = hcl.reduce_axis(0,3)
c = hcl.reduce_axis(0,3)
D = hcl.compute((height,width), lambda x,y: hcl.select(hcl.and_(x>0, x<(height -1), y>0, y<(width-1)), hcl.sum(B[x+r,y+c]*Gx[r,c], axis=[r,c]),B[x, y]),"D",dtype=hcl.Float())
t = hcl.reduce_axis(0,3)
g = hcl.reduce_axis(0,3)
E = hcl.compute((height,width), lambda x,y: hcl.select(hcl.and_(x>0, x<(height -1), y>0, y<(width-1)), hcl.sum(B[x+t,y+g]*Gy[t,g], axis=[t,g]),B[x, y]), "E",dtype=hcl.Float())
return hcl.compute((height,width), lambda x,y:hcl.sqrt(D[x][y]*D[x][y]+E[x][y]*E[x][y])/4328*255,dtype=hcl.Float())
s = hcl.create_schedule([A,Gx,Gy],sobel)
f = hcl.build(s)
#call the function
f(hcl_A, hcl_Gx,hcl_Gy, hcl_img)
npImg = hcl_img.asnumpy()
#=======================================sobel_x==============================================
def sobel_x(A,Gx):
B = hcl.compute((height,width), lambda x,y: A[x][y][0]+A[x][y][1]+A[x][y][2],"B",dtype=hcl.Float())
r = hcl.reduce_axis(0,3)
c = hcl.reduce_axis(0,3)
return hcl.compute((height,width), lambda x,y: hcl.select(hcl.and_(x>0, x<(height -1), y>0, y<(width-1)), hcl.sum(B[x+r,y+c]*Gx[r,c], axis=[r,c]), B[x,y]),"X",dtype=hcl.Float())
sx = hcl.create_schedule([A,Gx],sobel_x)
fx = hcl.build(sx)
hcl_X = hcl.asarray(np.zeros((height,width)))
fx(hcl_A, hcl_Gx, hcl_X)
npX = hcl_X.asnumpy()
#=======================================sobel_y===============================================
def sobel_y(A,Gy):
B = hcl.compute((height,width), lambda x,y: A[x][y][0]+A[x][y][1]+A[x][y][2],"B",dtype=hcl.Float())
t = hcl.reduce_axis(0,3)
g = hcl.reduce_axis(0,3)
return hcl.compute((height,width), lambda x,y: hcl.select(hcl.and_(x>0, x<(height -1), y>0, y<(width-1)), hcl.sum(B[x+t,y+g]*Gy[t,g], axis=[t,g]), B[x, y]),"Y",dtype=hcl.Float())
sy = hcl.create_schedule([A,Gy],sobel_y)
fy = hcl.build(sy)
hcl_Y = hcl.asarray(np.zeros((height,width)))
fy(hcl_A, hcl_Gy, hcl_Y)
npY = hcl_Y.asnumpy()
theta = np.arctan2(npY,npX)
#===================================Non_Max===================================================
#Edge Direction
#placeholder for non_max
angle = hcl.placeholder((height,width),"angle")
Z = hcl.placeholder((height,width),"Z")
image = hcl.placeholder((height,width),"image")
for i in range(0,height-1):
for j in range(0,width-1):
theta[i][j] = theta[i][j]*180. / np.pi
if(theta[i][j]<0):
theta[i][j] = theta[i][j]+180
def non_max_suppression(image,angle,Z):
def loop_body(x,y):
q = 255
r = 255
c1 = hcl.and_((0 <= angle[x][y]), (angle[x][y] < 22.5))
c2 = hcl.and_((157.5 <= angle[x][y]) ,(angle[x][y] <= 180))
c3 = hcl.and_((22.5 <= angle[x][y]), (angle[x][y]< 67.5))
c4 = hcl.and_((67.5 <= angle[x][y]), (angle[x][y]< 112.5))
c5 = hcl.and_((112.5 <= angle[x][y]), (angle[x][y]< 157.5))
#angle 0
with hcl.if_ (hcl.or_(c1, c2)):
q = image[x][ y+1]
r = image[x][ y-1]
#angle 45
with hcl.elif_ (c3):
q = image[x+1][ y-1]
r = image[x-1][ y+1]
#angle 90
with hcl.elif_ (c4):
q = image[x+1][ y]
r = image[x-1,][y]
#angle 135
with hcl.elif_ (c5):
q = image[x-1, y-1]
r = image[x+1, y+1]
with hcl.if_ (hcl.and_((image[x,y] >= q),(image[x,y] >= r))):
Z[x][y] = image[x][y]
with hcl.else_():
Z[x][y] = 0
hcl.mutate(Z.shape, lambda x,y: loop_body(x,y), "M")
sd = hcl.create_schedule([image,angle,Z],non_max_suppression)
fd = hcl.build(sd)
hcl_D = hcl.asarray(np.zeros((height,width)))
hcl_theta = hcl.asarray(theta)
hcl_img = hcl.asarray(npImg)
fd(hcl_img, hcl_theta, hcl_D)
hpD = hcl_D.asnumpy()
#===============================================================================================
#output image
newimg = np.zeros((height,width,3))
for x in range(0, height):
for y in range(0, width):
for z in range(0,3):
newimg[x,y,z]=hpD[x,y]
newimg = newimg.astype(np.uint8)
imageio.imsave("home_canny.jpg",newimg)