Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. import sys
  4. sys.setrecursionlimit(30000)
  5. class ConnectedComponent():
  6.  
  7. def boundOut(self,heigth,width,y,x):
  8. if y<0 or x<0 or y>=heigth or x>=width:
  9. return True
  10. return False
  11.  
  12. def findChildern(self,A,R,y,x,color):
  13. heigth,width=A.shape
  14. if(not self.boundOut(heigth,width,y,x+1) and A[y][x+1]!=0):
  15. R[y][x+1]=color
  16. A[y][x+1]=0
  17. self.findChildern(A,R,y,x+1,color)
  18. elif(not self.boundOut(heigth,width,y,x-1) and A[y][x-1]!=0):
  19. R[y][x-1]=color
  20. A[y][x-1]=0
  21. self.findChildern(A,R,y,x-1,color)
  22. elif(not self.boundOut(heigth,width,y+1,x) and A[y+1][x]!=0):
  23. R[y+1][x]=color
  24. A[y+1][x]=0
  25. self.findChildern(A,R,y+1,x,color)
  26. elif(not self.boundOut(heigth,width,y-1,x) and A[y-1][x]!=0):
  27. R[y-1][x]=color
  28. A[y-1][x]=0
  29. self.findChildern(A,R,y-1,x,color)
  30. elif(not self.boundOut(heigth,width,y+1,x+1) and A[y+1][x+1]!=0):
  31. R[y+1][x+1]=color
  32. A[y+1][x+1]=0
  33. self.findChildern(A,R,y+1,x+1,color)
  34. elif(not self.boundOut(heigth,width,y-1,x-1) and A[y-1][x-1]!=0):
  35. R[y-1][x-1]=color
  36. A[y-1][x-1]=0
  37. self.findChildern(A,R,y-1,x-1,color)
  38. elif(not self.boundOut(heigth,width,y+1,x-1) and A[y+1][x-1]!=0):
  39. R[y+1][x-1]=color
  40. A[y+1][x-1]=0
  41. self.findChildern(A,R,y+1,x-1,color)
  42. elif(not self.boundOut(heigth,width,y-1,x+1) and A[y-1][x+1]!=0):
  43. R[y-1][x+1]=color
  44. A[y-1][x+1]=0
  45. self.findChildern(A,R,y-1,x+1,color)
  46.  
  47. def bruteforce(self,matrix):
  48. result=np.zeros((matrix.shape[0],matrix.shape[1],3))
  49.  
  50. for y in xrange(matrix.shape[0]):
  51. for x in xrange(matrix.shape[1]):
  52. if (matrix[y][x]!=0):
  53. matrix[y][x]=0
  54. color=np.array([int(random.random()*255),int(random.random()*255),int(random.random()*255)])
  55. result[y][x]=color
  56. self.findChildern(matrix,result,y,x,color)
  57. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement