Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | None | 0 0
  1. from PIL import Image
  2. import numpy as np
  3.  
  4. # out_image = medianFilter(in image, filter, offset)
  5. # out image: Ergebnisbild nach Faltung von in image mit filter
  6. # in image: Eingangsbild (int) -> numpy array
  7. # filter: Filtermatrix (float)
  8. # offset Offset (int)
  9. def medianFilter(img, filter_matrix, offset_x, offset_y):
  10.  
  11.     img_array = np.array(img)
  12.     cpy_img_array = np.array(img)
  13.  
  14.     array_middle_index = int((offset_x*offset_y - 1) / 2)
  15.  
  16.     border_x = np.ceil(offset_x / 2) - 1
  17.     border_y = np.ceil(offset_y / 2) - 1
  18.  
  19.     # Converting datatypes np.float64 to python int
  20.     border_x = int(border_x.item())
  21.     border_y = int(border_y.item())
  22.  
  23.     # reform matrix to array so iterating over is easier
  24.     filter_matrix = np.array(filter_matrix).flatten()
  25.  
  26.     for y in range(border_y, img.height-1):
  27.         for x in range(border_x, img.width-1):
  28.  
  29.             tmp_array = img_array[y-border_y:y+border_y+1, x-border_x:x+border_x+1]
  30.  
  31.             # This seems not to be right -> result a darker picture
  32.             #tmp_array = tmp_array.dot(filter_matrix)
  33.  
  34.             # reform matrix to array so sorting is possible
  35.             tmp_array = np.asarray(tmp_array).reshape(-1)
  36.  
  37.             # Hard way without normed filter matrix, but this works
  38.             for idx, val in np.ndenumerate(filter_matrix):
  39.                 for e in range (1, val):
  40.                     tmp_array = np.append(tmp_array, tmp_array[idx])
  41.  
  42.             sort_array = np.sort(tmp_array, kind='heapsort')
  43.             tmp = sort_array[array_middle_index]
  44.             cpy_img_array[y][x] = tmp #(sort_array[array_middle_index] / filter_matrix[border_x][border_y])
  45.  
  46.     return Image.fromarray(cpy_img_array)
  47.  
  48.  
  49. def rgb2grey(rgb):
  50.     return rgb.convert('L')
  51.  
  52.  
  53. # accepts an array of images and the number of times which a image should be filtered
  54. # returns an array of images which were filtred
  55. def do_filter_multiple_times(img_array, times, filter_matrix, norm):
  56.     loop_image_array = img_array
  57.  
  58.     for img_idx in range(len(img_array)):
  59.         for x in range(times):
  60.             loop_image_array[img_idx] = medianFilter(img_array[img_idx], filter_matrix, offset_x, offset_y)
  61.  
  62.     return loop_image_array
  63.  
  64.  
  65. # returns a filter matrix
  66. # number==0 returns 3x3 matrix
  67. # number==1 returns 5x5 matrix
  68. # number==2 returns 7x7 matrix
  69. # number==3 returns 15x15 matrix
  70. def get_filter(number=0):
  71.  
  72.     filter_matrix3_3 = np.array([[1, 1, 1],
  73.                                  [1, 3, 1],
  74.                                  [1, 1, 1]])
  75.  
  76.     filter_matrix5_5 = np.array([[1, 1, 1, 1, 1],
  77.                                  [1, 1, 3, 1, 1],
  78.                                  [1, 3, 5, 3, 1],
  79.                                  [1, 1, 3, 1, 1],
  80.                                  [1, 1, 1, 1, 1]])
  81.  
  82.     filter_matrix7_7 = np.array([[1, 1, 1, 1, 1, 1, 1],
  83.                                  [1, 1, 3, 3, 3, 1, 1],
  84.                                  [1, 3, 5, 5, 5, 3, 1],
  85.                                  [1, 3, 5, 7, 5, 3, 1],
  86.                                  [1, 3, 5, 5, 5, 3, 1],
  87.                                  [1, 1, 3, 3, 3, 1, 1],
  88.                                  [1, 1, 1, 1, 1, 1, 1]])
  89.  
  90.     filter_matrix15_15 = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  91.                                    [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1],
  92.                                    [1, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1],
  93.                                    [1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 3, 1],
  94.                                    [1, 3, 5, 7, 9, 9, 9, 9, 9, 9, 9, 7, 5, 3, 1],
  95.                                    [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 9, 7, 5, 3, 1],
  96.                                    [1, 3, 5, 7, 9, 11, 13, 13, 13, 11, 9, 7, 5, 3, 1],
  97.                                    [1, 3, 5, 7, 9, 11, 13, 15, 13, 11, 9, 7, 5, 3, 1],
  98.                                    [1, 3, 5, 7, 9, 11, 13, 13, 13, 11, 9, 7, 5, 3, 1],
  99.                                    [1, 3, 5, 7, 9, 11, 11, 11, 11, 11, 9, 7, 5, 3, 1],
  100.                                    [1, 3, 5, 7, 9, 9, 9, 9, 9, 9, 9, 7, 5, 3, 1],
  101.                                    [1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 3, 1],
  102.                                    [1, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1],
  103.                                    [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1],
  104.                                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
  105.  
  106.     filter_array = [filter_matrix3_3, filter_matrix5_5, filter_matrix7_7, filter_matrix15_15]
  107.  
  108.     if number < 0 or number > 3:
  109.         return filter_array[0]
  110.  
  111.     return filter_array[number]
  112.  
  113.  
  114. if __name__ == "__main__":
  115.  
  116.     # Save image after computation
  117.     SAVE = True
  118.  
  119.     # Show image after computation
  120.     SHOW = False
  121.  
  122.     # How often a filter should run on a picture
  123.     TIMES = 3
  124.  
  125.     # Which filter should be taken.
  126.     FILTER = 0
  127.  
  128.     # Take one picture an filter it as often as TIMES says
  129.     MULTIPLE_FILTER_RUN = False
  130.  
  131.     NORM = np.sum(get_filter(FILTER))
  132.  
  133.     # determine width and length of filter.
  134.     offset_x, offset_y = get_filter(FILTER).shape
  135.  
  136.     # Shape for print name on image
  137.     first, second = get_filter(FILTER).shape
  138.     filter_shape = "" + str(first) + "x" + str(second)
  139.  
  140.     img_lena = Image.open("lena.jpg")
  141.     img_pepper = Image.open("pepper.jpg")
  142.     img_tree = Image.open("tree.png")
  143.  
  144.     img_lena_grey = rgb2grey(img_lena)
  145.     img_pepper_grey = rgb2grey(img_pepper)
  146.     img_tree_grey = rgb2grey(img_tree)
  147.  
  148.     img_array = [img_lena_grey] #, img_pepper_grey, img_tree_grey]
  149.     img_array_multi_filter = []
  150.     fileex_jpg = ".jpg"
  151.     fileex_png = ".png"
  152.     image_name = ["lena", "pepper", "tree"]
  153.     image_name_new = ["lena_new" + filter_shape, "pepper_new" + filter_shape, "tree_new" + filter_shape]
  154.  
  155.     # das array durch das Gewicht zu teilen macht kein sinn, da jedes element kleiner 1 werden würde.
  156.     # Daraus folgt, dass das Bild deutlich dunkler wird. Ebenso rutscht der hotpixel in der sortierung nach oben.
  157.     # new_image_lena = medianFilter(img_lena_grey, get_filter(FILTER) / NORM, offset_x, offset_y)
  158.  
  159.     if not MULTIPLE_FILTER_RUN:
  160.         for img_idx in range(0, len(img_array)):
  161.             img_new = medianFilter(img_array[img_idx], get_filter(FILTER), offset_x, offset_y)
  162.  
  163.             # Show image
  164.             if SHOW:
  165.  
  166.                 # Original grey scaled image
  167.                 Image._show(img_array[img_idx])
  168.  
  169.                 # Filtered Image
  170.                 Image._show(img_new)
  171.  
  172.             # Save new image
  173.             if SAVE:
  174.                 if img_array[img_idx] == image_name[2]:
  175.                     img_new.save(image_name_new[img_idx] + fileex_png, "PNG")
  176.                 else:
  177.                     img_new.save(image_name_new[img_idx] + fileex_jpg, "JPEG")
  178.  
  179.     # Take an image and filter it several times
  180.     if MULTIPLE_FILTER_RUN:
  181.         filtered = do_filter_multiple_times(img_array, TIMES, FILTER, NORM)
  182.  
  183.         for img_idx in range(0, len(filtered)):
  184.  
  185.             # Show image
  186.             if SHOW:
  187.                 # Original grey scaled image
  188.                 Image._show(img_array[img_idx])
  189.  
  190.                 # Filtered new image
  191.                 Image._show(filtered[img_idx])
  192.  
  193.             # Save new image
  194.             if SAVE:
  195.                 if image_name[img_idx] == image_name[2]:
  196.                     filtered[img_idx].save(image_name_new[img_idx] + "mul_filter" + fileex_png, "PNG")
  197.                 else:
  198.                     filtered[img_idx].save(image_name_new[img_idx] + "mul_filter" + fileex_jpg, "JPEG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement