-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdge_Detection.py
52 lines (38 loc) · 1.66 KB
/
Edge_Detection.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
import numpy as np
import matplotlib.pyplot as plt
import cv2
#define the vertical filter
vertical_filter = [[-1,-2,-1], [0,0,0], [1,2,1]]
#define the horizontal filter
horizontal_filter = [[-1,0,1], [-2,0,2], [-1,0,1]]
#read in the pinwheel image
img = plt.imread('/home/ryan/Documents/Galaxy_Zoo/Jell_Test/Train/Poss_Jellies/880993.jpg')#color jelly
#img = plt.imread('/home/ryan/Documents/Galaxy_Zoo/Jell_Test/Train/Not_Jellies/680527.jpg')#color non jelly
#Apply gaussian filter to smooth image
kernel = np.ones((5,5),np.float32)/50
img = cv2.filter2D(img,-1,kernel)
#get the dimensions of the image
n,m,d = img.shape
#initialize the edges image
edges_img = img.copy()
#loop over all pixels in the image
for row in range(3, n-2):
for col in range(3, m-2):
#create little local 3x3 box
local_pixels = img[row-1:row+2, col-1:col+2, 0]
#apply the vertical filter
vertical_transformed_pixels = vertical_filter*local_pixels
#remap the vertical score
vertical_score = vertical_transformed_pixels.sum()/4
#apply the horizontal filter
horizontal_transformed_pixels = horizontal_filter*local_pixels
#remap the horizontal score
horizontal_score = horizontal_transformed_pixels.sum()/4
#combine the horizontal and vertical scores into a total edge score
edge_score = (vertical_score**2 + horizontal_score**2)**.5
#insert this edge score into the edges image
edges_img[row, col] = [edge_score]*3
#remap the values in the 0-1 range in case they went out of bounds
edges_img = edges_img/edges_img.max()
plt.imshow(edges_img)
plt.show()