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

add variables as class parameters for VHistStack

parent a39656e5
......@@ -14,11 +14,40 @@ from copy import deepcopy
# andere ownership Probleme könne angeblich mit out= gelöst werden
# "Use __new__ when you need to control the creation of a new instance.
# Use __init__ when you need to control initialization of a new instance."
class Contrast(np.ndarray):
"""Core class for a color contrast in a movie
subclasses a numpy array"""
def __new__(cls, frames, input_array=None):
class View(np.ndarray):
"""Core class for the representation of colour contrasts in Movies
View is the basis class for specific ways to represent colour contrasts.
It does hold the definitions of contrasts itself. Objects of class View
subclass the numpy array class and hence inherit numpy methods. However,
it is not recommended to use functions which manipulate the array in
terms of structure. In this case some of the additional functions which
are implemented by this class and its subclasses might not lead to
reasonable results.
TODO Docstring komplettieren und Verfahren überprüfen
def __new__(cls, frames, input_array=None, ctrst='lightDark',
frm_stp=10, savefig=False):
"""instantiates the view class
instantiation complies with the recommendation for subclassing
frames :
input_array : itten.contrasts.View # TODO wie schaffte np lower case object
contrast : String # Modifizieren Channel Integer to String
frame_step : Int
savefig : Boolean
Object : View
Empty numpy.ndarray of type View
obj = input_array
if type(obj) == np.ndarray:
obj = np.asarray(input_array, dtype=np.uint8).view(cls).copy()
......@@ -26,19 +55,19 @@ class Contrast(np.ndarray):
input_array = np.zeros((0), dtype=np.uint8)
obj = np.asarray(input_array).view(cls).copy()
obj._frames = frames
obj._channel = 2
obj._frm_step = 50
obj._save = False
obj._contrast = ctrst
obj._frame_step = frm_stp
obj._savefig = savefig
return obj
def __array_finalize__(self, obj):
if obj is None: return
self._frames = getattr(obj, '_frames', None)
self._channel = getattr(obj, '_channel', None)
self._frm_step = getattr(obj, '_frame_step', None)
self._contrast = getattr(obj, '_contrast', None)
self._frame_step = getattr(obj, '_frame_step', None)
self._bins = getattr(obj, '_bins', None)
self._threshold = getattr(obj, '_threshold', None)
self._save = getattr(obj, '_save', None)
self._savefig = getattr(obj, '_savefig', None)
def __array_wrap__(self, out_arr, context=None):
return np.ndarray.__array_wrap__(self, out_arr, context)
......@@ -46,16 +75,39 @@ class Contrast(np.ndarray):
# subclassing subclass of numpy
# TODO es gibt noch das Problem, dass numpy nach mehreren Berechnungen von drive eine max recursion Warnung ausgiebt, warum? Brauche ich __del__
class VHistStack(Contrast):
def __new__(cls, frames, input_array=None):
obj = Contrast.__new__(cls, frames, input_array)
obj._bins = 16
obj._threshold = 60000
class VHistStack(View):
def __new__(cls, frames, input_array=None, ctrst='lightDark',
frm_stp=10, bins=16, thrsh=60000, savefig=False):
"""Represents a movie contrast in terms of stacked histograms
For each defined frame a histogram is calculated and each bin that
exceeds threshold is considered in the output array
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
Object : VHistStack
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
obj = View.__new__(cls, frames, input_array=input_array, ctrst=ctrst,
frm_stp=frm_stp, savefig=savefig)
obj._bins = bins
obj._threshold = thrsh
return obj
def __array_finalize__(self, obj):
if obj is None: return
Contrast.__array_finalize__(self, obj)
View.__array_finalize__(self, obj)
self._bins = getattr(obj, '_bins', None)
self._threshold = getattr(obj, '_threshold', None)
......@@ -64,12 +116,12 @@ class VHistStack(Contrast):
def derive(self):
contrast_points = []
# pwd list sollte in Frames sein und hier nur durchlaufen werden
for frm_nr in range(self._frames.start, self._frames.end, self._frm_step):
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)
if self._channel == 2:
if self._contrast == 2:
_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
luminances = luminance(_img)
hist_value, _ = np.histogram(luminances, bins=self._bins, range=(0, 255))
......@@ -77,7 +129,7 @@ class VHistStack(Contrast):
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
hist_value = cv2.calcHist([img_hsv], [self._channel], None, [16], [0, 256])
hist_value = cv2.calcHist([img_hsv], [self._contrast], None, [16], [0, 256])
for bin_index, point in enumerate(hist_value):
if point > self._threshold:
......@@ -2,4 +2,4 @@ from import Movie
from itten.contrasts import VHistStack
movie = Movie(prefix='rec_', folder='../DHd-2017/Data/Frames/Rec/')
cont = VHistStack(movie._frames)
cont = VHistStack(movie._frames, ctrst=2)
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