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

import numpy as np
5
import pandas as pd
6
# import math
7
8
9
10


# def lab2hc(labpixel, out='both'):
#     """returns hue and colorfulness of a pixel
11
#
12
13
#        the image pixel has to have CIE Lab colors.
#        the conversion follows Fairchild's 2013 conversion equations
14
#
15
16
17
18
#        labpixel : pixel from image using CIE Lab color model
#        out : 'h' (hue), 'c' (colorfulness) or 'both' (tuple containing hue
#              and colorfulness : default)
#     """
19
#
20
21
22
#     def get_hue(a, b):
#         hue = math.degrees(math.atan(b/a))
#         return hue
23
#
24
25
26
#     def get_colorfulness(a, b):
#         colorfulness = (a**2 + b**2)**(1/2)
#         return colorfulness
27
#
28
29
#     a = labpixel[1]
#     b = labpixel[2]
30
#
31
#     hc = {}
32
#
33
34
35
36
#     if not(out == 'c'):
#         hc['h'] = get_hue(a, b)
#     elif not(out == 'h'):
#         hc['c'] = get_colorfulness(a, b)
37
#
38
39
40
#     return hc

# TODO fps muss noch implementiert werden
41
def time2framenr(time, fps=4):
42
    """counts the frame index for a given timestamp
43

44
45
46
       time : timestamp needs to have the form [HH:]MM:SS
       fps :  frequency with which frames were taken from the movie
    """
47

48
49
50
51
52
53
54
55
56
    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
def frame2time(val, fps=4):
    """transforms framenumbers into timecodes
62

63
64
    Arguments:
        val {int} -- number of frame in the movie
65
66
67
        fps {int} -- time interval by which frames were extracted from the
                     movie

68
69
70
71
72
73
74
    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
75

76

77
def luminance(img):
78
79
80
81
82
    """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
    """
83
84
    # Luminance Faktoren nach
    # http://introcs.cs.princeton.edu/python/31datatype/luminance.py.html
85
86
87
    luminance_factors = np.array([.299, .587, .114])

    # Erzeugung eines eindimensionalen Arrays für die effizientere Berechnung
88
    img = np.multiply(img, luminance_factors)
89
90

    # addiert alle Werte auf einer bestimmten Achse
91
    luminances = np.sum(img, axis=2)
92
93

    return luminances