import numpy as np import cv2 as cv # TODO create generic class class LightDark(object): """docstring for LightDark""" def __init__(self, img, method='luminance'): self._img = img meth = getattr(LightDark, method) self.ctrst = meth(self) def luminance(self): """Creates light/dark values using luminance quantifiers for RGB The array has the same dimensions as the image. However the third does only have the size 1 which contains the luminance value """ # Luminance Faktoren nach http://introcs.cs.princeton.edu/python/31datatype/luminance.py.html luminance_factors = np.array([.114, .587, .299]) # Erzeugung eines eindimensionalen Arrays für die effizientere Berechnung self._img = np.multiply(self._img, luminance_factors) # addiert alle Werte auf einer bestimmten Achse luminances = np.sum(self._img, axis=2) return luminances def value(self): """Creates light/dark values using the value channel in HSV""" img = cv.cvtColor(self._img, cv.COLOR_BGR2HSV) values = img[:, :, 2].copy() return values def lightness(self): """Creates light/dark values using the value channel in HSV""" img = cv.cvtColor(self._img, cv.COLOR_BGR2HLS) lightness = img[:, :, 1].copy() return lightness