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

4

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

10
11
12
13
14
        meth = getattr(LightDark, method)
        self.ctrst = meth(self)
        
    def luminance(self):
        """Creates light/dark values using luminance quantifiers for RGB
15

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

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

25
26
        # addiert alle Werte auf einer bestimmten Achse
        luminances = np.sum(self._img, axis=2)
27

28
        return luminances
29

30
31
32
33
    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()
34

35
        return values
36

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

42
        return lightness
43