Select Git revision
scheduler.mch
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
histogram.py 4.27 KiB
from tokenize import String
from typing import Dict, List
import imageio
import matplotlib.pyplot as plt
import numpy as np
from skimage import color
from histogram_enum import HISTOGRAMS
class Histogram(object):
def __init__(self, origin_path: String, edit_path: String, b: int = 0):
"""
This class is the editor which edits a given image and stores a new one.
:param origin_path: The original image which should be edited.
:param edit_path: The edited image which should be stored.
:param b: Is only used if the LoG-Operator is used.
"""
self.image_path = origin_path
self.edit_path = edit_path
self.b = b
@staticmethod
def __get_bins(image: imageio.core.util.Array) -> Dict:
"""
This method counts each value in the image.
h(i) = card{(u,v) | I(u,v) = i}
0 <= i <= K - 1
:param image: the greyscale image
:return: Dict where the keys are the values and the key-value is the count
"""
K = 256
count = {}
for k in range(K):
count[str(k)] = 0
height, width = image.shape
for v in range(height):
for u in range(width):
count[str(image[v, u])] += 1
return count
@staticmethod
def __get_intervals(B: int):
intervals = {}
splits = np.arange(0, 256, 255 / B)
for b in range(B):
intervals[str(b)] = list(splits[b:b + 2])
return intervals
def __get_bins_in_interval(self, image: imageio.core.util.Array, B: int) -> Dict:
"""
This method counts each value in the image.
h(i) = card{(u,v) | a_j < I(u,v) < a_j+1}
0 <= j <= B
:param image: the greyscale image
:return: Dict where the keys are the values and the key-value is the count
"""
intervals = self.__get_intervals(B)
count = {}
for b in range(B):
count[str(b)] = 0
height, width = image.shape
for v in range(height):
for u in range(width):
for b in range(B):
if intervals[str(b)][0] < image[v, u] < intervals[str(b)][1]:
count[str(b)] += 1
return count
@staticmethod
def __bins_to_list(bins: Dict) -> List:
"""
This method converts a dict into a sorted list of tuples.
:param bins: Bins of the histogram
:return: Sorted list of tuples containing all key and key-value pairs.
"""
bins_list = []
for key in bins.keys():
bins_list += [(int(key), bins[str(key)])]
return bins_list
def __plot_h1(self, keys: List, values: List, option: String):
ticks = 6
indices = np.arange(len(keys))
plt.bar(indices, values, color='black')
if option == HISTOGRAMS.h1.value:
plt.xticks(np.arange(min(keys), 256, step=255 / (ticks - 1)))
elif option == HISTOGRAMS.h2.value:
plt.xticks(np.arange(0, self.b))
interval_strings = []
intervals = self.__get_intervals(self.b)
for i in range(self.b):
interval_strings += ['(' + str(intervals[str(i)][0]) + '-' + str(intervals[str(i)][1]) + ')']
plt.xticks(range(self.b),
interval_strings, rotation=45, fontsize=5)
plt.savefig('images/histogram.jpeg', dpi=100)
def get_histogram(self, option: String) -> None:
"""
This method iterates over the image and changes each pixel by a given method.
Notice, that the image writes the result as a grey-scale image.
:param option: The options defined in HISTOGRAMS.
:return: None
"""
image = imageio.imread(uri=self.image_path)
plt.imshow(image)
plt.show()
grey_scale_image = color.rgb2gray(image)
# convert greyscale to interval
result = np.multiply(255, grey_scale_image).astype(int)
bins = {'4': '2'}
if option == HISTOGRAMS.h1.value:
bins = self.__get_bins(result)
elif option == HISTOGRAMS.h2.value:
bins = self.__get_bins_in_interval(result, self.b)
bins = self.__bins_to_list(bins)
keys, values = zip(*bins)
self.__plot_h1(keys, values, option)