contrasts.py 1.42 KB
Newer Older
1
import numpy as np
2
import cv2 as cv
3

4
5
# TODO create generic class

6

7
8
9
10
class LightDark(object):
    """docstring for LightDark"""
    def __init__(self, img, method='luminance'):
        self._img = img
11

12
13
        meth = getattr(LightDark, method)
        self.ctrst = meth(self)
14

15
16
    def luminance(self):
        """Creates light/dark values using luminance quantifiers for RGB
17

18
19
           The array has the same dimensions as the image. However the third
           does only have the size 1 which contains the luminance value
20
        """
21
22
        # Luminance Faktoren nach http://introcs.cs.princeton.edu/python/31datatype/luminance.py.html
        luminance_factors = np.array([.114, .587, .299])
23

24
25
        # Erzeugung eines eindimensionalen Arrays für die effizientere Berechnung
        self._img = np.multiply(self._img, luminance_factors)
26

27
28
        # addiert alle Werte auf einer bestimmten Achse
        luminances = np.sum(self._img, axis=2)
29
        luminances = luminances.astype(np.uint8, copy=True)
30

31
        return luminances
32

33
34
35
36
    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()
37

38
        return values
39

40
41
42
43
    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()
44

45
        return lightness