Skip to content

Instantly share code, notes, and snippets.

@ramonesteban
Created February 28, 2013 15:23
Sección de código para detección de diagonales
def convolution(self, h, f):
F = self.open_image(self.image_file_path)
width, height = get_image_size(F)
k = len(h[1])
for x in range(width):
for y in range(height):
suma = 0
for i in range(k):
z1 = i - k/2
for j in range(k):
z2 = j - k/2
try:
suma += f.getpixel((x+z1, y+z2))[0]*h[i][j]
except:
pass
suma = int(suma)
F.putpixel((x, y), (suma, suma, suma))
return F
def detect_lines(self, image, imagenx, imageny):
width, height = get_image_size(image)
x_lines = imagenx.load()
y_lines = imageny.load()
dictionary = {}
complete_list = []
for i in range(width):
temp_list = []
for j in range(height):
x = x_lines[i, j][0]
y = y_lines[i, j][0]
if x == 0 and y == 0:
temp_list.append((None, None))
else:
angle = int(math.degrees(math.atan2(y, x)))
rho = abs((i)*math.cos(angle) + (j)*math.sin(angle))
if i > 0 and j > 0 and i < width and j < height:
if (rho, angle) in dictionary:
dictionary[(rho, angle)] += 1
else:
dictionary[(rho, angle)] = 1
temp_list.append((rho, angle))
complete_list.append(temp_list)
frecuency = {}
dictionary = sorted(dictionary.items(), key = lambda tupla: tupla[1], reverse = True)
for i in range(len(dictionary)):
(rho, angle) = dictionary[i][0]
frecuency[(rho, angle)] = dictionary[1]
x_counter = 0
y_counter = 0
d_counter = 0
for i in range(width):
for j in range(height):
if i > 0 and j > 0 and i < width and j < height:
rho, angle = complete_list[i][j]
if (rho, angle) in frecuency:
if angle == 0:
image.putpixel((i, j), (255, 0, 0))
x_counter += 1
elif angle == 90:
image.putpixel((i, j), (0, 0, 255))
y_counter += 1
else:
image.putpixel((i, j), (0, 255, 0))
d_counter += 1
print 'Vertical pixels:', x_counter
print 'Horizontal pixels:', y_counter
print 'Diagonal pixels:', d_counter
return image
def action(self):
f = self.open_image(self.image_file_path)
f = grayscale(f)
hx = [[-1, -1, -1], [2, 2, 2], [-1, -1, -1]]
hy = [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]]
imagex = self.convolution(hx, f)
imagey = self.convolution(hy, f)
imagex = binarization(imagex, 30)
imagey = binarization(imagey, 30)
image = self.detect_lines(f, imagex, imagey)
self.update_image(image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment