Initial add of EDS module
This commit is contained in:
parent
61cc0343e0
commit
6247ce24bf
3 changed files with 160 additions and 0 deletions
1
nafuma/eds/__init__.py
Normal file
1
nafuma/eds/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from . import io, plot
|
||||
99
nafuma/eds/io.py
Normal file
99
nafuma/eds/io.py
Normal file
|
|
@ -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
|
||||
60
nafuma/eds/plot.py
Normal file
60
nafuma/eds/plot.py
Normal file
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue