Beispiel #1
0
    def __init__(self, image, fit_par=None, dt=0, fw=None, win_size=None,
                 kernel=None, xkernel=None, bkg_image=None):

        self.image = image
        self.bkg_image = bkg_image

        # Noise removal by convolving with a null sum gaussian. Its FWHM
        # has to match the one of the objects we want to detect.
        try:
            self.fwhm = fw
            self.win_size = win_size
            self.kernel = kernel
            self.xkernel = xkernel
            self.image_conv = convolve(self.image.astype(float), self.kernel)
        except RuntimeError:
            # If the kernel is None, I assume all the args must be calculated
            self.fwhm = tools.get_fwhm(670, 1.42) / 120
            self.win_size = int(np.ceil(self.fwhm))
            self.kernel = tools.kernel(self.fwhm)
            self.xkernel = tools.xkernel(self.fwhm)
            self.image_conv = convolve(self.image.astype(float), self.kernel)

        # TODO: FIXME
        if self.bkg_image is None:
            self.bkg_image = self.image_conv

        self.fit_par = fit_par
        self.dt = dt
Beispiel #2
0
    def _postprocess_nodes(self):
        fluid_map = self._fluid_map_base(wet=False).astype(np.uint8)
        wet_map = self._fluid_map_base(wet=True).astype(np.uint8)
        neighbors = np.zeros((3, 3, 3), dtype=np.uint8)
        neighbors[1,1,1] = 1
        for ei in self.grid.basis:
            neighbors[1 + ei[2], 1 + ei[1], 1 + ei[0]] = 1

        # Any wet node not connected to at least one fluid node is marked unused.
        # Note that dry nodes connecting to wet nodes need to be retained.
        # For instance:
        #  W W
        #  W V
        # where W is a HBB wall and V is a velocity BC.
        where = (filters.convolve(fluid_map, neighbors, mode='wrap') == 0)
        self._type_map_base[where & wet_map.astype(np.bool)] = nt._NTUnused.id

        # Any dry node, not connected to at least one wet node is marked unused.
        # For instance, for HBB walls: .. W W W F -> .. U U W F.
        where = (filters.convolve(wet_map, neighbors, mode='wrap') == 0)
        self._type_map_base[where & np.logical_not(wet_map)] = nt._NTUnused.id

        # If an unused node touches a wet node, mark it as propagation only.
        # For instance, for HBB walls: .. U U W F -> .. U P W F.
        used_map = (self._type_map_base != nt._NTUnused.id).astype(np.uint8)
        where = (filters.convolve(used_map, neighbors, mode='wrap') > 0)
        self._type_map_base[where & (self._type_map_base == nt._NTUnused.id)] = nt._NTPropagationOnly.id
    def step(self, dt):
        if dt!=self.dt:
            print "I can only integrate at fixed dt!"
            return

        self.nCells = len(self.cellStates)
        # Check we have enough space allocated
        try:
            s = self.specLevel[self.nCells-1]
        except IndexError:
            # Could resize here, then would have to rebuild views
            print "Number of cells exceeded " \
                    + self.__class__.__name__ \
                    + "::maxCells (" + self.maxCells + ")"

        self.dataLen = self.signalDataLen + self.nCells*self.nSpecies

        # Do u += h(T(u_t)/2 + hf(u_t)) where T=transport operator, f(u_t) is 
        # our regulation function dydt
        self.signalling.transportRates(self.signalRate, self.signalLevel)
        self.signalRate *= 0.5
        self.dydt()
        self.rates[0:self.dataLen] *= self.dt
        self.levels[0:self.dataLen] += self.rates[0:self.dataLen]

        # Convolve (I+hT/2)u_t + f(u_t) with the Greens func to get u_{t+1}
        sigLvl = self.signalLevel.reshape(self.gridDim)
        convolve(sigLvl, self.greensFunc, mode='nearest')

        # Put the final signal levels into the cell states
        states = self.cellStates
        for (id,c) in states.items():
            if self.signalling:
                c.signals = self.signalling.signals(c, self.signalLevel)
Beispiel #4
0
def stitch(targets,images):
    mask = rois_mask(targets) # True where image data is
    gaps_mask = mask==False # True where infill needs to go
    # compute bounds relative to the camera field
    (x,y,w,h) = stitched_box(targets)
    uroi = img_as_float(stitch_raw(targets,images,(x,y,w,h))) # stitch with black infill

    # step 1: sparsely sample background mostly ignoring blob
    # compute gradient on both axes
    k = [[-3,-1,0,1,3],
         [-3,-1,0,1,3],
         [-3,-1,0,1,3],
         [-3,-1,0,1,3]]
    gy = convolve(uroi,k)
    gx = convolve(uroi,np.rot90(k))
    # ignore all but low-gradient areas
    bg = (abs(gy+gx) < 0.2) & mask

    # step 2: remove less contiguous areas
    filter_size = max(2,int(max(h,w)/200))
    mf = minimum_filter(bg*1,filter_size)

    # step 3: interpolate between samples
    z = inpaint(uroi*mf,mf==False)

    # step 4: subsample and re-interpolate to degrade artifacts in fill region
    random = RandomState(0)
    (h,w)=z.shape
    ng = random.rand(h,w) < 0.01
    z2 = inpaint(z*ng,ng==False)

    # step 5: final composite
    roi = (z2 * gaps_mask) + uroi
    return (roi * 255).astype(np.uint8), mask
Beispiel #5
0
def convolve1d(Z, K, toric=False):
    """ Discrete, clamped, linear convolution of two one-dimensional sequences.

        The convolution operator is often seen in signal processing, where it
        models the effect of a linear time-invariant system on a signal [1]_.
        In probability theory, the sum of two independent random variables is
        distributed according to the convolution of their individual
        distributions.
    
        :param array Z:
            One-dimensional array.
        :param array K:
            One-dimensional array.
        :param bool toric:
            Indicate whether convolution should be considered toric
        :return: 
            Discrete, clamped, linear convolution of `Z` and `K`.

        **Note**

            The discrete convolution operation is defined as

            .. math:: (f * g)[n] = \sum_{m = -\infty}^{\infty} f[m] g[n-m]

        **References**

        .. [1] Wikipedia, "Convolution",
                      http://en.wikipedia.org/wiki/Convolution.
    """

    if toric:
        return convolve(Z,K,mode='wrap')
    else:
        return convolve(Z,K,mode='constant')
Beispiel #6
0
def mvd_lr(initImg, imgList, psfList, iterNum):
    EPS = np.finfo(float).eps
    viewNum = len(imgList)
    initImg = initImg - np.amin(initImg)
    initImg = initImg / np.sum(np.abs(initImg))
    reconImg = initImg
    for i in xrange(iterNum):
        updateAll = np.ones(initImg.shape, dtype=float)
        for j in xrange(viewNum):
            img = imgList[j]
            psf = psfList[j]
            psf_prime = np.flipud(np.fliplr(psf))
            update = convolve(img/(convolve(reconImg, psf)+EPS), psf_prime)
            updateAll = updateAll * update
            # display progress
            progress = float(i*viewNum+j+1)/(viewNum*iterNum)
            timeElapsed = time.time() - startTime
            timeRemaining = timeElapsed/progress*(1-progress)
            sys.stdout.write('\r%.2f%%, %.2f s elapsed, %.2f s remaining' %
                             (progress*100.0, timeElapsed, timeRemaining))
            sys.stdout.flush()
        reconImg = reconImg * updateAll
        reconImg = np.abs(reconImg)
        reconImg = reconImg / np.sum(reconImg)
    sys.stdout.write('\n')
    return reconImg
Beispiel #7
0
def rl_damped(raw, psf, niter=2, damped=True, N=3, T=None, multiplier=1):
    """ working on it"""

    #psf /= psf.sum()

    conversion = raw.mean() / 10
    raw /= conversion
    lucy = np.ones(raw.shape) * raw.mean()

    #plt.ion()
    #plt.figure()
    #plt.plot(raw)
    #plt.axhline(y=0, lw=2, color='black')
    for i in xrange(niter):
        if damped:
            print "dampening"
            lucy_temp = convolve( lucy, psf, mode='mirror')
            ratio = dampen(lucy_temp, raw, N, T, multiplier)
        else:
            ratio = raw / convolve(lucy, psf, mode='mirror')

        ratio[ np.isnan(ratio) ] = 0

        top = convolve( ratio, psf, mode='mirror')
        top[ np.isnan(top) ] = 0

        lucy = lucy * (top / psf.sum())
        #plt.plot( lucy )
        print 'iteration', i, lucy.mean(), raw.mean()
        print

    #raw_input('Done')
    return lucy * conversion
Beispiel #8
0
def rl_standard(raw_image, psf, niter):
    """ Standerd lucy-richardson convolution

    arXiv 2002 Lauer

    """
   
    psf /= psf.sum()
    psf_inverse = psf[::-1]
    lucy = np.ones( raw_image.shape ) * raw_image.mean()

    for i in xrange( niter ):
        estimate = convolve(lucy, psf, mode='mirror')
        estimate[ np.isnan(estimate) ] = 0

        correction = convolve(raw_image/estimate, psf_inverse, mode='mirror')
        correction[ np.isnan(correction) ] = 0
        print 'Correction:',correction.mean()
        
        lucy *= correction
        print 'Means:', raw_image.mean(), lucy.mean()
        chisq = scipy.nansum((lucy - raw_image)**2 / (lucy)) / (raw_image.size-1)
        print chisq

    return lucy
 def transportRates(self, signalRates, signalLevels, boundcond='constant', mode='normal'):
     # Compute diffusion term, laplacian of grid levels in signalLevels, 
     # write into signalRates
     #
     # mode='greens' - do not use initLevels as these don't apply!
     signalRatesView = signalRates.reshape(self.gridDim)
     signalLevelsView = signalLevels.reshape(self.gridDim)
     advKernel = numpy.zeros((3,3,3))
     advKernel[:,1,1] = [-0.5,0,0.5]
     for s in range(self.nSignals):
         if boundcond=='constant' and self.initLevels and mode!='greens':
             boundval = self.initLevels[s]
         else:
             boundval = 0.0 
         if self.advRates:
             # Adevction term = du/dx
             # Note: always use 'nearest' edge case, this gives central 
             # differences in middle, and forward/backward differences on edges
             convolve(signalLevelsView[s], advKernel*self.advRates[s], output=signalRatesView[s], mode='nearest')
             # Diffusion term = \del^2u
             # Use edge case from boundary conditions for diffusion
             signalRatesView[s] += laplace(signalLevelsView[s], None, mode=boundcond, cval=boundval) * \
                                                                                 self.diffRates[s] / 6.0
         else:
             signalRatesView[s] = laplace(signalLevelsView[s], None, mode=boundcond, cval=boundval) \
                                                                                * self.diffRates[s] / 6.0
Beispiel #10
0
 def process(self, image):
     image = image.astype(np.double)
     if image.max() > 1:
         # The image is between 0 and 255 - we need to convert it to [0,1]
         image /= 255;
     if image.ndim == 3:
         # we do not deal with color images.
         image = np.mean(image,axis=2)
     H,W = image.shape
     IH = filters.convolve(image, self._GH, mode='nearest')
     IW = filters.convolve(image, self._GW, mode='nearest')
     I_mag = np.sqrt(IH ** 2 + IW ** 2)
     I_theta = np.arctan2(IH, IW)
     
     alpha = self.specs.get('alpha', _ALPHA)
     num_angles = self.specs.get('num_angles', _NUM_ANGLES)
     I_orient = np.empty((H, W, num_angles))
     if self.specs.get('twoside', True):
         for i in range(num_angles):
             I_orient[:,:,i] = I_mag * np.maximum(
                     np.cos(I_theta - self._ANGLES[i]) ** alpha, 0)
     else:
         for i in range(num_angles):
             I_orient[:,:,i] = I_mag * np.abs(
                     np.cos(I_theta - self._ANGLES[i]) ** alpha)
     return I_orient
Beispiel #11
0
def add_pointsources(map_shape, freq, alpha0=4.5, sigma=0.5, A=1, number=1):

    map = np.zeros(map_shape)
    spec_list = []

    for i in range(number):
        ra  = np.random.randint(0, map_shape[1])
        dec = np.random.randint(0, map_shape[2])

        alpha = np.random.normal(alpha0, sigma, 1)
        spec = A * (freq/150.)**alpha
        spec_list.append(spec)

        map[:, ra, dec] += spec

    out = np.zeros(map_shape)
    for i in range(map_shape[0]):
        kernel = np.arange(41) - 20. #GBT
        #kernel = np.arange(21) - 10.
        kernel = sp.exp(-kernel**2 / (2. * 3 ** 2.))
        kernel *= 1. / (2. * sp.pi * 3 ** 2.)
        kernel = kernel[:, None] * kernel[None, :]
        convolve(map[i], kernel, output=out[i])

    map = out

    return map, spec_list
Beispiel #12
0
    def _postprocess_nodes(self):
        fluid_map = self._fluid_map(wet=False, base=True).astype(np.uint8)
        wet_map_for_unused = self._fluid_map(wet=True, allow_unused=True, base=True).astype(np.uint8)
        wet_map = self._fluid_map(wet=True, base=True).astype(np.uint8)
        neighbors = self._lattice_kernel()

        # Any *wet* node not connected to at least one *fluid* node is marked unused.
        # Note that dry nodes connecting to wet nodes need to be retained.
        # For instance:
        #  W W
        #  W V
        # where W is a HBB wall and V is a velocity BC.
        where = filters.convolve(fluid_map, neighbors, mode="constant", cval=1) == 0
        self._type_map_base[where & wet_map_for_unused.astype(np.bool)] = nt._NTUnused.id

        # Any dry node, not connected to at least one wet node is marked unused.
        # For instance, for HBB walls: .. W W W F -> .. U U W F.
        where = filters.convolve(wet_map, neighbors, mode="constant", cval=0) == 0
        self._type_map_base[where & np.logical_not(wet_map.astype(np.bool))] = nt._NTUnused.id

        # If an unused node touches a wet node, mark it as propagation only.
        # For instance, for HBB walls: .. U U W F -> .. U P W F.
        used_map = (self._type_map_base != nt._NTUnused.id).astype(np.uint8)
        where = filters.convolve(used_map, neighbors, mode="constant", cval=0) > 0
        self._type_map_base[where & (self._type_map_base == nt._NTUnused.id)] = nt._NTPropagationOnly.id
def preprocess(pattern, img):
    #bilinear interpolation for bayer_rggb images
    if pattern == 'bayer_rggb':
        (z, q, h) = (0.0, 0.25, 0.5)
        sparse = np.array([[q, h, q],
                           [h, z, h],
                           [q, h, q]])

        dense = np.array([[z, q, z],
                          [q, z, q],
                          [z, q, z]])

        img[0,:,:] = \
            np.where(img[0,:,:] > 0.0,
            img[0,:,:],
            convolve(img[0,:,:], sparse, mode='mirror'))
        img[1,:,:] = \
            np.where(img[1,:,:] > 0.0,
            img[1,:,:],
            convolve(img[1,:,:], dense,  mode='mirror'))
        img[2,:,:] = \
            np.where(img[2,:,:] > 0.0,
            img[2,:,:],
            convolve(img[2,:,:], sparse, mode='mirror'))

        img = np.dstack((img[2,:,:],
                                img[1,:,:],
                                img[0,:,:]))

        return np.swapaxes(np.swapaxes(img, 2,0), 1,2)
    else:
        raise NotImplementedError('Preprocessing is implemented only for bayer_rggb')
    def step(self, dt):
        if dt!=self.dt:
            print "I can only integrate at fixed dt!"
            return

        self.nCells = len(self.cellStates)
        # Check we have enough space allocated
        try:
            s = self.specLevel[self.nCells-1]
        except IndexError:
            # Could resize here, then would have to rebuild views
            print "Number of cells exceeded " \
                    + self.__class__.__name__ \
                    + "::maxCells (" + self.maxCells + ")"

        self.dataLen = self.signalDataLen + self.nCells*self.nSpecies

        # growth dilution of species
        self.diluteSpecies()

        # Do u += h(T(u_t)/2 + hf(u_t)) where T=transport operator, f(u_t) is 
        # our regulation function dydt
        self.signalling.transportRates(self.signalRate, self.signalLevel, self.boundcond)
        self.signalRate *= 0.5
        self.dydt()
        self.rates[0:self.dataLen] *= self.dt
        self.levels[0:self.dataLen] += self.rates[0:self.dataLen]

        # Convolve (I+hT/2)u_t + f(u_t) with the Greens func to get u_{t+1}
        sigLvl = self.signalLevel.reshape(self.gridDim)
        convolve(sigLvl, self.greensFunc, mode=self.boundcond)

        # put local cell signal levels in array
        self.signalLevel_dev.set(self.signalLevel)
        self.program.setCellSignals(self.queue, (self.nCells,), None,
                numpy.int32(self.nSignals),
                numpy.int32(self.gridTotalSize),
                numpy.int32(self.signalling.gridDim[1]),
                numpy.int32(self.signalling.gridDim[2]),
                numpy.int32(self.signalling.gridDim[3]),
                self.gridIdxs_dev.data,
                self.triWts_dev.data,
                self.signalLevel_dev.data,
                self.cellSigLevels_dev.data).wait()
        self.cellSigLevels[:] = self.cellSigLevels_dev.get()

# Put the final signal levels into the cell states
#        states = self.cellStates
#        for (id,c) in states.items():
#            if self.signalling:
#                c.signals = self.signalling.signals(c, self.signalLevel)

        # Update cellType array
        for (id,c) in self.cellStates.items():
            self.celltype[c.idx] = numpy.int32(c.cellType)
        self.celltype_dev.set(self.celltype)
def np_hs_jacobi(im0, im1, u, v):
    It = im1 - im0
    Iy = convolve(im1, dy)
    Ix = convolve(im1, dx)
    denom = np.square(Ix) + np.square(Iy) + alpha ** 2

    for _ in range(100):
        ubar = convolve(u, jacobi)
        vbar = convolve(v, jacobi)
        t = (Ix * ubar + Iy * vbar + It) / denom
        u = ubar - Ix * t
        v = vbar - Iy * t
    return u, v
Beispiel #16
0
    def shade(self, grid):
        k = self.make_k()

        out_dtype = grid.dtype if not self.anti_alias else np.float64
        out = np.empty_like(grid, dtype=out_dtype)
        if len(grid.shape) == 3:
            cats = grid.shape[2]
            for cat in range(cats):
                convolve(grid[:, :, cat], k, output=out[:, :, cat],
                         mode='constant', cval=0.0)
        else:
            convolve(grid, k, mode='constant', cval=0.0, output=out)

        return out
Beispiel #17
0
    def _postprocess_nodes(self):
        fluid_map = self._fluid_map_base().astype(np.uint8)
        neighbors = np.zeros((3, 3, 3), dtype=np.uint8)
        neighbors[1,1,1] = 1
        for ei in self.grid.basis:
            neighbors[1 + ei[2], 1 + ei[1], 1 + ei[0]] = 1

        # Any node not connected to at least one wet node is marked unused.
        where = (filters.convolve(fluid_map, neighbors, mode='wrap') == 0)
        self._type_map_base[where] = nt._NTUnused.id

        # If an unused node touches a wet node, mark it as propagation only.
        used_map = (self._type_map_base != nt._NTUnused.id).astype(np.uint8)
        where = (filters.convolve(used_map, neighbors, mode='wrap') > 0)
        self._type_map_base[where & (self._type_map_base == nt._NTUnused.id)] = nt._NTPropagationOnly.id
Beispiel #18
0
def _filter_image(image, min_scale, max_scale, mode):

    response = np.zeros((image.shape[0], image.shape[1],
                         max_scale - min_scale + 1), dtype=np.double)

    if mode == 'dob':

        # make response[:, :, i] contiguous memory block
        item_size = response.itemsize
        response.strides = (item_size * response.shape[1], item_size,
                            item_size * response.shape[0] * response.shape[1])

        integral_img = integral_image(image)

        for i in range(max_scale - min_scale + 1):
            n = min_scale + i

            # Constant multipliers for the outer region and the inner region
            # of the bi-level filters with the constraint of keeping the
            # DC bias 0.
            inner_weight = (1.0 / (2 * n + 1) ** 2)
            outer_weight = (1.0 / (12 * n ** 2 + 4 * n))

            _censure_dob_loop(n, integral_img, response[:, :, i],
                              inner_weight, outer_weight)

    # NOTE : For the Octagon shaped filter, we implemented and evaluated the
    # slanted integral image based image filtering but the performance was
    # more or less equal to image filtering using
    # scipy.ndimage.filters.convolve(). Hence we have decided to use the
    # later for a much cleaner implementation.
    elif mode == 'octagon':
        # TODO : Decide the shapes of Octagon filters for scales > 7

        for i in range(max_scale - min_scale + 1):
            mo, no = OCTAGON_OUTER_SHAPE[min_scale + i - 1]
            mi, ni = OCTAGON_INNER_SHAPE[min_scale + i - 1]
            response[:, :, i] = convolve(image,
                                         _octagon_kernel(mo, no, mi, ni))

    elif mode == 'star':

        for i in range(max_scale - min_scale + 1):
            m = STAR_SHAPE[STAR_FILTER_SHAPE[min_scale + i - 1][0]]
            n = STAR_SHAPE[STAR_FILTER_SHAPE[min_scale + i - 1][1]]
            response[:, :, i] = convolve(image, _star_kernel(m, n))

    return response
def smooth_nD(x,window_len=10,window='hanning',axis=0):
	#smooths nD data along one axis only.
	from scipy.ndimage.filters import convolve
	#axis argument still in testing
	if x.ndim > 3: raise ValueError, "smooth only accepts 1,2 or 3 dimensional arrays."
	if x.size < window_len: raise ValueError, "Input vector needs to be bigger than window size."
	if window_len<3: return x
	if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
		raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"

	if window == 'flat': #moving average
		w=ones(window_len,'d')
	else:
		w=eval(window+'(window_len)')
    #
	#Now extrude the 1D window into nD, along the correct axis
	#if x.ndim==1: w_temp = ones()
	if x.ndim==2:
		if axis==0: 
			w = array([w]) #seems to work
		elif axis==1: 
			w.transpose()#seems to work
	elif x.ndim==3:
		w = array([[w]]) #seems to work
		if axis==1: 
			w = w.transpose((2,1,0)) #seems to work
		elif axis==0: 
			w = w.transpose((0,2,1)) #seems to work
	
	y=convolve(x, w/w.sum(),mode='reflect')
	return y
Beispiel #20
0
def fitScaling(n_events, box, YTOF, YBVG, goodIDX=None, neigh_length_m=3):
    YJOINT = 1.0 * YTOF * YBVG
    YJOINT /= 1.0 * YJOINT.max()

    convBox = 1.0 * \
        np.ones([neigh_length_m, neigh_length_m, neigh_length_m]) / \
        neigh_length_m**3
    conv_n_events = convolve(n_events, convBox)

    QX, QY, QZ = ICCFT.getQXQYQZ(box)
    dP = 8
    fitMaxIDX = tuple(
        np.array(np.unravel_index(YJOINT.argmax(), YJOINT.shape)))
    if goodIDX is None:
        goodIDX = np.zeros_like(YJOINT).astype(np.bool)
        goodIDX[max(fitMaxIDX[0] - dP, 0):min(fitMaxIDX[0] + dP, goodIDX.shape[0]),
                max(fitMaxIDX[1] - dP, 0):min(fitMaxIDX[1] + dP, goodIDX.shape[1]),
                max(fitMaxIDX[2] - dP, 0):min(fitMaxIDX[2] + dP, goodIDX.shape[2])] = True
    goodIDX = np.logical_and(goodIDX, conv_n_events > 0)

    scaleLinear = Polynomial(n=1)
    scaleLinear.constrain("A1>0")
    scaleX = YJOINT[goodIDX]
    scaleY = n_events[goodIDX]
    CreateWorkspace(OutputWorkspace='__scaleWS', dataX=scaleX, dataY=scaleY)
    fitResultsScaling = Fit(Function=scaleLinear, InputWorkspace='__scaleWS',
                            Output='__scalefit', CostFunction='Unweighted least squares')
    A0 = fitResultsScaling[3].row(0)['Value']
    A1 = fitResultsScaling[3].row(1)['Value']
    YRET = A1 * YJOINT + A0
    chiSqRed = fitResultsScaling[1]

    return YRET, chiSqRed, A1
Beispiel #21
0
def whiten(img, whiten_kernel):
    filt_img = np.zeros_like(img)
    for i in xrange(img.shape[2]):
        for j in xrange(img.shape[2]):
            filt_img[:,:,i] = filt_img[:,:,i] + convolve(img[:,:,j], whiten_kernel[i,:,:,j])
            
    return filt_img
def gTV(x, N, strtag, kern, dirWeight, dirs=None, nmins=0, dirInfo=[None,None,None,None], a=10):

    if nmins:
        M = dirInfo[0]
        dIM = dirInfo[1]
        Ause = dirInfo[2]
        inds = dirInfo[3]
    else:
        M = None
        dIM = None
        Ause = None
        inds = None
    
    if len(x.shape) == 2:
        N = np.hstack([1,N])
        
    x0 = x.reshape(N)
    grad = np.zeros(np.hstack([N[0], len(strtag), N[1:]]),dtype=float)
    if kern.shape == 3:
        Nkern = np.hstack(1,[kern.shape[1:]])
    else:
        Nkern = kern.shape[1:]
        
    TV_data = tf.TV(x0,N,strtag,kern,dirWeight,dirs,nmins,dirInfo)
    for i in xrange(len(strtag)):
        if strtag[i] == 'spatial':
            grad[:,i,:,:] = convolve(np.tanh(a*TV_data[i]),kern[i].reshape(Nkern),mode='wrap')
        elif strtag[i] == 'diff':
            None
    grad = np.sum(grad,axis=1)
    return grad
Beispiel #23
0
    def set_active_node_map_from_wall_map(self, wall_map):
        """Sets the active node map from a wall map.

        Takes care of ensuring that only a single layer of wall nodes is
        remains as active nodes. To be used in load_active_node_map().

        :param wall_map: Boolean array covering all nodes, including ghosts.
            True indicates a solid node not participating in the simulation.
            These nodes would typically be set to NTFullBBWall or similar.
        """
        fluid_map = np.logical_not(wall_map)
        # Build a convolution kernel to count active neighbor nodes.
        if self.dim == 3:
            neighbors = np.zeros((3, 3, 3), dtype=np.uint8)
            for x in self.grid.basis:
                neighbors[x[0] + 1, x[1] + 1, x[2] + 1] = 1
        else:
            neighbors = np.zeros((3, 3), dtype=np.uint8)
            for x in self.grid.basis:
                neighbors[x[0] + 1, x[1] + 1] = 1

        # Mark nodes connected to at least one active node as active.
        # We need these nodes for walls and ghost nodes.
        where = (filters.convolve(fluid_map.astype(np.uint8),
                                  neighbors, mode='wrap') > 0)
        fluid_map[where] = True
        self.active_node_mask = fluid_map
 def test(self):
     # Expected
     in_channels = 3
     in_dim = 11
     out_channels = 5
     out_dim = (in_dim/2 + 1)
     img = np.arange(0,in_dim*in_dim*in_channels*1, dtype=np.float32)
     img = np.reshape(img,[in_dim,in_dim,in_channels,1])
     filter = np.arange(0,3*3*in_channels*out_channels, dtype=np.float32)
     filter = np.reshape(filter,[3,3,in_channels,out_channels])
     bias = np.zeros([5])
     expected = np.zeros([out_dim,out_dim,out_channels])
     for och in range(out_channels):
         tmp = np.zeros([out_dim,out_dim,1])
         for ich in range(in_channels):
             imgslice = np.reshape(img[:,:,ich,0],[in_dim,in_dim])
             filterslice = np.reshape(filter[:,:,ich,och],[3,3])
             tmp += np.reshape(convolve(imgslice,filterslice,mode='constant',cval = 0.0)[::2,::2] , [out_dim, out_dim, 1])
         expected[:,:,och] = np.squeeze(tmp) + bias[och]
         
     # test
     owlimg = owl.from_numpy(np.transpose(img))
     owlfilter = owl.from_numpy(np.transpose(filter))
     owlbias = owl.from_numpy(bias)
     convolver = owl.conv.Convolver(1,1,2,2)   
     test = convolver.ff(owlimg, owlfilter, owlbias)
     
     print 'Expected\n',expected
     print "Actual\n",test.to_numpy()
     self.assertTrue(np.allclose(expected, test))
Beispiel #25
0
def convolve_with_gaussian2d(x, y, d, lx, ly, pa):
    from scipy.ndimage.filters import convolve
    import numpy as np
    
    def gausian2d(lx, ly, t):
        sint = np.sin(t)
        cost = np.cos(t)
        lx, ly = lx/np.sqrt(4*np.log(2)), ly/np.sqrt(4*np.log(2))
        def f(x, y):
            xx = x * cost - y * sint
            yy = x * sint + y * cost
            return np.exp(-((xx/lx)**2+(yy/ly)**2))
        return f
    
    pa_radian = pa * np.pi / 180.0
    lx_coor = lx * np.cos(pa_radian)
    ly_coor = ly * np.sin(pa_radian)
    
    dx = x[1] - x[0]
    dy = y[1] - y[0]
    
    nx = 9 + 2*int(abs(lx_coor / dx))
    ny = 9 + 2*int(abs(ly_coor / dy))
    
    kx, ky = np.meshgrid(np.arange(-nx, nx+1) * dx,
                         np.arange(-ny, ny+1) * dy)
    
    kk = gausian2d(lx, ly, pa_radian)(kx, ky)
    #print kx.shape
    return convolve(d, kk, mode='constant', cval=0.0, origin=0)
Beispiel #26
0
def MultiScaleSSIM(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5,
                   k1=0.01, k2=0.03, weights=None):
  """Return the MS-SSIM score between `img1` and `img2`.

  This function implements Multi-Scale Structural Similarity (MS-SSIM) Image
  Quality Assessment according to Zhou Wang's paper, "Multi-scale structural
  similarity for image quality assessment" (2003).
  Link: https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf

  Author's MATLAB implementation:
  http://www.cns.nyu.edu/~lcv/ssim/msssim.zip

  Arguments:
    img1: Numpy array holding the first RGB image batch.
    img2: Numpy array holding the second RGB image batch.
    max_val: the dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
    filter_size: Size of blur kernel to use (will be reduced for small images).
    filter_sigma: Standard deviation for Gaussian blur kernel (will be reduced
      for small images).
    k1: Constant used to maintain stability in the SSIM calculation (0.01 in
      the original paper).
    k2: Constant used to maintain stability in the SSIM calculation (0.03 in
      the original paper).
    weights: List of weights for each level; if none, use five levels and the
      weights from the original paper.

  Returns:
    MS-SSIM score between `img1` and `img2`.

  Raises:
    RuntimeError: If input images don't have the same shape or don't have four
      dimensions: [batch_size, height, width, depth].
  """
  if img1.shape != img2.shape:
    raise RuntimeError('Input images must have the same shape (%s vs. %s).',
                       img1.shape, img2.shape)
  if img1.ndim != 4:
    raise RuntimeError('Input images must have four dimensions, not %d',
                       img1.ndim)

  # Note: default weights don't sum to 1.0 but do match the paper / matlab code.
  weights = np.array(weights if weights else
                     [0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
  levels = weights.size
  downsample_filter = np.ones((1, 2, 2, 1)) / 4.0
  im1, im2 = [x.astype(np.float64) for x in [img1, img2]]
  mssim = np.array([])
  mcs = np.array([])
  for _ in range(levels):
    ssim, cs = _SSIMForMultiScale(
        im1, im2, max_val=max_val, filter_size=filter_size,
        filter_sigma=filter_sigma, k1=k1, k2=k2)
    mssim = np.append(mssim, ssim)
    mcs = np.append(mcs, cs)
    filtered = [convolve(im, downsample_filter, mode='reflect')
                for im in [im1, im2]]
    im1, im2 = [x[:, ::2, ::2, :] for x in filtered]
  return (np.prod(mcs[0:levels-1] ** weights[0:levels-1]) *
          (mssim[levels-1] ** weights[levels-1]))
Beispiel #27
0
def convolve_image(image, filt):
    layers = ()
    for i in range(image.shape[2]):
        layer_lpf = im_filters.convolve(image[:,:,i], filt) # signal.convolve2d(image[:,:,i], filt, mode='same')
        layers += (layer_lpf.astype(np.uint8), )

    return np.dstack(layers)
Beispiel #28
0
def hog(image, orientations=8, ksize=(5, 5)):
    '''
    returns the Histogram of Oriented Gradients

    :param ksize: convolution kernel size as (y,x) - needs to be odd
    :param orientations: number of orientations in between rad=0 and rad=pi

    similar to http://scikit-image.org/docs/dev/auto_examples/plot_hog.html
    but faster and with less options
    '''
    s0, s1 = image.shape[:2]

    # speed up the process through saving generated kernels:
    try:
        k = hog.kernels[str(ksize) + str(orientations)]
    except KeyError:
        k = _mkConvKernel(ksize, orientations)
        hog.kernels[str(ksize) + str(orientations)] = k

    out = np.empty(shape=(s0, s1, orientations))
    image[np.isnan(image)] = 0

    for i in range(orientations):
        out[:, :, i] = convolve(image, k[i])
    return out
Beispiel #29
0
def semi_local(l, p_templates, L, beta):

    q = l/(l.sum(0) + np.finfo(np.float32).eps)

    k = np.ones((1, 1, 3, 3, 3), dtype=np.float32)

    diff = np.inf

    m_last = np.zeros(L.shape, dtype=np.uint8)

    while diff > 1000000:

        for i in range(l.shape[0]):

            p_template = np.zeros((1, 1) + L.shape)

            for j in range(p_templates.shape[1]):

                mask = L == j

                p_template[0, 0, mask] += p_templates[i, j, mask]

            q[i] = l[i:i+1]*p_template*np.exp(beta*convolve(q[i:i+1], k, mode='constant'))

        q = q/(q.sum(0) + np.finfo(np.float32).eps)
        m = np.argmax(q, 0)[0]
        diff = (m != m_last).sum()
        m_last = m

        print diff

    p = p_templates*q
    L = np.argmax(p.sum(0), 0)

    return L
Beispiel #30
0
def formatTS(start, stop, r, useIntersects=True):
    aStarts = {}
    X = []
    Y = []
    coords = []
    sigNum = []
    strcArr = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    for sig in xrange(start, stop):
        if sig % 50 == 0:
            print sig
        path = np.array(sigs[sig])
        imgO = imread(getFilePath(sig), as_grey=True)
        xs = [n for n in xrange()]
        plt.subplot(1, 2, 1)
        plt.imshow(imgO)
        plt.subplot(1, 2, 2)
        plt.imshow(guassian_filter(imgO, 1))
        plt.show()
        thresh = 0.9
        img = gaussian_filter(imgO, 1) < thresh
        imgX, imgY = np.nonzero(img)
        imgW = imgX.max() - imgX.min()
        imgH = imgY.max() - imgY.min()
        img = skeletonize(img)
        img = img[imgX.min() : imgX.max(), imgY.min() : imgY.max()]
        skltnSegs, nSkltnSegs = label(img, structure=strcArr)
        # print nSkltnSegs
        # plt.imshow(skltnSegs)
        # plt.show()
        imgO = imgO[imgX.min() : imgX.max(), imgY.min() : imgY.max()]
        sumArr = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
        summed = convolve(img, sumArr, mode="constant", cval=0)
        corners = (summed == 2) & (img == 1)
        startx = path[0][0] * img.shape[1]
        starty = path[0][1] * img.shape[0]
        aStarts[sig] = [startx, starty]
        if useIntersects:
            intersects = (summed >= 4) & (img == 1)
            labeled, nLabels = label(intersects, structure=strcArr)
            itrscts = []
            for l in xrange(1, nLabels + 1):
                intersect = np.array((labeled == l), dtype=int)
                posX, posY = np.nonzero(intersect)
                xC, yC = np.array([[np.sum(posX) / posX.size], [np.sum(posY) / posY.size]])
                itrscts.append([xC[0], yC[0]])
            itrscts = np.array(itrscts)
        corners = np.transpose(np.array(np.nonzero(corners)))
        if useIntersects:
            try:
                corners = np.vstack((itrscts, corners))
            except:
                print "something went wrong at", sig
        corners[:, [0, 1]] = corners[:, [1, 0]]
        for i, corner in enumerate(corners):
            x, y = corner[0], corner[1]
            x = getFeatures(imgO, x, y, r, imgH, imgW, skltnSegs)
            X.append(x)
            sigNum.append(sig)
            coords.append([x, y])
    return np.array(X), np.array(sigNum), np.array(coords)
Beispiel #31
0
def harris_corners(img, window_size=3, k=0.04):
    """
    Compute Harris corner response map. Follow the math equation
    R=Det(M)-k(Trace(M)^2).

    Hint:
        You may use the function scipy.ndimage.filters.convolve,
        which is already imported above.

    Args:
        img: Grayscale image of shape (H, W)
        window_size: size of the window function
        k: sensitivity parameter

    Returns:
        response: Harris response image of shape (H, W)
    """

    H, W = img.shape
    window = np.ones((window_size, window_size))

    response = np.zeros((H, W))

    dx = filters.sobel_v(img)
    dy = filters.sobel_h(img)

    ### YOUR CODE HERE
    mx = convolve(dx**2, window, mode='constant', cval=0)
    my = convolve(dy**2, window, mode='constant', cval=0)
    mxy = convolve(dx * dy, window, mode='constant', cval=0)

    for r in range(H):
        for c in range(W):
            M = [[mx[r, c], mxy[r, c]], [mxy[r, c], my[r, c]]]
            response[r, c] = np.linalg.det(M) - (k * (np.trace(M)**2))

    return response
    def sobel(self, img):
        """
        Sobel edge detection
        """
        kernel_sy = np.array([
            [1.0, 2.0, 1.0],
            [0.0, 0.0, 0.0],
            [-1.0, -2.0, -1.0],
        ])
        # This converts it from a 2D filter to a 3D filter, replicating the same
        # filter for each channel: R, G, B
        kernel_sy = np.stack([kernel_sy] * 3, axis=2)

        kernel_sx = np.array([
            [1.0, 0.0, -1.0],
            [2.0, 0.0, -2.0],
            [1.0, 0.0, -1.0],
        ])
        # This converts it from a 2D filter to a 3D filter, replicating the same
        # filter for each channel: R, G, B
        kernel_sx = np.stack([kernel_sx] * 3, axis=2)

        img = img.astype('float32')
        sobel = np.absolute(convolve(img, kernel_sx)) + \
            np.absolute(convolve(img, kernel_sy))

        # We sum the energies in the red, green, and blue channels
        self.energy_map = sobel.sum(axis=2)

        # If the image being handled has the same number of columns as
        # the input image, then this is the first iteration
        if img.shape[1] == self.input_image.shape[1]:
            cv2.imwrite(SOBEL_EDGE_PATH, np.rot90(sobel, 3, (0, 1)))
            cv2.imwrite(SOBEL_ENERGY_PATH, np.rot90(self.energy_map, 3,
                                                    (0, 1)))

        return self.energy_map
Beispiel #33
0
    def sobel_filters(self, img):
        """Function that generates sobel filters to be used in the canny edge detection process. This step intensifies
           the gradients of the image.

        Parameters
        ----------
        img : (numpy.ndarray)
            The 2-D array that represents the image

        Returns
        -------
        (numpy.ndarray)
            The image after the sobel operation.
        """
        Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float64)
        Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float64)

        Ix = convolve(img, Kx)
        Iy = convolve(img, Ky)

        G = np.hypot(Ix, Iy)
        G = G / G.max() * 255
        theta = np.arctan2(Iy, Ix)
        return (G, theta)
Beispiel #34
0
def calc_energy(img):
    """
    calculate image energy based on gradient using sobel operator

    Args:
        img:  numpy.ndarray of shape [H, W, C]

    Returns:
        energy_map:  numpy.ndarray of shape [H, W]
    """

    kern_du = np.asarray([[1., 2., 1.], [
        0.,
        0.,
        0.,
    ], [-1., -2., -1.]])
    kern_dv = kern_du.T
    filter_du = np.stack([kern_du] * 3, axis=2)
    filter_dv = np.stack([kern_dv] * 3, axis=2)
    img = img.astype(np.float32)
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))
    energy_map = np.sum(convolved, axis=2)
    return energy_map
Beispiel #35
0
def predict(pdf, offset, kernel, mode='wrap', cval=0.):
    """ Performs the discrete Bayes filter prediction step, generating
    the prior.

    `pdf` is a discrete probability distribution expressing our initial
    belief.

    `offset` is an integer specifying how much we want to move to the right
    (negative values means move to the left)

    We assume there is some noise in that offset, which we express in `kernel`.
    For example, if offset=3 and kernel=[.1, .7., .2], that means we think
    there is a 70% chance of moving right by 3, a 10% chance of moving 2
    spaces, and a 20% chance of moving by 4.

    It returns the resulting distribution.

    If `mode='wrap'`, then the probability distribution is wrapped around
    the array.

    If `mode='constant'`, or any other value the pdf is shifted, with `cval`
    used to fill in missing elements.

    Examples
    --------
    .. code-block:: Python

        belief = [.05, .05, .05, .05, .55, .05, .05, .05, .05, .05]
        prior = predict(belief, offset=2, kernel=[.1, .8, .1])
    """

    if mode == 'wrap':
        return convolve(np.roll(pdf, offset), kernel, mode='wrap')

    return convolve(shift(pdf, offset, cval=cval), kernel,
                    cval=cval, mode='constant')
Beispiel #36
0
def makeData():
    samples = np.zeros((config.nbOfKernels*config.nbOfSamplesFromBatch, config.imageSize*config.imageSize + config.kernelSize*config.kernelSize))
    targets = np.zeros((config.nbOfKernels*config.nbOfSamplesFromBatch, config.imageSize*config.imageSize))
    kernels = convolutionKernels.makeRandomKernels(nbOfKernels=config.nbOfKernels, kernelSize=config.kernelSize)
    data = loadCifar10.load()
    count = 0
    for i in range(0, config.nbOfSamplesFromBatch):
        for j in range(0, config.nbOfKernels):
            kernel = kernels[j, :, :]
            image = data[i, :, :]/(255*1.0)
            res = convolve(image, kernel, mode='constant', cval=0.0)
            samples[count, :] = np.hstack((np.reshape(image,(1, config.imageSize*config.imageSize)), np.reshape(kernel,(1,config.kernelSize*config.kernelSize))))
            targets[count, :] = np.reshape(res, (1, res.shape[0]*res.shape[1]))

    return samples, targets
def harris_corner_detector(im):
    """
    Detects harris corners.
    Make sure the returned coordinates are x major!!!
    :param im: A 2D array representing an image.
    :return: An array with shape (N,2), where ret[i,:] are the [x,y] coordinates of the ith corner points.
    """
    # get the x der and y der using [1,0,-1] and its transpose respectively
    dx_vec = np.array([[0, 0, 0], [1, 0, -1], [0, 0, 0]])
    dy_vec = np.array([[0, -1, 0], [0, 0, 0], [0, 1, 0]])

    der_x = convolve(im, dx_vec)
    der_y = convolve(im, dy_vec)

    # blur the images der_x^2, der_y^2, der_x*der_y using spatial blur from utils with sernel 3
    der_x_sq = sol4_utils.blur_spatial(der_x ** 2, 3)
    der_y_sq = sol4_utils.blur_spatial(der_y ** 2, 3)
    der_x_y = sol4_utils.blur_spatial(der_x * der_y, 3)

    # for each pixel, find the size of both eigenvalues using det(M) - 0.04(trace(M))^2, place in response output image
    response = np.zeros(shape=im.shape)

    XSquaredYSquaredMat= np.multiply(der_x_sq, der_y_sq)
    XYSquaredMat = np.multiply(der_x_y, der_x_y)
    detM = XSquaredYSquaredMat - XYSquaredMat
    traceM = der_x_sq + der_y_sq

    response = detM - (0.04 * np.multiply(traceM, traceM))

    # use supplied non_maximum_supp function to get binary image of corners
    response = non_maximum_suppression(response)

    # return (x,y) of all corners (which is (col,row))
    indices = np.argwhere(response.T)

    return indices
def compute_smoothness_weights(L: np.ndarray,
                               x: int,
                               kernel: np.ndarray,
                               eps: float = 1e-3):
    """Compute the smoothness weights used in refining the illumination map optimization problem.
    Arguments:
        L {np.ndarray} -- the initial illumination map to be refined.
        x {int} -- the direction of the weights. Can either be x=1 for horizontal or x=0 for vertical.
        kernel {np.ndarray} -- spatial affinity matrix
    Keyword Arguments:
        eps {float} -- small constant to avoid computation instability. (default: {1e-3})
    Returns:
        np.ndarray - smoothness weights according to direction x. same dimension as `L`.
    """
    Lp = cv2.Sobel(L, cv2.CV_64F, int(x == 1), int(x == 0), ksize=1)
    #weight=Lp*255
    #output_illumination_map(weight)
    T = convolve(np.ones_like(L), kernel, mode='constant')
    #output_illumination_map(T)
    T = T / (np.abs(convolve(Lp, kernel, mode='constant')) + eps)
    #output_illumination_map(T)
    #weight=(T / (np.abs(Lp) + eps))
    #output_illumination_map(weight)
    return T / (np.abs(Lp) + eps)
Beispiel #39
0
 def _add_motion_blur(self, image, kernel_size, angle):
     """
     simulate motion blur on the given image using a square kernel of
     size kernel_size where the line has the given angle in radians.
     :param image: a gray-scale image with values in the
     [0, 1] range of type float64.
     :param kernel_size: an odd integer specifying the size of
     the kernel (even integers are ill-defined).
     :param angle: an angle in radians in the range [0, π).
     :return: im after motion blur on it using a square kernel of
     size kernel_size where the line has the
     given angle in radians.
     """
     kernel = self._motion_blur_kernel(kernel_size, angle)
     return convolve(image, kernel)
Beispiel #40
0
def calc_energy(img):
    filter_du = np.array([
        [1.0, 2.0, 1.0],
        [0.0, 0.0, 0.0],
        [-1.0, -2.0, -1.0],
    ])

    filter_du = np.stack([filter_du] * 3, axis=2)

    filter_dv = np.array([
        [1.0, 0.0, -1.0],
        [2.0, 0.0, -2.0],
        [1.0, 0.0, -1.0],
    ])

    filter_dv = np.stack([filter_dv] * 3, axis=2)

    img = img.astype('float32')
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))

    energy_map = convolved.sum(axis=2)

    return energy_map
Beispiel #41
0
def add_motion_blur(image, kernel_size, angle):
    """
    Simulates motion blur on the given image using a square kernel of size 'kernel_size', made of a single
    line crossing its center, and the line has the given 'angle' in radians, measured relative to the positive
    horizontal axis.
    :param image: a grayscale image with values in the [0,1] range of type float64.
    :param kernel_size: an odd integer specifying the size of the kernel (even integers are ill-defined).
    :param angle: an angle in radians in the range [0,pi).
    :return: the given image after it was blurred by convolving it with a kernel as described above.
    """
    motion_blur_kernel = image_restoration_NNs_utils.motion_blur_kernel(kernel_size, angle)
    blurred_image = (convolve(image, motion_blur_kernel)).astype(np.float64)
    blurred_image = (np.around(blurred_image * 255)) / 255
    np.clip(blurred_image, 0, 1, out=blurred_image)
    return blurred_image
