Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. def box_filter(self):
  2. matrix = np.full(9, 1)
  3. self.filter(matrix)
  4.  
  5. def filter(self, matrix):
  6. new_image = np.zeros(self.imageCV.shape)
  7. for ch in range(3):
  8. for x in range(len(self.imageCV)):
  9. for y in range(len(self.imageCV[x])):
  10. sum, n = 0, 0
  11. for i in range(-1, 2):
  12. for j in range(-1, 2):
  13. try:
  14. if x + i >= 0 and y + j >= 0:
  15. sum += matrix[n] * self.imageCV[x + i, y + j, ch]
  16. n += 1
  17. else:
  18. sum += 0
  19. except IndexError:
  20. sum += 0
  21. if sum > 255:
  22. sum = 255
  23. elif sum < 0:
  24. sum = 0
  25. #sum = sum/n
  26. new_image[x, y, ch] = int(sum)
  27.  
  28. self.imageCV = new_image
  29. self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
  30. self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
  31.  
  32. def prewitt(self):
  33. #matrix1 = [-1, -1, 0, -1, 0, 1, 0, 1, 1]
  34. matrix1 = [0, 1, 1, -1, 0, 1, -1, -1, 0]
  35. self.filter(matrix1)
  36.  
  37. def sobel(self):
  38. matrix = [2, 1, 0, 1, 0, -1, 0, -1, -2]
  39. self.filter(matrix)
  40.  
  41. def laplace(self):
  42. matrix1 = [0, -1, 0, -1, 4, -1, 0, -1, 0]
  43. self.filter(matrix1)
  44.  
  45. def corners(self):
  46. matrix = [1, -1, -1, 1, -2, -1, 1, 1, 1]
  47. self.filter(matrix)
  48.  
  49. def kuwahara(self):
  50. #np.var wariancja
  51. new_image = np.zeros(self.imageCV.shape)
  52. n = -2
  53. m = -2
  54. for ch in range(3):
  55. for x in range(len(self.imageCV)):
  56. for y in range(len(self.imageCV[x])):
  57. for box in range(4):
  58. pixels = []
  59. variations = []
  60. mean = []
  61. for i in range(n, n+3):
  62. for j in range(m, m+3):
  63. try:
  64. if x + i >= 0 and y + j >= 0:
  65. pixels.append(self.imageCV[x + i, y + j, ch])
  66. except IndexError:
  67. pass
  68. if box == 1:
  69. m = 0
  70. if box == 2:
  71. n = 0
  72. if box == 3:
  73. m = -2
  74. mean.append(np.mean(pixels))
  75. variations.append(np.var(pixels))
  76. min_var = min(variations)
  77. new_image[x, y, ch] = int(mean[variations.index(min_var)])
  78.  
  79. self.imageCV = new_image
  80. self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
  81. self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
  82.  
  83. def median(self):
  84. new_image = np.zeros(self.imageCV.shape)
  85. for ch in range(3):
  86. for x in range(len(self.imageCV)):
  87. for y in range(len(self.imageCV[x])):
  88. pixels = []
  89. for i in range(-1, 2):
  90. for j in range(-1, 2):
  91. try:
  92. if x + i >= 0 and y + j >= 0:
  93. pixels.append(self.imageCV[x + i, y + j, ch])
  94. except IndexError:
  95. pass
  96. new_image[x, y, ch] = int(np.median(pixels))
  97.  
  98. self.imageCV = new_image
  99. self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
  100. self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement