Commit cd37ece1 authored by Niels-Oliver Walkowski's avatar Niels-Oliver Walkowski
Browse files

add BivariateSequence class to views

is not transfered to numpy array yet
parent 28ca53f6
......@@ -5,6 +5,7 @@
# TODO import as cv
import cv2
import numpy as np
import peakutils
from .helpers import luminance
from copy import deepcopy
from . import contrasts
......@@ -53,7 +54,7 @@ class View(np.ndarray):
if type(obj) == np.ndarray:
obj = np.asarray(input_array, dtype=np.uint8).view(cls).copy()
else:
input_array = np.zeros((0), dtype=np.uint8)
input_array = np.empty((0, 3), dtype=np.uint8)
obj = np.asarray(input_array).view(cls).copy()
obj._frames = frames
obj._contrast = 2
......@@ -100,7 +101,7 @@ class View(np.ndarray):
# subclassing subclass of numpy http://stackoverflow.com/questions/7342637/how-to-subclass-a-subclass-of-numpy-ndarray
# TODO es gibt noch das Problem, dass numpy nach mehreren Berechnungen von drive eine max recursion Warnung ausgiebt, warum? Brauche ich __del__
class VHistStack(View):
class UnivariateSequence(View):
def __new__(cls, frames, input_array=None):
"""Represents a movie contrast in terms of stacked histograms
......@@ -109,15 +110,10 @@ class VHistStack(View):
Parameters
----------
bins : Int
Number of bins for the calculation of the histogram
thrsh : Int
Threshhold defining the number of pixels a bin needs to contain
to be considered in the result
Returns
-------
Object : VHistStack
Object : UnivariateSequence
VHistStack is a 2-dimensional numpy array which contains
the frame number, the bin number and a quantifier which
represents the relative weight of the bin in the frame
......@@ -145,10 +141,11 @@ class VHistStack(View):
self._bins = bins
self._threshold = thrsh
# TODO dafür müssen erst getters und setters in movie.frames definiert werden
# slef._frames.start = start
# slef._frames.end = end
# selef._frames.start = start
# self._frames.end = end
contrast_points = np.empty((0, 3), dtype=np.uint8)
# pwd list sollte in Frames sein und hier nur durchlaufen werden
for frm_nr in range(self._frames.start, self._frames.end, self._frame_step):
pwd = self._frames.folder + self._frames.prefix + str(frm_nr) + '.png'
......@@ -169,17 +166,152 @@ class VHistStack(View):
for bin_index, point in enumerate(hist_value):
if point > self._threshold:
entry = np.array([[frm_nr, bin_index, int(point)]], dtype=np.uint8)
# print('Entry: {0}'.format(entry)) FUNZT
contrast_points = np.vstack((contrast_points, entry))
# print('contrast points in loop: {0}'.format(contrast_points))
# np.append(contrast_points, [[frm_nr, bin_index, int(point)]], axis=0)
# irgendwie prüfen, ob ich contrast_points insgesamt durch self ersetzen kann
contrast_points = np.asarray(contrast_points, np.uint8)
print(contrast_points)
shape = contrast_points.shape
print('contrast_points shape {0}'.format(shape))
self.resize(shape, refcheck=False)
print('self_shape {0}'.format(self.shape))
self[:, :] = contrast_points
contrast_points = None
return deepcopy(self) # TODO does not create a new object
class BivariateSequence(View):
def __new__(cls, frames, input_array=None):
"""Represents a movie contrast in terms minbound maxbound values
For each defined frame a histogram is calculated and each bin that
exceeds threshold is considered in the output array
Parameters
----------
Returns
-------
Object : BivariateSequence
"""
obj = View.__new__(cls, frames, input_array=input_array)
return obj
def __array_finalize__(self, obj):
if obj is None: return
View.__array_finalize__(self, obj)
# das kann ich jetzt auch mit quantilen machen
def populate(self, strategy='peak', ctrst='light_dark',
method='luminance', frm_stp=10, bins=256, **kwargs):
"""Creates a scatterplot for the dynamic of contrast across movie frames
strategy: peak or bin (one of max/min peak; uper/lower mean)
threshold: for the peak strategy TODO
smooth: smooth plot TODO
"""
# set class properties
self._strategy = strategy
self._contrast = ctrst
self._method = method
self._frame_step = frm_stp
self._bins = bins
# TODO um start und end in der Methode zu parametrisieren müssen erst
# getters und setters in movie.frames definiert werden
# self._frames.start = start
# self._frames.end = end
self._channel = 2 # TODO! Durch Klasse ersetzen
# TODO change mins and maxs to (0,2) np.array
maxs = []
mins = []
# sofern kein oder nur ein Peak gefunden wird, man könnte dann auch
# noch einen Durchlauf mit geringeren thres und min_dist versuceh
lastmin = 0
lastmax = 256
# pwd list sollte in Frames sein und hier nur durchlaufen werden
for frm_nr in range(self._frames.start, self._frames.end,
self._frame_step):
pwd = self._frames.folder + self._frames.prefix + str(frm_nr) + '.png'
img = cv2.imread(pwd)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
# BREAK: Funktioniert nicht
if method == 'bin':
hist_values = cv2.calcHist([img_hsv], [self._channel], None,
[256], [0, 256])
hist_mins = hist_values[0:127].flatten().tolist()
hist_maxs = hist_values[128:256].flatten().tolist()
# Für jeden Frame wird jeder Kontrastwert mit der Anzahl der
# Pixel für diesen Kontrastwert multipliziert
# So wird der gesamte Kontrastwert des Frames sowie errechnet
bin_total = 0
total_value = 0
for bin_id, points in enumerate(hist_mins):
bin_total = (bin_id + 1) * points
total_value += bin_total
sum_mins = int(sum(hist_mins))
sum_maxs = int(sum(hist_maxs))
# print('LOG: {0:8d} min points in frame {1:5d}'.format(int(sum_mins), int(frm_nr)))
if int(total_value) == 0:
total_value = [(bin_nr, value) for bin_nr, value in
enumerate(hist_maxs) if
int(value) > 0][0][0]
mins.append(total_value)
else:
mins.append(int(total_value / sum_mins))
bin_total = 0
total_value = 0
for bin_id, points in enumerate(hist_maxs):
bin_total = (bin_id + 1) * points
total_value += bin_total
# if there are no values in hist_max take highes bin with value
# from hist_mins
if int(total_value) == 0:
total_value = [(bin_nr, value) for bin_nr, value in
enumerate(hist_mins) if
int(value) > 0][-1][0]
maxs.append(total_value)
else:
maxs.append(int(total_value / sum(hist_maxs)) + 127)
else:
hist_value = cv2.calcHist([img_hsv], [self._channel],
None, [256], [0, 256])
peaks = peakutils.indexes(hist_value.flatten(), thres=0.2,
min_dist=15)
# Abfangen von von nur 1 oder keinem Peak
if len(peaks) > 1:
lastmin = peaks[0]
mins.append(lastmin)
lastmax = peaks[-1]
maxs.append(lastmax)
elif len(peaks) == 1:
# je nachdem ob der neue peak dem alten min oder max wert
# näher liegt wird er dem einen oder Anderen zugeschlagen
if (lastmax - peaks[0]) < (peaks[0] - lastmin):
lastmax = peaks[0]
maxs.append(lastmax)
mins.append(lastmin)
else:
lastmin = peaks[0]
mins.append(lastmin)
maxs.append(lastmax)
else:
mins.append(lastmin)
maxs.append(lastmax)
return mins, maxs
from itten.movie import Movie
from itten.views import VHistStack
from itten.views import BivariateSequence
movie = Movie(prefix='rec_', folder='../DHd-2017/Data/Frames/Rec/')
cont = VHistStack(movie._frames)
cont = BivariateSequence(movie._frames)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment