From 6247ce24bf209ff52e4d0d129ce7bec578f713f0 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 27 Sep 2022 10:20:13 +0200 Subject: [PATCH] Initial add of EDS module --- nafuma/eds/__init__.py | 1 + nafuma/eds/io.py | 99 ++++++++++++++++++++++++++++++++++++++++++ nafuma/eds/plot.py | 60 +++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 nafuma/eds/__init__.py create mode 100644 nafuma/eds/io.py create mode 100644 nafuma/eds/plot.py diff --git a/nafuma/eds/__init__.py b/nafuma/eds/__init__.py new file mode 100644 index 0000000..e0e052c --- /dev/null +++ b/nafuma/eds/__init__.py @@ -0,0 +1 @@ +from . import io, plot \ No newline at end of file diff --git a/nafuma/eds/io.py b/nafuma/eds/io.py new file mode 100644 index 0000000..8f269d6 --- /dev/null +++ b/nafuma/eds/io.py @@ -0,0 +1,99 @@ +from PIL import Image +import numpy as np +import cv2 + +def read_image(path, weight=None, colour=None, resize=None): + + img = np.array(Image.open(path)) + + if colour is not None: + img = change_colour(img, colour) + + if resize is not None: + img = resize_image(img, resize) + + if weight is not None: + img = scale_image(img, weight) + + return img + + +def scale_image(image, factor): + + + for i in range(0,image.shape[0]): + for j in range(0, image.shape[1]): + image[i][j][0] = image[i][j][0]*factor + image[i][j][1] = image[i][j][1]*factor + image[i][j][2] = image[i][j][2]*factor + + return image + + +def resize_image(image, factor): + + y, x = image.shape[0:2] + + new_y, new_x = int(y*factor), int(x*factor) + + image = image[:new_y, :new_x] + + res = cv2.resize(image, dsize=(x, y), interpolation=cv2.INTER_CUBIC) + + return res + + +def add_images(image1, image2): + + assert image1.shape == image2.shape + + compound_image = np.zeros((image1.shape[0], image1.shape[1], image1.shape[2])) + for i in range(image1.shape[0]): + for j in range(image1.shape[1]): + compound_image[i][j] = [0, 0, 0] + + compound_image[i][j][0] = int(int(image1[i][j][0]) + int(image2[i][j][0])) + compound_image[i][j][1] = int(int(image1[i][j][1]) + int(image2[i][j][1])) + compound_image[i][j][2] = int(int(image1[i][j][2]) + int(image2[i][j][2])) + + + + return compound_image + + + + +def get_colour(image): + + + colour = [0, 0, 0] + for i in range(image.shape[0]): + for j in range(image.shape[1]): + if image[i][j][0] > colour[0]: + colour[0] = image[i][j][0] + + if image[i][j][1] > colour[1]: + colour[1] = image[i][j][1] + + if image[i][j][2] > colour[2]: + colour[2] = image[i][j][2] + + colour = np.array(colour) + + return colour + + +def change_colour(image, new_colour): + + new_colour = np.array(new_colour) + + old_colour = get_colour(image) + + + for i in range(image.shape[0]): + for j in range(image.shape[1]): + factor = max(image[i][j]) / max(old_colour) + image[i][j] = new_colour.astype(float) * factor + + + return image diff --git a/nafuma/eds/plot.py b/nafuma/eds/plot.py new file mode 100644 index 0000000..9e634e4 --- /dev/null +++ b/nafuma/eds/plot.py @@ -0,0 +1,60 @@ +import nafuma.auxillary as aux +import nafuma.plotting as btp +import nafuma.eds.io as io + +import numpy as np + +def show_image(data, options={}): + + + default_options = { + 'hide_x_labels': True, + 'hide_y_labels': True, + 'hide_x_ticklabels': True, + 'hide_y_ticklabels': True, + 'hide_x_ticks': True, + 'hide_y_ticks': True, + 'colours': None, + 'show_image': True + } + + options = aux.update_options(options=options, required_options=default_options.keys(), default_options=default_options) + + + + if not isinstance(data['path'], list): + data['path'] = [data['path']] + + + if not 'image' in data.keys(): + + data['image'] = [None for _ in range(len(data['path']))] + + if not 'weights' in data.keys(): + data['weights'] = [1.0 for _ in range(len(data['path']))] + + if not options['colours']: + options['colours'] = [None for _ in range(len(data['path']))] + + for i, (path, weight, colour) in enumerate(zip(data['path'], data['weights'], options['colours'])): + data['image'][i] = io.read_image(path=path, weight=weight, colour=colour, resize=options['resize']) + + + images = [] + for i, image in enumerate(data['image']): + images.append(image) +# + final_image = np.mean(images, axis=0) / 255 + if len(data['path']) > 1: + data['image'].append(final_image) + + if options['show_image']: + fig, ax = btp.prepare_plot(options) + ax.imshow(final_image) + btp.adjust_plot(fig=fig, ax=ax, options=options) + + return data['image'], fig, ax + + else: + return data['image'], None, None +