Beispiel #42
0
def expand(img, new_size=None):
    # This is what opencv pyrUp does. No idea how to do it myself.
    # Quote: "The function performs the upsampling step of the
    # Gaussian pyramid construction, though it can actually
    # be used to construct the Laplacian pyramid. First, it
    # upsamples the source image by injecting even zero rows
    # and columns and then convolves the result with the same
    # kernel as in pyrDown() multiplied by 4."
    new_size = new_size or (2 * img.shape[0], 2 * img.shape[1])
    image_up = np.zeros(
        new_size)  # odd number of rows or cols will get rounded
    # down during reduce. Here we simply make the output as big as desired,
    # hopefully no one will notice
    image_up[::2, ::2] = img
    return convolve(image_up, 4 * kernel, mode='constant')
Beispiel #43
0
 def transportRates(self, signalRates, signalLevels, boundcond='constant', mode='normal'):
     # Compute diffusion term, laplacian of grid levels in signalLevels, 
     # write into signalRates
     #
     # mode='greens' - do not use initLevels as these don't apply!
     signalRatesView = signalRates.reshape(self.gridDim)
     signalLevelsView = signalLevels.reshape(self.gridDim)
     advKernel = numpy.zeros((3,3,3))
     advKernel[:,1,1] = [-0.5,0,0.5]
     for s in range(self.nSignals):
         if boundcond=='constant' and self.initLevels and mode!='greens':
             boundval = self.initLevels[s]
         else:
             boundval = 0.0 
         if self.advRates:
             # Adevction term = du/dx
             # Note: always use 'nearest' edge case, this gives central 
             # differences in middle, and forward/backward differences on edges
             convolve(signalLevelsView[s], advKernel*self.advRates[s], output=signalRatesView[s], mode='nearest')
             # Diffusion term = \del^2u
             # Use edge case from boundary conditions for diffusion
             signalRatesView[s] += laplace(signalLevelsView[s], None, mode=boundcond, cval=boundval) * self.diffRates[s]
         else:
             signalRatesView[s] = laplace(signalLevelsView[s], None, mode=boundcond, cval=boundval) * self.diffRates[s]
Beispiel #44
0
def calc_energy(img):
    filter_dv = np.array([
        [1.0, 2.0, 1.0],
        [0.0, 0.0, 0.0],
        [-1.0, -2.0, -1.0],
    ])

    # transpose switches indices of rows and columns
    filter_du = np.transpose(filter_dv)

    # This converts from a 2D filter to a 3D filter, replicating the same
    # filter for each channel: R, G, B
    filter_du = np.stack([filter_du] * 3, axis=2)
    filter_dv = np.stack([filter_dv] * 3, axis=2)

    # Find each pixel's energy value
    img = img.astype('float32')
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))

    # We sum the energies in the red, green, and blue channels
    energy_map = convolved.sum(axis=2)

    return energy_map
def _coastline_classification_2(dataset, water_band='wofs'):
    import scipy.ndimage.filters as conv

    kern = np.array([[1, 1, 1], [1, 0.001, 1], [1, 1, 1]])
    convolved = conv.convolve(
        dataset[water_band], kern, mode='constant', cval=-999) // 1

    ds = dataset.copy(deep=True)
    ds.wofs.values[(~np.isnan(ds[water_band].values))
                   & (ds.wofs.values == 1)] = 1
    ds.wofs.values[convolved < 0] = 0
    ds.wofs.values[convolved > 6] = 0
    ds = ds.rename({"wofs": "coastline"})

    return ds
Beispiel #46
0
def harris_corners(img, window_size=3, k=0.04):
    """
    Compute Harris corner response map. Follow the math equation
    R=Det(M)-k(Trace(M)^2).

    Hint:
        You may use the function scipy.ndimage.filters.convolve, 
        which is already imported above
        
    Args:
        img: Grayscale image of shape (H, W)
        window_size: size of the window function
        k: sensitivity parameter

    Returns:
        response: Harris response image of shape (H, W)
    """

    H, W = img.shape
    window = np.ones((window_size, window_size))

    response = np.zeros((H, W))

    dx = filters.sobel_v(img)
    dy = filters.sobel_h(img)
    
    dx_prod = convolve(dx * dx, window)
    dy_prod = convolve(dy * dy, window)
    dx_dy_p = convolve(dx * dy, window)
    
    for x in range(H):
        for y in range(W):
            M = np.array([[dx_prod[x, y], dx_dy_p[x, y]], [dx_dy_p[x, y], dy_prod[x, y]]])
            response[x, y] = np.linalg.det(M) - k * (np.trace(M) ** 2)

    return response
Beispiel #47
0
def mark(img):
    lo = np.array([[-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]])
    core = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
    msk = img > 0
    convolve(msk, core, output=img)
    img *= msk
    np.clip(img, 0, 3, out=img)
    pts = np.array(np.where(img == 3)).T

    for p in pts:
        idx = (lo.T + p).T
        v = img[idx[0], idx[1]] > 0
        fac = np.array([1, 2, 4, 8, 16, 32, 64, 128])
        c = lut[np.dot(v, fac)]
        if c == 0: img[tuple(p)] = 0
    pts = np.array(np.where(img == 3)).T
    for p in pts:
        if img[tuple(p)] == 0: continue
        idx = (lo.T + p).T
        v = img[idx[0], idx[1]] > 0
        fac = np.array([1, 2, 4, 8, 16, 32, 64, 128])
        c = lut[np.dot(v, fac)]
        if c == 1: img[tuple(p)] = 2
    img[:] = np.array([0, 255, 128, 255], dtype=np.uint8)[img]
def num_neighbours(skel) -> np.ndarray:
    """Computes the number of neighbours of each skeleton pixel.

    Parameters
    ----------
    skel : (H, W) array_like
        Input skeleton image.

    Returns
    -------
    (H, W) array_like
        Array containing the numbers of neighbours at each skeleton pixel and 0 elsewhere.
    """
    skel = np.asarray(skel, dtype=int)
    return filters.convolve(skel, _NB_MASK, mode='constant') * skel
def iterate(a, k, m):
    while True:
        a_prev = np.copy(a)
        c = convolve(a, k, mode="constant")
        vacate_bool = (a == 1) & (c >= 4)
        fill_bool = (a == 0) & (c == 0)
        a[vacate_bool & m] = 0
        a[fill_bool & m] = 1

        print(np.sum(np.sum(a)))

        if np.array_equal(a, a_prev):
            break

    return a
Beispiel #50
0
    def __call__(self, tensor):
        """

        :param tensor: Tensor image os size (C, H, W) to be normalized.
        :return:
            Tensor: Normalized Tensor image, in size (C, H, W).
        """

        C, H, W = tensor.size()

        arr = np.array(tensor)
        arr_v = arr - np.sum(np.array([
            convolve(arr[c], self.kernel, mode=self.mode, cval=self.cval)
            for c in range(C)
        ]),
                             axis=0)
        # arr_v = arr - np.array([convolve(arr[c], self.kernel, mode=self.mode, cval=self.cval)
        #                           for c in range(C)])

        arr_sigma_square_stack = np.array([
            convolve(np.square(arr_v[c]),
                     self.kernel,
                     mode=self.mode,
                     cval=self.cval) for c in range(C)
        ])  # An array that has shape (C, H, W).
        # (c, h, w) element is the result of
        # convolution, at (h, w) coordinates,
        # with the Gaussian kernel(2 dim)
        # and arr_v[c] as input

        arr_sigma = np.sqrt(np.sum(arr_sigma_square_stack, axis=0))
        c = np.mean(arr_sigma)
        arr_v_divided_by_c = arr_v / c
        arr_v_divided_by_arr_sigma = arr_v / (arr_sigma)
        arr_y = np.maximum(arr_v_divided_by_c, arr_v_divided_by_arr_sigma)
        return torch.Tensor(arr_y)
Beispiel #51
0
def calc_energy(img):
    filter_du = np.array([
        [1.0, 2.0, 1.0],
        [0.0, 0.0, 0.0],
        [-1.0, -2.0, -1.0],
    ])
    # This converts it from a 2D filter to a 3D filter, replicating the same filter for each channel: R, G, B
    filter_du = np.stack([filter_du] * 3, axis=2)

    filter_dv = np.array([
        [1.0, 0.0, -1.0],
        [2.0, 0.0, -2.0],
        [1.0, 0.0, -1.0],
    ])
    filter_dv = np.stack([filter_dv] * 3, axis=2)

    img = img.astype('float32')
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))

    # Summing the energies in the red, green, and blue channels
    energy_map = convolved.sum(axis=2)

    return energy_map
def demosaic(im):
    # Assuming GBRG ordering.
    B = np.zeros_like(im)
    R = np.zeros_like(im)
    G = np.zeros_like(im)
    R[0::2, 1::2] = im[0::2, 1::2]
    B[1::2, 0::2] = im[1::2, 0::2]
    G[0::2, 0::2] = im[0::2, 0::2]
    G[1::2, 1::2] = im[1::2, 1::2]

    fG = np.asarray(
            [[0, 1, 0],
             [1, 4, 1],
             [0, 1, 0]]) / 4.0
    fRB = np.asarray(
            [[1, 2, 1],
             [2, 4, 2],
             [1, 2, 1]]) / 4.0

    im_color = np.zeros(im.shape+(3,), dtype='uint8') #RGB
    im_color[:, :, 0] = convolve(R, fRB)
    im_color[:, :, 1] = convolve(G, fG)
    im_color[:, :, 2] = convolve(B, fRB)
    return im_color
Beispiel #53
0
    def detect(self):
        imgs_final = []
        for i, img in enumerate(self.imgs):
            print(img.size, self.kernel_size, self.sigma)
            self.img_smoothed = convolve(
                img, self.gaussian_kernel(self.kernel_size, self.sigma))
            self.gradientMat, self.thetaMat = self.sobel_filters(
                self.img_smoothed)
            self.nonMaxImg = self.non_max_suppression(self.gradientMat,
                                                      self.thetaMat)
            self.thresholdImg = self.threshold(self.nonMaxImg)
            img_final = self.hysteresis(self.thresholdImg)
            self.imgs_final.append(img_final)

        return self.imgs_final
Beispiel #54
0
def harris_corners(img, window_size=3, k=0.04):
    """
    Compute Harris corner response map. Follow the math equation
    R=Det(M)-k(Trace(M)^2).

    Hint:
        You may use the function scipy.ndimage.filters.convolve,
        which is already imported above.

    Args:
        img: Grayscale image of shape (H, W)
        window_size: size of the window function
        k: sensitivity parameter

    Returns:
        response: Harris response image of shape (H, W)
    """

    H, W = img.shape
    window = np.ones((window_size, window_size))

    response = np.zeros((H, W))

    dx = filters.sobel_v(img)
    dy = filters.sobel_h(img)

    # YOUR CODE HERE
    # compute the matrices and filter with window
    dxdx_f = convolve(dx * dx, window)
    dxdy_f = convolve(dx * dy, window)
    dydy_f = convolve(dy * dy, window)
    # compute the response function
    response = dxdx_f * dydy_f - dxdy_f**2 - k * (dxdx_f + dydy_f)**2
    # END YOUR CODE

    return response
Beispiel #55
0
def get_energy(img):  #得到能量图
    filter_du = np.array([
        [1.0, 2.0, 1.0],
        [0.0, 0.0, 0.0],
        [-1.0, -2.0, -1.0],
    ])
    # 这会将它从2D滤波转换为3D滤波器
    # 为每个通道:R,G,B复制相同的滤波器
    filter_du = np.stack([filter_du] * 3, axis=2)

    filter_dv = np.array([
        [1.0, 0.0, -1.0],
        [2.0, 0.0, -2.0],
        [1.0, 0.0, -1.0],
    ])
    # 这会将它从2D滤波转换为3D滤波器
    # 为每个通道:R,G,B复制相同的滤波器
    filter_dv = np.stack([filter_dv] * 3, axis=2)
    img = img.astype('float32')
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))
    # 我们计算红,绿,蓝通道中的能量值之和
    energy_map = convolved.sum(axis=2)
    return energy_map
Beispiel #56
0
def add_motion_blur_image(image, kernel_size, angle):
    """
    :param image: a RGB image with values in the [0, 1] range of type float64.
    :param kernel_size: an odd integer specifying the size of the kernel (even integers are ill-defined).
    :param angle: an angle in radians in the range [0, π).
    :return:
    """
    # get kernel
    kernel = motion_blur_kernel(kernel_size, angle)

    #kernel_3d = np.dstack((kernel, kernel, kernel))

    # Convolve.
    result = convolve(image, kernel)
    return result
Beispiel #57
0
def calc_energy(img):
    # calculation of frequency energy after passed
    # through the convolution filter
    filter_du = np.array([
        [1.0, 2.0, 1.0],
        [0.0, 0.0, 0.0],
        [-1.0, -2.0, -1.0],
    ])
    filter_du = np.stack([filter_du] * 3, axis=2)

    filter_dv = np.array([
        [1.0, 0.0, -1.0],
        [2.0, 0.0, -2.0],
        [1.0, 0.0, -1.0],
    ])
    filter_dv = np.stack([filter_dv] * 3, axis=2)

    img = img.astype('float32')
    convolved = np.absolute(convolve(img, filter_du)) + np.absolute(
        convolve(img, filter_dv))

    energy_map = convolved.sum(axis=2)

    return energy_map
Beispiel #58
0
def opticalFlowDerevatives(img_1, img_2):
  mask_x = np.array([[-1, 1], [-1, 1]]) * 0.25
  mask_y = np.array([[-1, -1], [1, 1]]) * 0.25
  mask_t = -np.ones((2, 2)) * 0.25
    
  fx = convolve(img_1, mask_x) + convolve(img_2, mask_x)
  fy = convolve(img_1, mask_y) + convolve(img_2, mask_y)
  ft = convolve(img_1, mask_t) + convolve(img_2, -mask_t)

  return fx, fy, ft
def computeGradients(im1, im2):
    kernelX = np.array([[-1, 1], [-1, 1]]) * .25
    kernelY = np.array([[-1, -1], [1, 1]]) * .25

    fx = convolve(im1, kernelX) + convolve(im2, kernelX)
    fy = convolve(im1, kernelY) + convolve(im2, kernelY)
    ft = convolve(im1,
                  np.ones((2, 2)) * .25) + convolve(im2, -np.ones(
                      (2, 2)) * .25)
    return fx, fy, ft
Beispiel #60
0
def functional(img, kernel, real_img, alpha, noise=0):
    """Tikhonov regularization functional
        | Az+noise-u |^2 + alpha*BTV[z]
    Parameters
    ----------
    img: 2-D array
        input image
    kernel: 2-D array
        kernel image
    real_img: string, optional
        deconvolved image
    alpha: float, optional
        BTV parameter
    noise_level: float, optional
        input image noise level - [0,255]

    Returns
    -------
    deConv: 1x3 array
        Tikhonov regularization functional value at z (deconvolved image) point
    """
    n_channels = img.shape[2]
    strides = [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, -1), (-1, 1),
               (1, -1)]

    # | Az+noise-u |^2
    error = np.zeros(img.shape)

    for ch in range(n_channels):
        error[:, :, ch] = convolve(real_img[:, :, ch], kernel, mode="nearest")
    error += noise - img

    # Functional
    J = np.sum(error**2, axis=(0, 1))

    # Extrapolation
    real_img = np.vstack(
        (real_img[0, np.newaxis, :], real_img, real_img[-1, np.newaxis, :]))
    real_img = np.hstack(
        (real_img[:, 0, np.newaxis], real_img, real_img[:, -1, np.newaxis]))

    # BTV computation
    BTV = np.zeros(n_channels)
    for x, y in strides:
        sums = np.roll(real_img, (x, y), axis=(0, 1)) - real_img
        BTV += np.sum(sums, axis=(0, 1)) / np.hypot(x, y)

    return J + alpha * BTV