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

import numpy as np
5
import pandas as pd
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
40
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
41
def time2framenr(time, fps=4):
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    """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])

57
    return in_sec *  fps
58
59


60
61
62
63
64
65
66
67
68
69
70
71
72
73
def frame2time(val, fps=4):
    """transforms framenumbers into timecodes
    
    Arguments:
        val {int} -- number of frame in the movie
        fps {int} -- time interval by which frames were extracted from the movie
    
    Returns:
        np.datetime64 -- timecode of the frame in the movie
    """

    val = round(val/fps)  # scale frame number to the unit of 1 sec
    tstmp = pd.Timedelta(val, unit='s')
    return tstmp
74
75

def luminance(img):
76
77
78
79
80
    """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
    """
81
82
83
84
    # 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
85
    img = np.multiply(img, luminance_factors)
86
87

    # addiert alle Werte auf einer bestimmten Achse
88
    luminances = np.sum(img, axis=2)
89
90

    return luminances