Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def box_filter(self):
- matrix = np.full(9, 1)
- self.filter(matrix)
- def filter(self, matrix):
- new_image = np.zeros(self.imageCV.shape)
- for ch in range(3):
- for x in range(len(self.imageCV)):
- for y in range(len(self.imageCV[x])):
- sum, n = 0, 0
- for i in range(-1, 2):
- for j in range(-1, 2):
- try:
- if x + i >= 0 and y + j >= 0:
- sum += matrix[n] * self.imageCV[x + i, y + j, ch]
- n += 1
- else:
- sum += 0
- except IndexError:
- sum += 0
- if sum > 255:
- sum = 255
- elif sum < 0:
- sum = 0
- #sum = sum/n
- new_image[x, y, ch] = int(sum)
- self.imageCV = new_image
- self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
- self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
- def prewitt(self):
- #matrix1 = [-1, -1, 0, -1, 0, 1, 0, 1, 1]
- matrix1 = [0, 1, 1, -1, 0, 1, -1, -1, 0]
- self.filter(matrix1)
- def sobel(self):
- matrix = [2, 1, 0, 1, 0, -1, 0, -1, -2]
- self.filter(matrix)
- def laplace(self):
- matrix1 = [0, -1, 0, -1, 4, -1, 0, -1, 0]
- self.filter(matrix1)
- def corners(self):
- matrix = [1, -1, -1, 1, -2, -1, 1, 1, 1]
- self.filter(matrix)
- def kuwahara(self):
- #np.var wariancja
- new_image = np.zeros(self.imageCV.shape)
- n = -2
- m = -2
- for ch in range(3):
- for x in range(len(self.imageCV)):
- for y in range(len(self.imageCV[x])):
- for box in range(4):
- pixels = []
- variations = []
- mean = []
- for i in range(n, n+3):
- for j in range(m, m+3):
- try:
- if x + i >= 0 and y + j >= 0:
- pixels.append(self.imageCV[x + i, y + j, ch])
- except IndexError:
- pass
- if box == 1:
- m = 0
- if box == 2:
- n = 0
- if box == 3:
- m = -2
- mean.append(np.mean(pixels))
- variations.append(np.var(pixels))
- min_var = min(variations)
- new_image[x, y, ch] = int(mean[variations.index(min_var)])
- self.imageCV = new_image
- self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
- self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
- def median(self):
- new_image = np.zeros(self.imageCV.shape)
- for ch in range(3):
- for x in range(len(self.imageCV)):
- for y in range(len(self.imageCV[x])):
- pixels = []
- for i in range(-1, 2):
- for j in range(-1, 2):
- try:
- if x + i >= 0 and y + j >= 0:
- pixels.append(self.imageCV[x + i, y + j, ch])
- except IndexError:
- pass
- new_image[x, y, ch] = int(np.median(pixels))
- self.imageCV = new_image
- self.photo = ImageTk.PhotoImage(image=Image.fromarray(self.imageCV.astype('uint8')))
- self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement