Skip to content

Commit 3103151

Browse files
filename change
1 parent 1774724 commit 3103151

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

2d_edge_detection.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
b_img = np.zeros((200,200),dtype='uint8')
6+
w_img = np.ones((200,200),dtype='uint8')
7+
w_img = w_img * 255
8+
u_img = cv2.hconcat([b_img,w_img])
9+
l_img = cv2.hconcat([w_img,b_img])
10+
11+
img = cv2.vconcat([u_img,l_img])
12+
13+
img = cv2.imread('/home/prasadmatta/Desktop/CNN/2.jpg',0)
14+
15+
print('input image',img.shape)
16+
vertical_kernal = [[1,0,-1],[1,0,-1],[1,0,-1]]
17+
horizontal_kernal = [[1,1,1],[0,0,0],[-1,-1,-1]]
18+
sobel_filter = [[1,0,-1],[2,0,-2],[1,0,-1]]
19+
20+
p_val = 1
21+
for i in range(4):
22+
img = np.rot90(img)
23+
pad_val = np.zeros((p_val,img.shape[1]),dtype=np.uint8)
24+
img = cv2.vconcat([img,pad_val])
25+
26+
class EDGE:
27+
28+
def __init__(self,img,kernal,strides):
29+
30+
self.img = img
31+
self.height,self.width = self.img.shape
32+
self.kernal = np.array(kernal)
33+
self.strides = strides
34+
self.sample = np.array([[1,1,1],[1,1,1],[1,1,1]])
35+
36+
def vertical(self):
37+
38+
self.gen_img = []
39+
for ht in range(0,self.height-2,self.strides):
40+
row =[]
41+
for wdt in range(0,self.width-2,self.strides):
42+
# print(self.img[ht:ht+3,wdt:wdt+3])
43+
n_img = self.img[ht:ht+3,wdt:wdt+3] * self.kernal
44+
# print(n_img)
45+
row.append(sum(list(map(lambda x :sum(x),n_img))))
46+
# print(self.sample*sum(list(map(lambda x :sum(x),n_img))))
47+
# row.append(self.sample*sum(list(map(lambda x :sum(x),n_img))))
48+
self.gen_img.append(row)
49+
self.gen_img = np.array(self.gen_img,dtype='uint8')
50+
return self.gen_img
51+
52+
for i in ['vertical_kernal','horizontal_kernal']:
53+
k = eval(i)
54+
edge_vertical = EDGE(img,k,2)
55+
vertical_img = edge_vertical.vertical()
56+
print('output',img.shape,vertical_img.shape)
57+
cv2.imshow('{0}'.format(str(i)),vertical_img)
58+
cv2.waitKey(0)
59+
cv2.destroyAllWindows()
60+

0 commit comments

Comments
 (0)