-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge_detection.py
executable file
·91 lines (86 loc) · 2.7 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
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
#!/usr/bin/python2
#import cv2
from random import choice
import matplotlib.pyplot as plt
import numpy as np
from random import randint
import matplotlib.cm as cm
from sys import argv
from abrir_imagen import *
import math
import matplotlib.pyplot as plt
from medias import * #filters
import math
from dfs_and_vec import * #subroutine for dfs
from detectar_cuadro import * #detect box
img2=gray_scale(argv[1])
img=median(img2)
ancho=len(img)
alto=len(img[0])
Sx=[[-1,0,1],[-2,0,2],[-1,0,1]] # Masks for the Sobel 3 3 3 operator
Sy=[[1,2,1],[0,0,0],[-1,-2,-1]]
mask_long=len(Sx) #gettng mask length
print "ancho: ",ancho
print "alto: ",alto
angulos=[]
#convulucion_discreta
#blancos con ultimo valor en 0
def recorrer(): #getting gradient magnitudes
magnitudes=[] #vector for magnitudes
for i in xrange(1,ancho-1):
for j in xrange(1,alto-1):
sumax=0.0
sumay=0.0
for m in xrange(mask_long):
for b in xrange(mask_long):
if (i+m)-1<ancho and (j+b)-1<alto: #validating borders of matrix, working only on central pixels
sumax+=img[(i+m)-1][(j+b)-1]*Sx[m][b] #getting Gx
sumay+=img[(i+m)-1][(j+b)-1]*Sy[m][b] #Getting Gy
#resultado=abs(sumax)+abs(sumay) #Getting magnitudes by using absolute value
resultado=math.sqrt(pow(sumax,2)+pow(sumay,2))
radians=math.atan2(sumay,sumax)
angulos.append((radians*180)/math.pi)
#if resultado>255: #validating pixel ranges
# resultado=255
magnitudes.append(resultado)
#nuevo_array[i][j]=abs(sumax)+abs(sumay)
return magnitudes
histo=recorrer() #getting magnitudes
#print "histo->\n",histo
def frecuencia(arr): #getting frecuency
val=[]
freq={}
for j in xrange(len(arr)):
val.append(arr[j])
if arr[j] in val:
if arr[j] in freq:
freq[arr[j]]+=1
else:
freq[arr[j]]=1
return freq
frecuencias=frecuencia(histo)
def prom(elm): #Getting average
suma=0.0
for u in elm:
suma+=u
return suma/len(elm)
#caj=cajas(frecuencias) # Getting boxes
#promedio=prom(frecuencias)
#promedio=prom(histo)
#print "caj: ",caj
#print "prom: ", promedio
longitud_magn=len(histo)
threshold=otsu_t2(frecuencias,ancho*alto)
print threshold
r=0
pixeles=[]
for i in xrange(1,ancho-1): #Detecting edges by using previously calculated threshold
for j in xrange(1,alto-1):
if histo[r]>threshold:
img[i][j]=255
pixeles.append((i,j))
else:
img[i][j]=0
r+=1
plt.imshow(img,cmap = cm.Greys_r) #Drawing modified image with detected shapes
plt.show()