contrasts.py 2.43 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 Contrast(object):
    """Base class for color contrasts"""
    def __init__(self, img):
        self._img = img
        
11

12
class LightDark(Contrast):
13
14
    """docstring for LightDark"""
    def __init__(self, img, method='luminance'):
15
        super(LightDark, self).__init__(img)
16

17
18
        meth = getattr(LightDark, method)
        self.ctrst = meth(self)
19

20
21
    def luminance(self):
        """Creates light/dark values using luminance quantifiers for RGB
22

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

29
30
        # Erzeugung eines eindimensionalen Arrays für die effizientere Berechnung
        self._img = np.multiply(self._img, luminance_factors)
31

32
33
        # addiert alle Werte auf einer bestimmten Achse
        luminances = np.sum(self._img, axis=2)
34
        luminances = luminances.astype(np.uint8, copy=True)
35

36
        return luminances
37

38
39
40
41
    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()
42

43
        return values
44

45
46
47
48
    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()
49

50
        return lightness
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88


class Saturation(Contrast):
    """docstring for Saturation"""
    def __init__(self, img, method='saturation'):
        super(Saturation, self).__init__(img)

        meth = getattr(Saturation, method)
        self.ctrst = meth(self)

    def saturation(self):
        """docstring for saturation"""
        img = cv.cvtColor(self._img, cv.COLOR_BGR2HSV)
        saturations = img[:, :, 1].copy()

        return saturations

    def chroma(self):
        """docstring for chroma"""
        pass


class Hue(Contrast):
    """docstring for Saturation"""
    def __init__(self, img, method='hue'):
        super(Hue, self).__init__(img)

        meth = getattr(Hue, method)
        self.ctrst = meth(self)

    def hue(self):
        """docstring for hue"""
        
        img = cv.cvtColor(self._img, cv.COLOR_BGR2HSV)
        hues = img[:, :, 0].copy()

        return hues