def hist_normalization(picture): histogram = Image.fromarray(picture).convert("L").histogram() lut = [] for b in range(0, len(histogram), 256): step = reduce(operator.add, histogram[b:b + 256]) / 255 n = 0 for i in range(256): lut.append(n / step) n = n + histogram[i + b] return np.asarray(Image.fromarray(picture).point(lut * 1))
def matdims(arr, oned_as='column'): """ Determine equivalent MATLAB dimensions for given array Parameters ---------- arr : ndarray Input array oned_as : {'column', 'row'}, optional Whether 1-D arrays are returned as MATLAB row or column matrices. Default is 'column'. Returns ------- dims : tuple Shape tuple, in the form MATLAB expects it. Notes ----- We had to decide what shape a 1 dimensional array would be by default. ``np.atleast_2d`` thinks it is a row vector. The default for a vector in MATLAB (e.g. ``>> 1:12``) is a row vector. Versions of scipy up to and including 0.11 resulted (accidentally) in 1-D arrays being read as column vectors. For the moment, we maintain the same tradition here. Examples -------- >>> matdims(np.array(1)) # numpy scalar (1, 1) >>> matdims(np.array([1])) # 1d array, 1 element (1, 1) >>> matdims(np.array([1,2])) # 1d array, 2 elements (2, 1) >>> matdims(np.array([[2],[3]])) # 2d array, column vector (2, 1) >>> matdims(np.array([[2,3]])) # 2d array, row vector (1, 2) >>> matdims(np.array([[[2,3]]])) # 3d array, rowish vector (1, 1, 2) >>> matdims(np.array([])) # empty 1d array (0, 0) >>> matdims(np.array([[]])) # empty 2d (0, 0) >>> matdims(np.array([[[]]])) # empty 3d (0, 0, 0) Optional argument flips 1-D shape behavior. >>> matdims(np.array([1,2]), 'row') # 1d array, 2 elements (1, 2) The argument has to make sense though >>> matdims(np.array([1,2]), 'bizarre') Traceback (most recent call last): ... ValueError: 1D option "bizarre" is strange """ shape = arr.shape if shape == (): # scalar return (1,1) if reduce(operator.mul, shape) == 0: # zero elememts return (0,) * np.max([arr.ndim, 2]) if len(shape) == 1: # 1D if oned_as == 'column': return shape + (1,) elif oned_as == 'row': return (1,) + shape else: raise ValueError('1D option "%s" is strange' % oned_as) return shape
def matdims(arr, oned_as='column'): """ Determine equivalent MATLAB dimensions for given array Parameters ---------- arr : ndarray Input array oned_as : {'column', 'row'}, optional Whether 1-D arrays are returned as MATLAB row or column matrices. Default is 'column'. Returns ------- dims : tuple Shape tuple, in the form MATLAB expects it. Notes ----- We had to decide what shape a 1 dimensional array would be by default. ``np.atleast_2d`` thinks it is a row vector. The default for a vector in MATLAB (e.g. ``>> 1:12``) is a row vector. Versions of scipy up to and including 0.11 resulted (accidentally) in 1-D arrays being read as column vectors. For the moment, we maintain the same tradition here. Examples -------- >>> matdims(np.array(1)) # numpy scalar (1, 1) >>> matdims(np.array([1])) # 1d array, 1 element (1, 1) >>> matdims(np.array([1,2])) # 1d array, 2 elements (2, 1) >>> matdims(np.array([[2],[3]])) # 2d array, column vector (2, 1) >>> matdims(np.array([[2,3]])) # 2d array, row vector (1, 2) >>> matdims(np.array([[[2,3]]])) # 3d array, rowish vector (1, 1, 2) >>> matdims(np.array([])) # empty 1d array (0, 0) >>> matdims(np.array([[]])) # empty 2d (0, 0) >>> matdims(np.array([[[]]])) # empty 3d (0, 0, 0) Optional argument flips 1-D shape behavior. >>> matdims(np.array([1,2]), 'row') # 1d array, 2 elements (1, 2) The argument has to make sense though >>> matdims(np.array([1,2]), 'bizarre') Traceback (most recent call last): ... ValueError: 1D option "bizarre" is strange """ shape = arr.shape if shape == (): # scalar return (1, 1) if reduce(operator.mul, shape) == 0: # zero elememts return (0, ) * np.max([arr.ndim, 2]) if len(shape) == 1: # 1D if oned_as == 'column': return shape + (1, ) elif oned_as == 'row': return (1, ) + shape else: raise ValueError('1D option "%s" is strange' % oned_as) return shape
def _basis(j): p = [(xq - x[i]) / (x[j] - x[i]) for i in range(k) if i != j] return reduce((lambda q, w: q * w), p)