Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. from PIL import Image
  2. from numpy import array
  3. import cv2 as cv
  4. import numpy as np
  5. import math
  6. from time import time
  7.  
  8. def main():
  9. t_inicial = time()
  10.  
  11. im = Image.open('Gray_figuras.png')
  12. im.show()
  13. arr_gry = array(im)
  14. (i,j) = (arr_gry.shape[0],arr_gry.shape[1])
  15. arr_gra = np.zeros(shape = (arr_gry.shape[0],arr_gry.shape[1]))
  16. arr_bor = np.zeros(shape = (arr_gry.shape[0],arr_gry.shape[1]))
  17. # mask_Robx = ([0,1],[-1,0])
  18. # mask_Roby = ([-1,0],[0,-1])
  19. mask_Prewx = array([(-1,0,1),(-1,0,1),(-1,0,1)])
  20. mask_Prewy = array([(1,1,1),(0,0,0),(-1,-1,-1)])
  21. a = arr_gry #solo le cambio el nombre
  22. #Barrido para la mascara Prewitt
  23.  
  24. # Me comere las lineas de las imagnes y las reconstruire duplicando valores
  25. n = 1
  26. m = 1
  27. for n in range(i-1): #Barremos por filas
  28. for m in range(j-1): #Barremos por columnas
  29.  
  30. p_z_9 = array([(a[n-1,m-1],a[n-1,m],a[n-1,m+1]),(a[n,m-1],a[n,m],a[n,m+1]),(a[n+1,m-1],a[n+1,m],a[n+1,m+1])])
  31. gx = np.sum(p_z_9*mask_Prewx)
  32. gy = np.sum(p_z_9*mask_Prewy)
  33. #edge_mag(gx,gy,n,m)
  34. xm = pow(gx,2)
  35. ym = pow(gy,2)
  36. g = int(math.sqrt(xm+ym))
  37. #arr_gra[n,m] = g
  38. if g > 135: #Este numero sirve de umbral para quitar ruido
  39. g = 255 #Si el gradiente es mayor a 255 entonces es blanco
  40. elif g < 0: #Si el gradiente es menor a 0 entonces es negro
  41. g = 0
  42. else: #Este tambien sirve para quitar ruido
  43. g = 0
  44. arr_bor[n,m] = g #Si es borde valdra 255 si no lo es sera 0 para fondo; ¡a sí! con esto construimos la imagen de bordes.
  45.  
  46. cv.imwrite("bord_figuras.png",arr_bor)
  47. im_cv = cv.LoadImage("bord_n_50.png")
  48. im_b = Image.open('bord_figuras.png')
  49. im_b.show()
  50. cv.ShowImage('imagen',im_cv)
  51. cv.WaitKey(0)
  52.  
  53. t_final = time()
  54. t_total = t_final - t_inicial
  55. print "Tiempo de convolución: ",t_total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement