Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import numpy as np
- # out_image = medianFilter(in image, filter, offset)
- # out image: Ergebnisbild nach Faltung von in image mit filter
- # in image: Eingangsbild (int) -> numpy array
- # filter: Filtermatrix (float)
- # offset Offset (int)
- def medianFilter(img, filter_matrix, offset_x, offset_y):
- img_array = np.array(img)
- cpy_img_array = np.array(img)
- array_middle_index = int((offset_x*offset_y - 1) / 2)
- border_x = np.ceil(offset_x / 2) - 1
- border_y = np.ceil(offset_y / 2) - 1
- # Converting datatypes np.float64 to python int
- border_x = int(border_x.item())
- border_y = int(border_y.item())
- # reform matrix to array so iterating over is easier
- filter_matrix = np.array(filter_matrix).flatten()
- for y in range(border_y, img.height-1):
- for x in range(border_x, img.width-1):
- tmp_array = img_array[y-border_y:y+border_y+1, x-border_x:x+border_x+1]
- # This seems not to be right -> result a darker picture
- #tmp_array = tmp_array.dot(filter_matrix)
- # reform matrix to array so sorting is possible
- tmp_array = np.asarray(tmp_array).reshape(-1)
- # Hard way without normed filter matrix, but this works
- for idx, val in np.ndenumerate(filter_matrix):
- for e in range (1, val):
- tmp_array = np.append(tmp_array, tmp_array[idx])
- sort_array = np.sort(tmp_array, kind='heapsort')
- tmp = sort_array[array_middle_index]
- cpy_img_array[y][x] = tmp #(sort_array[array_middle_index] / filter_matrix[border_x][border_y])
- return Image.fromarray(cpy_img_array)
- def rgb2grey(rgb):
- return rgb.convert('L')
- # accepts an array of images and the number of times which a image should be filtered
- # returns an array of images which were filtred
- def do_filter_multiple_times(img_array, times, filter_matrix, norm):
- loop_image_array = img_array
- for img_idx in range(len(img_array)):
- for x in range(times):
- loop_image_array[img_idx] = medianFilter(img_array[img_idx], filter_matrix, offset_x, offset_y)
- return loop_image_array
- # returns a filter matrix
- # number==0 returns 3x3 matrix
- # number==1 returns 5x5 matrix
- # number==2 returns 7x7 matrix
- # number==3 returns 15x15 matrix
- def get_filter(number=0):
- filter_matrix3_3 = np.array([[1, 1, 1],
- [1, 3, 1],
- [1, 1, 1]])
- filter_matrix5_5 = np.array([[1, 1, 1, 1, 1],
- [1, 1, 3, 1, 1],
- [1, 3, 5, 3, 1],
- [1, 1, 3, 1, 1],
- [1, 1, 1, 1, 1]])
- filter_matrix7_7 = np.array([[1, 1, 1, 1, 1, 1, 1],
- [1, 1, 3, 3, 3, 1, 1],
- [1, 3, 5, 5, 5, 3, 1],
- [1, 3, 5, 7, 5, 3, 1],
- [1, 3, 5, 5, 5, 3, 1],
- [1, 1, 3, 3, 3, 1, 1],
- [1, 1, 1, 1, 1, 1, 1]])
- filter_matrix15_15 = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1],
- [1, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1],
- [1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 9, 9, 9, 9, 9, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 11, 13, 13, 13, 11, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 11, 13, 15, 13, 11, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 11, 13, 13, 13, 11, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 9, 9, 9, 9, 9, 9, 9, 7, 5, 3, 1],
- [1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 3, 1],
- [1, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1],
- [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
- filter_array = [filter_matrix3_3, filter_matrix5_5, filter_matrix7_7, filter_matrix15_15]
- if number < 0 or number > 3:
- return filter_array[0]
- return filter_array[number]
- if __name__ == "__main__":
- # Save image after computation
- SAVE = True
- # Show image after computation
- SHOW = False
- # How often a filter should run on a picture
- TIMES = 3
- # Which filter should be taken.
- FILTER = 0
- # Take one picture an filter it as often as TIMES says
- MULTIPLE_FILTER_RUN = False
- NORM = np.sum(get_filter(FILTER))
- # determine width and length of filter.
- offset_x, offset_y = get_filter(FILTER).shape
- # Shape for print name on image
- first, second = get_filter(FILTER).shape
- filter_shape = "" + str(first) + "x" + str(second)
- img_lena = Image.open("lena.jpg")
- img_pepper = Image.open("pepper.jpg")
- img_tree = Image.open("tree.png")
- img_lena_grey = rgb2grey(img_lena)
- img_pepper_grey = rgb2grey(img_pepper)
- img_tree_grey = rgb2grey(img_tree)
- img_array = [img_lena_grey] #, img_pepper_grey, img_tree_grey]
- img_array_multi_filter = []
- fileex_jpg = ".jpg"
- fileex_png = ".png"
- image_name = ["lena", "pepper", "tree"]
- image_name_new = ["lena_new" + filter_shape, "pepper_new" + filter_shape, "tree_new" + filter_shape]
- # das array durch das Gewicht zu teilen macht kein sinn, da jedes element kleiner 1 werden würde.
- # Daraus folgt, dass das Bild deutlich dunkler wird. Ebenso rutscht der hotpixel in der sortierung nach oben.
- # new_image_lena = medianFilter(img_lena_grey, get_filter(FILTER) / NORM, offset_x, offset_y)
- if not MULTIPLE_FILTER_RUN:
- for img_idx in range(0, len(img_array)):
- img_new = medianFilter(img_array[img_idx], get_filter(FILTER), offset_x, offset_y)
- # Show image
- if SHOW:
- # Original grey scaled image
- Image._show(img_array[img_idx])
- # Filtered Image
- Image._show(img_new)
- # Save new image
- if SAVE:
- if img_array[img_idx] == image_name[2]:
- img_new.save(image_name_new[img_idx] + fileex_png, "PNG")
- else:
- img_new.save(image_name_new[img_idx] + fileex_jpg, "JPEG")
- # Take an image and filter it several times
- if MULTIPLE_FILTER_RUN:
- filtered = do_filter_multiple_times(img_array, TIMES, FILTER, NORM)
- for img_idx in range(0, len(filtered)):
- # Show image
- if SHOW:
- # Original grey scaled image
- Image._show(img_array[img_idx])
- # Filtered new image
- Image._show(filtered[img_idx])
- # Save new image
- if SAVE:
- if image_name[img_idx] == image_name[2]:
- filtered[img_idx].save(image_name_new[img_idx] + "mul_filter" + fileex_png, "PNG")
- else:
- filtered[img_idx].save(image_name_new[img_idx] + "mul_filter" + fileex_jpg, "JPEG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement