def __init__(self, minVal, maxVal, nbCells, perturbatorGenerator, numberSizeGenerator, squareSized=True, normalisation=FilterGenerator.NORMALISATION_NONE, cellSeed=None): """ Parameters ---------- minVal : number The minimum value of a component (included) maxVal : number The maximum value of a component (excluded) nbCells : int > 0 The number of cells/The number of division on the minVal-maxVal segment perturbatorGenerator : class:`NumberGenerator` (must be [0, 1) range) The generator which will produce the perturbation cellSeed : int (default : None = random) The seed for the random generator which will draw the chosen cells """ FilterGenerator.__init__(self, perturbatorGenerator, numberSizeGenerator, squareSized, normalisation) self._min = minVal self._max = maxVal self._nbCells = nbCells self._cellChooser = IntegerUniformGenerator(0, nbCells, cellSeed)
def __init__(self, minVal, maxVal, nbCells, perturbatorGenerator, numberSizeGenerator, squareSized=True, normalisation=FilterGenerator.NORMALISATION_NONE, cellSeed=None): """ Parameters ---------- minVal : number The minimum value of a component (included) maxVal : number The maximum value of a component (excluded) nbCells : int > 0 The number of cells/The number of division on the minVal-maxVal segment perturbatorGenerator : class:`NumberGenerator` (must be [0, 1) range) The generator which will produce the perturbation cellSeed : int (default : None = random) The seed for the random generator which will draw the chosen cells """ FilterGenerator.__init__(self, perturbatorGenerator, numberSizeGenerator, squareSized, normalisation) self._min = minVal self._max = maxVal self._nbCells = nbCells self._cellChooser = IntegerUniformGenerator(0, nbCells, cellSeed)
class StratifiedFG(FilterGenerator): def __init__(self, minVal, maxVal, nbCells, perturbatorGenerator, numberSizeGenerator, squareSized=True, normalisation=FilterGenerator.NORMALISATION_NONE, cellSeed=None): """ Parameters ---------- minVal : number The minimum value of a component (included) maxVal : number The maximum value of a component (excluded) nbCells : int > 0 The number of cells/The number of division on the minVal-maxVal segment perturbatorGenerator : class:`NumberGenerator` (must be [0, 1) range) The generator which will produce the perturbation cellSeed : int (default : None = random) The seed for the random generator which will draw the chosen cells """ FilterGenerator.__init__(self, perturbatorGenerator, numberSizeGenerator, squareSized, normalisation) self._min = minVal self._max = maxVal self._nbCells = nbCells self._cellChooser = IntegerUniformGenerator(0, nbCells, cellSeed) def _createFilter(self, height, width): """Overload""" linearFilter = np.zeros((height, width)) for i in xrange(height): for j in xrange(width): #Locating the middle of the normalized cell inc = 1. / self._nbCells cell = self._cellChooser.getNumber() start = inc * cell end = start + inc middle = (start + end) / 2. #Applying the perturbation perturbation = self._valGen.getNumber(0, inc) val = middle + perturbation #Scaling & shifting valRange = self._max - self._min val = self._min + valRange * val #Assigning the value linearFilter[i][j] = val return linearFilter
class StratifiedFG(FilterGenerator): def __init__(self, minVal, maxVal, nbCells, perturbatorGenerator, numberSizeGenerator, squareSized=True, normalisation=FilterGenerator.NORMALISATION_NONE, cellSeed=None): """ Parameters ---------- minVal : number The minimum value of a component (included) maxVal : number The maximum value of a component (excluded) nbCells : int > 0 The number of cells/The number of division on the minVal-maxVal segment perturbatorGenerator : class:`NumberGenerator` (must be [0, 1) range) The generator which will produce the perturbation cellSeed : int (default : None = random) The seed for the random generator which will draw the chosen cells """ FilterGenerator.__init__(self, perturbatorGenerator, numberSizeGenerator, squareSized, normalisation) self._min = minVal self._max = maxVal self._nbCells = nbCells self._cellChooser = IntegerUniformGenerator(0, nbCells, cellSeed) def _createFilter(self, height, width): """Overload""" linearFilter = np.zeros((height, width)) for i in xrange(height): for j in xrange(width): #Locating the middle of the normalized cell inc = 1./self._nbCells cell = self._cellChooser.getNumber() start = inc*cell end = start+inc middle = (start+end)/2. #Applying the perturbation perturbation = self._valGen.getNumber(0, inc) val = middle+perturbation #Scaling & shifting valRange = self._max - self._min val = self._min + valRange*val #Assigning the value linearFilter[i][j] = val return linearFilter
if __name__ == "__main__": test = True if test: imgpath = "lena.png" from FilterGenerator import FilterGenerator, Finite3SameFilter from Convolver import RGBConvolver from SubWindowExtractor import MultiSWExtractor, SubWindowExtractor from NumberGenerator import OddUniformGenerator, NumberGenerator, IntegerUniformGenerator from Aggregator import AverageAggregator imgPil = Image.open(imgpath) img = np.array(imgPil) #CONVOLUTIONAL EXTRACTOR #Filter generator filterValGenerator = IntegerUniformGenerator(-5, 5) filterSizeGenerator = OddUniformGenerator(3, 10) baseFilterGenerator = FilterGenerator(filterValGenerator, filterSizeGenerator) filterGenerator = Finite3SameFilter(baseFilterGenerator, 6) #Convolver convolver = RGBConvolver() #SubWindowExtractor subwindowTargetWidth = 200 subwindowTargetHeight = 200 swNumGenerator = NumberGenerator() swExtractor = SubWindowExtractor( 0.5, 1., subwindowTargetWidth, subwindowTargetHeight, SubWindowExtractor.INTERPOLATION_BILINEAR, swNumGenerator)
# for i in xrange(nbFilters): # ls[i] = filterGenerators[i % len(filterGenerators)].next() # return ls # else: # pass # TODO XXX if __name__ == "__main__": test = False if test: from NumberGenerator import (NumberGenerator, IntegerUniformGenerator, OddUniformGenerator) fltGen = FilterGenerator(NumberGenerator(-7, 15), OddUniformGenerator(30, 16)) fltGen2 = FilterGenerator(IntegerUniformGenerator(15, 20), OddUniformGenerator(3, 10)) diff3 = Finite3Filter(fltGen, 5) same3 = Finite3SameFilter(fltGen2, 5) for f in diff3: print f[0], f[0].shape print f[1], f[1].shape print f[2], f[2].shape for f in same3: print f[0], f[0].shape print f[1], f[1].shape print f[2], f[2].shape