helpers.py 2.1 KB
Newer Older
1
2
3
4
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import math


# def lab2hc(labpixel, out='both'):
#     """returns hue and colorfulness of a pixel
# 
#        the image pixel has to have CIE Lab colors.
#        the conversion follows Fairchild's 2013 conversion equations
# 
#        labpixel : pixel from image using CIE Lab color model
#        out : 'h' (hue), 'c' (colorfulness) or 'both' (tuple containing hue
#              and colorfulness : default)
#     """
# 
#     def get_hue(a, b):
#         hue = math.degrees(math.atan(b/a))
#         return hue
# 
#     def get_colorfulness(a, b):
#         colorfulness = (a**2 + b**2)**(1/2)
#         return colorfulness
# 
#     a = labpixel[1]
#     b = labpixel[2]
# 
#     hc = {}
# 
#     if not(out == 'c'):
#         hc['h'] = get_hue(a, b)
#     elif not(out == 'h'):
#         hc['c'] = get_colorfulness(a, b)
# 
#     return hc

# TODO fps muss noch implementiert werden
40
def time2framenr(time, fps=5):
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    """counts the frame index for a given timestamp
    
       time : timestamp needs to have the form [HH:]MM:SS
       fps :  frequency with which frames were taken from the movie
    """
    
    timestmp = time.split(':')
    in_sec = 0

    if len(timestmp) > 2:
        in_sec += int(timestmp[0]) * 3600

    in_sec += int(timestmp[-2]) * 60
    in_sec += int(timestmp[-1])

56
    return in_sec *  fps
57
58
59
60
61
62
63
64


def timelabels(val, pos):
    min, sec = divmod(int(val), 60)
    timelabel = "{0}:{1:02d}".format(min, sec)
    return timelabel

def luminance(img):
65
66
67
68
69
    """Creates an array of luminance value for each pixel of an image

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

    # Erzeugung eines eindimensionalen Arrays für die effizientere Berechnung
74
    img = np.multiply(img, luminance_factors)
75
76

    # addiert alle Werte auf einer bestimmten Achse
77
    luminances = np.sum(img, axis=2)
78
79

    return luminances