Advertisement
Guest User

cresc regioes

a guest
Feb 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import numpy as np
  2. from collections import deque
  3.  
  4.  
  5.  
  6.  
  7. class CrescimentoRegioes:
  8.    
  9.     def __init__(self, imagens):
  10.         self.imagens = imagens
  11.        
  12.        
  13.     def obterSemente(self, image):
  14.  
  15.         # A semente será um retângulo no centro
  16.        
  17.         w, h = image.shape
  18.         p = 10. # p é uma porcentagem da altura e da largura
  19.        
  20.         pos_ini_x_mrk = int(w/2 - p*w/1000.)
  21.         pos_ini_y_mrk = int(h/2 - p*h/1000.)
  22.         pos_fim_x_mrk = int(w/2 + p*w/1000.)
  23.         pos_fim_y_mrk = int(h/2 + p*h/1000.)
  24.    
  25.         # Semente é uma imagem do mesmo tamanho que img, contendo zeros
  26.         semente = np.zeros(shape=(w,h), dtype=np.uint8)
  27.         # acrescenta um retângulo central de pixels = 255
  28.         semente[pos_ini_x_mrk:pos_fim_x_mrk, pos_ini_y_mrk:pos_fim_y_mrk] = 255
  29.        
  30.         return semente
  31.    
  32.     def vizinhos(self, x, y, w, h):
  33.        
  34.         lista = deque()
  35.        
  36.         pontos = [(x-1,y), (x+1, y), (x,y-1), (x,y+1),
  37.                   (x-1,y+1), (x+1, y+1), (x-1,y-1), (x+1,y-1),
  38.                  ]
  39.         for p in pontos:
  40.             if (p[0]>=0 and p[1]>=0 and p[0]<w and p[1]<h):
  41.                 lista.append((p[0], p[1]))
  42.                
  43.         return lista
  44.        
  45.     def crescerRegiao(self, image, reg, epsilon):
  46.        
  47.        
  48.         w, h = image.shape
  49.        
  50.         fila = deque()
  51.         for x in range(w):
  52.             for y in range(h):
  53.                 if reg[x,y]==255:
  54.                     fila.append((x,y))
  55.            
  56.         while fila:
  57.             ponto = fila.popleft()
  58.             x = ponto[0]
  59.             y = ponto[1]
  60.                
  61.             v_list = self.vizinhos(x, y, w, h)
  62.             for v in v_list:
  63.                 v_x = v[0]
  64.                 v_y = v[1]
  65.                 if( (reg[v_x][v_y]!=255) and (abs(image[x][y]-image[v_x][v_y])<epsilon)):
  66.                     reg[v_x][v_y] = 255
  67.                     fila.append((v_x,v_y))
  68.            
  69.         print("")
  70.         return reg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement