Esempio n. 1
0
def upsample_interp23(image, ratio):

    image = np.transpose(image, (2, 0, 1))

    b, r, c = image.shape

    CDF23 = 2 * np.array([
        0.5, 0.305334091185, 0, -0.072698593239, 0, 0.021809577942, 0,
        -0.005192756653, 0, 0.000807762146, 0, -0.000060081482
    ])
    d = CDF23[::-1]
    CDF23 = np.insert(CDF23, 0, d[:-1])
    BaseCoeff = CDF23

    first = 1
    for z in range(1, np.int(np.log2(ratio)) + 1):
        I1LRU = np.zeros((b, 2**z * r, 2**z * c))
        if first:
            I1LRU[:, 1:I1LRU.shape[1]:2, 1:I1LRU.shape[2]:2] = image
            first = 0
        else:
            I1LRU[:, 0:I1LRU.shape[1]:2, 0:I1LRU.shape[2]:2] = image

        for ii in range(0, b):
            t = I1LRU[ii, :, :]
            for j in range(0, t.shape[0]):
                t[j, :] = ndimage.correlate(t[j, :], BaseCoeff, mode='wrap')
            for k in range(0, t.shape[1]):
                t[:, k] = ndimage.correlate(t[:, k], BaseCoeff, mode='wrap')
            I1LRU[ii, :, :] = t
        image = I1LRU

    re_image = np.transpose(I1LRU, (1, 2, 0))

    return re_image
Esempio n. 2
0
File: ff.py Progetto: elout/lewd
        def step(self):
            """
            Perform single automaton step.
            """

            gsum = sum(self.grid)

            # Compute burn touched cells
            maximum(1, self.grid, self.burn_map)
            self.burn_map -= 1

            # Correlate cells for next set of fires
            correlate(self.burn_map, self.spread, mode='constant', cval=0,
                output=self.next_burn_map)

            # And cutoff at 1 and multiply by grid to remove
            # barren cells.
            self.next_burn_map *= self.grid
            minimum(1, self.next_burn_map, self.next_burn_map)

            # Finally ignite next set of trees and top at barren
            self.grid += self.next_burn_map
            self.grid += self.burn_map
            minimum(3, self.grid, self.grid)

            if p.sleep:
                __import__('time').sleep(p.sleep)

            # No more fire?
            return gsum < sum(self.grid)
Esempio n. 3
0
    def _compute_patch_size_of_cover_types(self, lct, footprint):
        """Computes the mean size of all patches of covertypes of interest
        that are (partially) within each cell's footprint"""
        eightway_structure = ones(
            (3, 3),
            dtype="int32")  # put this to class variable will result in error
        patchsizes = zeros(shape=lct.shape, dtype=float32)
        patchcount = zeros(shape=lct.shape, dtype=float32)

        labels, n = label(lct, eightway_structure)
        slices = find_objects(labels)
        # Summing the 0/1 mask gives the patch size
        for ip in range(n):
            locmask = where(labels[slices[ip]] == (ip + 1), lct[slices[ip]], 0)
            patchcount[slices[ip]] += locmask
            patchsizes[slices[ip]] += locmask.sum() * locmask
            del locmask
        pcount_corr = correlate(patchcount,
                                footprint,
                                mode="reflect",
                                cval=0.0)
        psize_corr = correlate(patchsizes, footprint, mode="reflect", cval=0.0)
        nonzeros = where(pcount_corr <> 0)
        result = zeros(psize_corr.shape, dtype=float32)
        result[nonzeros] = psize_corr[nonzeros] / pcount_corr[nonzeros]
        return result
Esempio n. 4
0
File: edge.py Progetto: sdsk/mahotas
def sobel(img):
    '''
    edges = sobel(img)


    Compute edges using Sobel's algorithm

    `edges` is a binary image of edges computed according to Matlab's
    implementation of Sobel's algorithm.


    Parameters
    ----------
      img : Any 2D-ndarray
    Returns
    -------
      edges : Binary image of edges
    '''
    # This is based on Octave's implementation,
    # but with some reverse engineering to match Matlab exactly
    img = img.astype(np.float)
    img = (img-img.min())/img.ptp()
    vfiltered = ndimage.correlate(img, _vsobel_filter, mode='nearest') # This emulates Matlab's implementation
    hfiltered = ndimage.correlate(img, _hsobel_filter, mode='nearest')
    filtered = vfiltered**2 + hfiltered**2
    thresh = 2*np.sqrt(filtered.mean())
    filtered *= (np.sqrt(filtered) < thresh)

    r,c = filtered.shape
    x = (filtered > np.hstack((np.zeros((r,1)),filtered[:,:-1]))) & (filtered > np.hstack((filtered[:,1:], np.zeros((r,1)))))
    y = (filtered > np.vstack((np.zeros(c),filtered[:-1,:]))) & (filtered > np.vstack((filtered[1:,:], np.zeros(c))))
    return x | y
Esempio n. 5
0
def harris_corner_detector(image, window_size, stddev, thresh):
    image_gray = rgb2gray(image)

    # CREATE GAUSSIAN WINDOW FILTER
    if window_size%2==0: window_size=window_size+1
    k = int((window_size-1)/2)
    window = np.zeros((window_size, window_size))
    window[k, k] = 1
    window = ndimage.gaussian_filter(window, sigma=stddev)

    # FIND IMAGE GRADIENT TERMS
    G, I_x, I_y = gradient(image_gray)
    I_xx = I_x * I_x
    I_yy = I_y * I_y
    I_xy = I_x * I_y

    # COMPUTE M
    M_11 = ndimage.correlate(I_xx, window, mode='nearest')
    M_12 = M_21 = ndimage.correlate(I_xy, window, mode='nearest')
    M_22 = ndimage.correlate(I_yy, window, mode='nearest')

    # COMPUTE CORNERNESS R
    alpha = 0.04
    R = (M_11 * M_22 - M_12 * M_21) - alpha * (M_11 + M_22)**2

    # THRESHOLD R
    threshold = thresh
    R[R<threshold] = 0

    # NON-LOCAL-MAXIMUM SUPRESSION
    Corners = find_local_maximum(R)

    return R, Corners
def convolveLayer(inputTensor, kernel, stride, padding=0):
    kernelSizeY, kernelSizeX = np.shape(kernel)
    strideX, strideY = getBinaryParameter(stride)
    paddingX, paddingY = getBinaryParameter(padding)

    #convert inputTensor's dim to 3
    inputShapeLen = len(inputTensor.shape)
    if inputShapeLen == 1:
        inputTensor = np.expand_dims(inputTensor, 0)
        inputTensor = np.expand_dims(inputTensor, 0)
        pass
    elif inputShapeLen == 2:
        inputTensor = np.expand_dims(inputTensor, 0)
    elif inputShapeLen == 3:
        pass
    else:
        #error
        pass

    # concatnate input array with padding
    inputTensor = concatnateInputWithPadding(inputTensor, paddingX, paddingY)

    resultTensor = None
    correlate(inputTensor, kernel, resultTensor, mode="constant")
    return resultTensor
Esempio n. 7
0
def createFilterBank():

    # Code to generate reasonable filter bank
    gaussianScales = [1, 2, 4, 8, math.sqrt(2) * 8]
    logScales = [1, 2, 4, 8, math.sqrt(2) * 8]
    dxScales = [1, 2, 4, 8, math.sqrt(2) * 8]
    dyScales = [1, 2, 4, 8, math.sqrt(2) * 8]

    filterBank = []

    for scale in gaussianScales:
        siz = 2 * math.ceil(scale * 2.5) + 1
        tmp = gaussian(shape=(siz, siz), sigma=scale)
        filterBank.append(tmp)

    for scale in logScales:
        siz = 2 * math.ceil(scale * 2.5) + 1
        tmp = laplacian_of_gaussian(shape=(siz, siz), sigma=scale)
        filterBank.append(tmp)

    xgrad = np.array([[-1, 0, 1]])
    for scale in dxScales:
        siz = 2 * math.ceil(scale * 2.5) + 1
        f = gaussian(shape=(siz, siz), sigma=scale)
        f = ndimage.correlate(f, xgrad)
        filterBank.append(f)

    for scale in dyScales:
        f = gaussian(shape=(siz, siz), sigma=scale)
        f = ndimage.correlate(f, xgrad.transpose())
        filterBank.append(f)

    return filterBank
Esempio n. 8
0
def interp23(image, ratio):
    if (2**round(np.log2(ratio)) != ratio):
        #        print 'Error: only resize factors of power 2'
        return

    b, r, c = image.shape

    CDF23 = 2 * np.array([
        0.5, 0.305334091185, 0, -0.072698593239, 0, 0.021809577942, 0,
        -0.005192756653, 0, 0.000807762146, 0, -0.000060081482
    ])
    d = CDF23[::-1]
    CDF23 = np.insert(CDF23, 0, d[:-1])
    BaseCoeff = CDF23

    first = 1
    for z in range(1, np.int(np.log2(ratio)) + 1):
        I1LRU = np.zeros((b, 2**z * r, 2**z * c))
        if first:
            I1LRU[:, 1:I1LRU.shape[1]:2, 1:I1LRU.shape[2]:2] = image
            first = 0
        else:
            I1LRU[:, 0:I1LRU.shape[1]:2, 0:I1LRU.shape[2]:2] = image

        for ii in range(0, b):
            t = I1LRU[ii, :, :]
            for j in range(0, t.shape[0]):
                t[j, :] = ndimage.correlate(t[j, :], BaseCoeff, mode='wrap')
            for k in range(0, t.shape[1]):
                t[:, k] = ndimage.correlate(t[:, k], BaseCoeff, mode='wrap')
            I1LRU[ii, :, :] = t
        image = I1LRU

    return image
Esempio n. 9
0
 def _activate(self, inputs, out, cval=np.nan):
     """Generate input activities for neurons (convolve and add bias)."""
     out[...] = 0.
     ndimage.correlate(inputs, self.filter, mode='constant', cval=cval, 
         output=out)
     if self.biases.size > 0:
         out[self.active_slice] += self.biases
Esempio n. 10
0
def rankBoxes(frame,
              boxSize,
              k=1,
              energyBlur=32,
              captionId=None,
              objBox=None,
              prevPositions={}):
    grayFrame = rgb2gray(frame)
    grayFrame = ndimage.gaussian_filter(grayFrame, 1.5, mode='nearest')
    height, width = boxSize
    halfHeight, halfWidth = math.ceil(height / 2), math.ceil(width / 2)

    frameHeight, frameWidth, _ = frame.shape

    # differentiation filter
    filterX = [[1, 0, -1], [1, 0, -1], [1, 0, -1]]
    filterY = [[1, 1, 1], [0, 0, 0], [-1, -1, -1]]
    gx = ndimage.correlate(grayFrame, filterX, mode='nearest')
    gy = ndimage.correlate(grayFrame, filterY, mode='nearest')

    energy = np.abs(gx) + np.abs(gy)

    if captionId is not None and captionId in prevPositions:
        energy[prevPositions[captionId]] -= energyBlur**2 * 512

    if objBox is not None:
        objX, objY, objWidth, objHeight = objBox
        midX, midY = objX + objWidth // 2, objY + objHeight // 2
        midX, midY = max(0, min(frameWidth - 1,
                                midX)), max(0, min(frameHeight - 1, midY))
        energy[objY, midX] -= 512 * energyBlur**2

    energy = ndimage.gaussian_filter(energy,
                                     energyBlur,
                                     mode='constant',
                                     cval=0)

    boxFilter = np.ones(boxSize)
    filtered = ndimage.correlate(energy, boxFilter, mode='constant', cval=0)

    if objBox is not None:
        objX, objY, objWidth, objHeight = objBox
        midX, midY = objX + objWidth // 2, objY + objHeight // 2
        midX, midY = max(0, min(frameWidth - 1,
                                midX)), max(0, min(frameHeight - 1, midY))
        filtered[:, :max(min(midX - objWidth, frameWidth - 1), 0)] += 10000
        filtered[:, max(min(midX + objWidth, frameWidth - 1), 0):] += 10000
        filtered[:max(min(objY - objHeight, frameHeight - 1), 0), :] += 10000
        filtered[max(min(objY + objHeight, frameHeight - 1), 0):, :] += 10000

    filtered[:halfHeight, :] = np.inf
    filtered[frameHeight - halfHeight:, :] = np.inf
    filtered[:, :halfWidth] = np.inf
    filtered[:, frameWidth - halfWidth:] = np.inf

    bestY, bestX = np.unravel_index(np.argmin(filtered), filtered.shape)
    if captionId is not None:
        prevPositions[captionId] = (bestY, bestX)

    return [(bestX - halfWidth, bestY - halfHeight)]
def get_part_scores(E,log_part_blocks,log_invpart_blocks,
                    frequency_mode='valid',time_mode='same'):
    part_block_shape = log_part_blocks[0].shape
    if time_mode=='valid':
        t_start = part_block_shape[0]/2
        t_end = t_start + E.shape[0] - part_block_shape[0] + 1
    else: # feature_mode == 'same'
        # TODO: check whether I should make the modes available
        #       type-safe
        t_start = 0
        t_end = E.shape[0]
    # check the frequency parameters
    if frequency_mode=='valid':
        f_start = part_block_shape[1]/2
        f_end = f_start + E.shape[1] - part_block_shape[1] + 1
    else: # frequency_mode == 'same'
        f_start = 0
        f_end = E.shape[1]
    e_pos = part_block_shape[2]/2
    return np.array([
            (ndimage.correlate(E,log_part_block)[t_start:t_end,
                                                 f_start:f_end,
                                                 e_pos]
             +ndimage.correlate(1-E,log_invpart_block)[t_start:t_end,
                                                       f_start:f_end,
                                                       e_pos])
            for log_part_block,log_invpart_block in zip(log_part_blocks,
                                                        log_invpart_blocks)])
Esempio n. 12
0
def makeCorrImg(movie, numNeighbours):
    # taken from epnev ca_source_extraction

    if numNeighbours == 8:
        neighbourhood = np.array(((1, 1, 1), (1, 0, 1), (1, 1, 1)))
    elif numNeighbours == 4:
        neighbourhood = np.array(((0, 1, 0), (1, 0, 1), (0, 1, 0)))
    neighbourhood = neighbourhood.reshape((1, 3, 3))

    d0, d1, d2 = movie.shape

    # centering
    mY = movie.mean(axis=0)
    movie = movie - mY

    # normalizing
    sY = np.sqrt(np.mean(movie**2, axis=0))
    movie = movie * (1 / sY)

    # compute the correlation
    Yconv = correlate(movie, neighbourhood)  # sum over the neighbouring pixels
    MASK = correlate(np.ones((1, d1, d2)),
                     neighbourhood)  # count the number of neighbouring pixels
    corrImg = np.mean(Yconv * movie,
                      axis=0) / MASK  # compute correlation and normalize
    corrImg = corrImg.reshape((d1, d2))

    return corrImg
Esempio n. 13
0
        def step(self):
            """
            Perform single automaton step.
            """

            gsum = sum(self.grid)

            # Compute burn touched cells
            maximum(1, self.grid, self.burn_map)
            self.burn_map -= 1

            # Correlate cells for next set of fires
            correlate(self.burn_map,
                      self.spread,
                      mode='constant',
                      cval=0,
                      output=self.next_burn_map)

            # And cutoff at 1 and multiply by grid to remove
            # barren cells.
            self.next_burn_map *= self.grid
            minimum(1, self.next_burn_map, self.next_burn_map)

            # Finally ignite next set of trees and top at barren
            self.grid += self.next_burn_map
            self.grid += self.burn_map
            minimum(3, self.grid, self.grid)

            if p.sleep:
                __import__('time').sleep(p.sleep)

            # No more fire?
            return gsum < sum(self.grid)
Esempio n. 14
0
    def compute(self, dataset_pool):
        constant = dataset_pool.get_dataset('constants')
        footprint = constant['FOOTPRINT']
        #denominator = dataset.get_attribute(self.footprint_size) - dataset.get_attribute(self.land_cover_type_ow_within_footprint)
        lct = ma.filled(
            self.get_dataset().get_2d_attribute(self.land_cover_types), 0)
        covertypes_of_interest = self._cover_type_translation(constant)
        is_lct_of_interest = reduce(
            lambda prev_answer, lct_num: logical_or(prev_answer, lct == lct_num
                                                    ), covertypes_of_interest,
            zeros(shape=lct.shape)).astype(int32)
        lct_mask = logical_not(self.get_dataset().get_mask(is_2d_version=True))

        temp = not_equal(lct, constant['OW'])

        denominator = correlate((temp & lct_mask).astype(int32),
                                footprint,
                                mode='reflect')
        #denominator = dataset.flatten_by_id(denominator);

        values = correlate(is_lct_of_interest, footprint, mode="reflect")

        #pct = dataset.get_attribute(self.land_cover_type_within_footprint).astype(float32) / \
        pct = values.astype(float32) / \
               ma.masked_where(denominator==0, denominator)
        pct = ma.filled(pct, 0.0)
        return self.get_dataset().flatten_by_id(arcsin(sqrt(pct)))
Esempio n. 15
0
def bf_fluo_correlate(embryo_idx,
                      bf_image,
                      fluo_image,
                      normalized=False,
                      smoothed=None,
                      dim=3):
    """Plot correlation between bf/fluo images, pixel-by-pixel or smoothed in tiles."""
    norm_str = ('N' if normalized else 'Unn') + 'ormalized'
    smooth_str = 'unsmoothed'
    if normalized:
        bf_image = normalize_img(bf_image)
        fluo_image = normalize_img(fluo_image)
    if smoothed:
        if smoothed == 'avg':
            weights = [[1 / (dim**2)] * dim] * dim
            bf_image = correlate(bf_image, weights)
            fluo_image = correlate(fluo_image, weights)
        elif smoothed == 'max':
            bf_image = maximum_filter(bf_image, size=dim)
            fluo_image = maximum_filter(fluo_image, size=dim)
        elif smoothed == 'min':
            bf_image = minimum_filter(bf_image, size=dim)
            fluo_image = minimum_filter(fluo_image, size=dim)
        else:
            raise Exception('smoothed can only be avg, max, or min')
        smooth_str = 'smoothed by ' + smoothed
    plt.scatter(bf_image.flatten(), fluo_image.flatten())
    plt.xlabel(f"{norm_str} bf frame val")
    plt.ylabel(f"{norm_str} fluo frame val")
    plt.title(f"Correlation plot for {smooth_str} embryo {embryo_idx}")
    plt.show()
Esempio n. 16
0
    def warp(self):
        if self.display:
            print 'Warp %d' % (self.counter, )

        u0, v0 = self.u.copy(), self.v.copy()

        idxx = self.idx + u0
        idyy = self.idy + v0

        idxx = np.clip(idxx, 0, self.N - 1)
        idyy = np.clip(idyy, 0, self.M - 1)

        I1warped = interp2linear(self.I1, idxx,
                                 idyy).astype(theano.config.floatX)
        if self.display:
            #plt.figure()
            #plt.imshow(I1warped)
            print "I1warped", I1warped
            print "I0", self.I0
            print "u0", u0
            print "v0", v0
            pass

        It = I1warped - self.I0
        if self.display:
            print "It", It

        Ix = ndimage.correlate(I1warped, self.mask, mode='nearest')
        Iy = ndimage.correlate(I1warped, self.mask.T, mode='nearest')

        # boundary handling
        m = (idxx > self.N - 1) | (idxx < 0) | (idyy > self.M - 1) | (idyy < 0)
        Ix[m] = 0.0
        Iy[m] = 0.0
        It[m] = 0.0

        self.Ix = Ix
        self.Iy = Iy
        self.It = It

        self.train.init(np.zeros_like(self.I0), np.zeros_like(self.I0), Ix, Iy,
                        It)
        cnt_step = 0
        while not self.train.done():
            e = self.train.step()[0]
            if self.display and (cnt_step % 10 == 0 or self.train.done()):
                print e,
            cnt_step += 1

        if self.display:
            print  # finish line

        self.u += self.train.tu.get_value()
        self.v += self.train.tv.get_value()
        self.counter += 1

        if self._intermediate_saver:
            self._intermediate_saver.save_members(self)
            self._intermediate_saver.save_locals(locals())
Esempio n. 17
0
def fusion_images(multispectral, panchromatic, save_image=False, savepath=None, timeCondition=True):
    end = 0
    start = 0
    #Verifica que ambas imagenes cumplan con las condiciones
    if multispectral.shape[2] == 3:
        print('The Multispectral image has '+str(multispectral.shape[2])+' channels and size of '+str(multispectral.shape[0])+'x'+str(multispectral.shape[1]))
    else:
        sys.exit('The first image is not multispectral')

    if len(panchromatic.shape) == 2:
        print(' The Panchromatic image has a size of '+str(panchromatic.shape[0])+'x'+str(panchromatic.shape[1]))
    else:
        sys.exit('The second image is not panchromatic')


    start=time.time()
    hsv = rgb2hsv(multispectral)
    val = hsv[:,:,2]
    sat = hsv[:,:,1]
    mat = hsv[:,:,0]

    pani = hist_match(panchromatic,val)

    s = np.array([[1/256, 1/64, 3/128, 1/64, 1/256],[1/64, 1/16, 3/32, 1/16, 1/64],[3/128, 3/32, 9/64, 3/32, 3/128],
                    [1/64, 1/16, 3/32, 1/16, 1/64],[1/256, 1/64, 3/128, 1/64, 1/256]])
    I1 = ndimage.correlate(pani, s, mode='constant')

    s1 = np.array([[1/256, 0, 1/64, 0, 3/128, 0, 1/64, 0, 1/256],[0, 0, 0, 0, 0, 0, 0, 0, 0],[1/64, 0, 1/16, 0, 3/32, 0, 1/16, 0, 1/64],[0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [3/128, 0, 3/32, 0, 9/64, 0, 3/32, 0, 3/128],[0, 0, 0, 0, 0, 0, 0, 0, 0],[1/64, 0, 1/16, 0, 3/32, 0, 1/16, 0, 1/64], [0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [1/256, 0, 1/64, 0, 3/128, 0, 1/64, 0, 1/256]])
    I2 = ndimage.correlate(I1, s1, mode='constant')

    W1=(pani-I1)
    W1 = adjust_values(W1)
    W1 = W1.astype(np.uint8)

    W2=(I1-I2)
    W2 = adjust_values(W2)
    W2 = W2.astype(np.uint8)

    nint=(panchromatic+W1+W2).astype(np.uint8)

    end = time.time()

    n_hsv1 = np.stack((mat, sat, nint),axis=2)
    n_hsv = n_hsv1
    fusioned_image = hsv2rgb(n_hsv).astype(np.uint8)
    if(save_image):
        # Guarda la imagen resultando de acuerdo al tercer parametro establecido en la linea de ejecución del script
        if(savepath != None):
            t = skimage.io.imsave(savepath+'/atrouscpu_image.tif',fusioned_image, plugin='tifffile')
        else:
            t = skimage.io.imsave('atrouscpu_image.tif',fusioned_image, plugin='tifffile')
    #time_calculated de ejecución para la transformada de Brovey en GPU
    time_calculated = (end-start)
    if(timeCondition):
        return {"image": fusioned_image, "time" :  time_calculated}
    else:
        return fusioned_image
Esempio n. 18
0
 def _compute_pct_cover_type_within_footprint(self, lct, cover_type, footprint, lct_mask):
     """Calculates percentage of one covertype within the footprint"""
     temp = equal(lct, cover_type)
     # use ma.masked_array to prevent divide-by-zero warnings
     mask_invert = logical_not(lct_mask)  # in numpy import ma.masked, 0 == valid
     pixels = ma.masked_array(correlate(mask_invert.astype(int32), footprint, mode="reflect"))
     values = correlate(temp.astype(int32), footprint, mode="reflect")
     return ma.filled(values / pixels.astype(float32), 0)
Esempio n. 19
0
    def warp(self):
        if self.display:
            print 'Warp %d' % (self.counter,)

        u0, v0 = self.u.copy(), self.v.copy()

        idxx = self.idx + u0
        idyy = self.idy + v0

        idxx = np.clip(idxx, 0, self.N-1)
        idyy = np.clip(idyy, 0, self.M-1)

        I1warped = interp2linear(self.I1, idxx, idyy).astype(theano.config.floatX)
        if self.display:
            #plt.figure()
            #plt.imshow(I1warped)
            print "I1warped", I1warped
            print "I0", self.I0
            print "u0", u0
            print "v0", v0
            pass

        It = I1warped - self.I0
        if self.display:
            print "It", It
            
        Ix = ndimage.correlate(I1warped, self.mask, mode='nearest')
        Iy = ndimage.correlate(I1warped, self.mask.T, mode='nearest')

        # boundary handling
        m = (idxx > self.N - 1) | (idxx < 0) | (idyy > self.M - 1) | (idyy < 0)
        Ix[m] = 0.0
        Iy[m] = 0.0
        It[m] = 0.0

        self.Ix = Ix
        self.Iy = Iy
        self.It = It

        self.train.init(np.zeros_like(self.I0), np.zeros_like(self.I0), Ix, Iy, It)
        cnt_step = 0
        while not self.train.done():
            e = self.train.step()[0]
            if self.display and (cnt_step % 10 == 0 or self.train.done()):
                print e,
            cnt_step += 1

        if self.display:
            print # finish line


        self.u += self.train.tu.get_value()
        self.v += self.train.tv.get_value()
        self.counter += 1

        if self._intermediate_saver:
            self._intermediate_saver.save_members(self)
            self._intermediate_saver.save_locals(locals())
def remove_cell_template(norm, features):
    if features['_fingers_grid']:
        # finger grid
        rh = int(round(features['finger_period_row']))
        cw = int(round(features['finger_period_col']))
        no_fingers = ndimage.uniform_filter(norm, size=(rh, cw))

        features['im_no_fingers'] = no_fingers

        # remove busbars
        no_bbs = no_fingers.copy()
        pixel_ops.InterpolateBBs(no_bbs,
                                 np.array(features['_busbar_cols'], np.int32),
                                 features['busbar_width'] + 6)
        features['im_no_figners_bbs'] = no_bbs

        if False:
            view = ImageViewer(norm)
            ImageViewer(no_fingers)
            ImageViewer(no_bbs)
            view.show()
    else:
        # remove fingers
        f_len = features['finger_period']
        f = np.ones((int(round(f_len)), 1), np.float32) / f_len
        no_lines = ndimage.correlate(norm, f)

        # sharpen
        F_LEN2 = int(round(1.5 * f_len))
        f2 = np.ones((1, F_LEN2), np.float32) / F_LEN2
        filtered = ndimage.correlate(norm, f2)
        filtered[filtered < 0.1] = 1.0
        if False:
            view = ImageViewer(norm)
            ImageViewer(no_lines)
            ImageViewer(filtered)
            view.show()

        edges = norm / filtered
        no_fingers = edges * no_lines
        features['im_no_fingers'] = no_fingers

        if '_busbar_cols' in features:
            # remove busbars
            no_bbs = no_fingers.copy()
            pixel_ops.InterpolateBBs(
                no_bbs, np.array(features['_busbar_cols'], np.int32),
                features['busbar_width'] + 6)
        else:
            no_bbs = no_fingers

        features['im_no_figners_bbs'] = no_bbs

        if False:
            view = ImageViewer(norm)
            ImageViewer(no_fingers)
            ImageViewer(no_bbs)
            view.show()
Esempio n. 21
0
File: deblur.py Progetto: 1zb/deconv
    def matvec(self, x):
        # the linear operator is P^T * P * x + mu * D^T * D * x
        x = x.copy()
        x.resize(self.spatial_size)

        return correlate(convolve(x, self.kernel), self.kernel).flatten(
        ) + self.rho * correlate(convolve(
            x, self.kernel_h), self.kernel_h).flatten() + self.rho * correlate(
                convolve(x, self.kernel_v), self.kernel_v).flatten()
Esempio n. 22
0
 def _activate(self, inputs, out, cval=np.nan):
     """Generate input activities for neurons (convolve and add bias)."""
     out[...] = 0.
     ndimage.correlate(inputs,
                       self.filter,
                       mode='constant',
                       cval=cval,
                       output=out)
     if self.biases.size > 0:
         out[self.active_slice] += self.biases
Esempio n. 23
0
def _mean_std(image, w):
    """Return local mean and standard deviation of each pixel using a
    neighborhood defined by a rectangular window size ``w``.
    The algorithm uses integral images to speedup computation. This is
    used by :func:`threshold_niblack` and :func:`threshold_sauvola`.

    Parameters
    ----------
    image : ndarray
        Input image.
    w : int, or iterable of int
        Window size specified as a single odd integer (3, 5, 7, …),
        or an iterable of length ``image.ndim`` containing only odd
        integers (e.g. ``(1, 5, 5)``).

    Returns
    -------
    m : ndarray of float, same shape as ``image``
        Local mean of the image.
    s : ndarray of float, same shape as ``image``
        Local standard deviation of the image.

    References
    ----------
    .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient
           implementation of local adaptive thresholding techniques
           using integral images." in Document Recognition and
           Retrieval XV, (San Jose, USA), Jan. 2008.
           :DOI:`10.1117/12.767755`
    """
    if not isinstance(w, Iterable):
        w = (w,) * image.ndim
    _validate_window_size(w)

    pad_width = tuple((k // 2 + 1, k // 2) for k in w)
    padded = np.pad(image.astype('float'), pad_width,
                    mode='reflect')
    padded_sq = padded * padded

    integral = integral_image(padded)
    integral_sq = integral_image(padded_sq)

    kern = np.zeros(tuple(k + 1 for k in w))
    for indices in itertools.product(*([[0, -1]] * image.ndim)):
        kern[indices] = (-1) ** (image.ndim % 2 != np.sum(indices) % 2)

    total_window_size = np.prod(w)
    sum_full = ndi.correlate(integral, kern, mode='constant')
    m = crop(sum_full, pad_width) / total_window_size
    sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant')
    g2 = crop(sum_sq_full, pad_width) / total_window_size
    # Note: we use np.clip because g2 is not guaranteed to be greater than
    # m*m when floating point error is considered
    s = np.sqrt(np.clip(g2 - m * m, 0, None))
    return m, s
Esempio n. 24
0
def _mean_std(image, w):
    """Return local mean and standard deviation of each pixel using a
    neighborhood defined by a rectangular window size ``w``.
    The algorithm uses integral images to speedup computation. This is
    used by :func:`threshold_niblack` and :func:`threshold_sauvola`.

    Parameters
    ----------
    image : ndarray
        Input image.
    w : int, or iterable of int
        Window size specified as a single odd integer (3, 5, 7, …),
        or an iterable of length ``image.ndim`` containing only odd
        integers (e.g. ``(1, 5, 5)``).

    Returns
    -------
    m : ndarray of float, same shape as ``image``
        Local mean of the image.
    s : ndarray of float, same shape as ``image``
        Local standard deviation of the image.

    References
    ----------
    .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient
           implementation of local adaptive thresholding techniques
           using integral images." in Document Recognition and
           Retrieval XV, (San Jose, USA), Jan. 2008.
           :DOI:`10.1117/12.767755`
    """
    if not isinstance(w, Iterable):
        w = (w,) * image.ndim
    _validate_window_size(w)

    pad_width = tuple((k // 2 + 1, k // 2) for k in w)
    padded = np.pad(image.astype('float'), pad_width,
                    mode='reflect')
    padded_sq = padded * padded

    integral = integral_image(padded)
    integral_sq = integral_image(padded_sq)

    kern = np.zeros(tuple(k + 1 for k in w))
    for indices in itertools.product(*([[0, -1]] * image.ndim)):
        kern[indices] = (-1) ** (image.ndim % 2 != np.sum(indices) % 2)

    total_window_size = np.prod(w)
    sum_full = ndi.correlate(integral, kern, mode='constant')
    m = crop(sum_full, pad_width) / total_window_size
    sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant')
    g2 = crop(sum_sq_full, pad_width) / total_window_size
    # Note: we use np.clip because g2 is not guaranteed to be greater than
    # m*m when floating point error is considered
    s = np.sqrt(np.clip(g2 - m * m, 0, None))
    return m, s
Esempio n. 25
0
        def weak_texture_mask(img, patchsize, thresh):
            if img.ndim < 3:
                img = np.expand_dims(img, 2)

            kh = np.expand_dims(
                np.transpose(np.vstack(np.array([-0.5, 0, 0.5]))), 2)
            imgh = correlate(img, kh, mode="nearest")
            imgh = imgh[:, 1:imgh.shape[1] - 1, :]
            imgh = imgh * imgh

            kv = np.expand_dims(np.vstack(np.array([-0.5, 0, 0.5])), 1)
            imgv = correlate(img, kv, mode="nearest")
            imgv = imgv[1:imgv.shape[0] - 1, :, :]
            imgv = imgv * imgv

            s = img.shape
            msk = np.zeros_like(img)

            for cha in range(s[2]):
                m = view_as_windows(img[:, :, cha], (patchsize, patchsize))
                m = np.zeros_like(
                    m.reshape(np.int(m.size / patchsize**2),
                              patchsize**2,
                              order="F").transpose())

                Xh = view_as_windows(imgh[:, :, cha],
                                     (patchsize, patchsize - 2))
                Xh = Xh.reshape(np.int(Xh.size /
                                       ((patchsize - 2) * patchsize)),
                                ((patchsize - 2) * patchsize),
                                order="F").transpose()

                Xv = view_as_windows(imgv[:, :, cha],
                                     (patchsize - 2, patchsize))
                Xv = Xv.reshape(np.int(Xv.size /
                                       ((patchsize - 2) * patchsize)),
                                ((patchsize - 2) * patchsize),
                                order="F").transpose()

                Xtr = np.expand_dims(
                    np.sum(np.concatenate((Xh, Xv), axis=0), axis=0), 0)

                p = Xtr < thresh[cha]
                ind = 0

                for col in range(0, s[1] - patchsize + 1):
                    for row in range(0, s[0] - patchsize + 1):
                        if p[:, ind]:
                            msk[row:row + patchsize - 1,
                                col:col + patchsize - 1, cha] = 1
                        ind = ind + 1

            # clean up
            img = np.squeeze(img)
            return np.squeeze(msk)
Esempio n. 26
0
 def _compute_pct_cover_type_within_footprint(self, lct, cover_type,
                                              footprint, lct_mask):
     """Calculates percentage of one covertype within the footprint"""
     temp = equal(lct, cover_type)
     # use ma.masked_array to prevent divide-by-zero warnings
     mask_invert = logical_not(
         lct_mask)  # in numpy import ma.masked, 0 == valid
     pixels = ma.masked_array(
         correlate(mask_invert.astype(int32), footprint, mode="reflect"))
     values = correlate(temp.astype(int32), footprint, mode="reflect")
     return ma.filled(values / pixels.astype(float32), 0)
Esempio n. 27
0
def step(G: np.ndarray, herd: complex):
    '''Perform one timestep for the given herd (east- or south-facing).'''
    G2 = G.copy()
    if herd == 1:
        nei = correlate(G, KERNEL.real, mode='wrap')
        G2[(G == 0) & (nei.real > 1)] = herd
    else:
        nei = correlate(G, KERNEL.imag, mode='wrap')
        G2[(G == 0) & (nei.imag > 1)] = herd
    G2[(G == herd) & (nei.imag % 2 == 0) & (nei.real % 2 == 0)] = 0
    return G2
Esempio n. 28
0
def local_normalize(image, ks1, sigma1, ks2, sigma2):
    #Generate gaussian kernel
    kernel1 = make_2d_gauss(ks1, sigma1)
    kernel2 = make_2d_gauss(ks2, sigma2)

    mu = correlate(image, kernel1)

    centered = image - mu

    sd = correlate(centered**2, kernel2)**0.5

    return centered / (sd + 1e-9)
Esempio n. 29
0
def bwmorph_thin(img,n_iter=0):
    # http://www.mathworks.com/help/images/ref/bwmorph.html#f1-500491
    # code adapted from skimage.morphology.skeletonize
    # FIXME support infinite iteration
    LUT=[0,0,0,0,0,1,0,1,0,0,0,0,0,1,3,1,0,0,0,0,2,0,2,0,0,0,
         0,0,3,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
         2,0,2,0,2,0,0,0,2,0,2,0,0,1,0,1,0,1,0,1,0,0,0,0,0,1,
         0,1,2,0,0,0,2,0,2,0,2,0,0,0,2,0,2,0,0,1,0,1,0,1,0,1,
         0,0,0,0,0,0,0,0,2,0,0,0,2,0,2,0,2,0,0,0,2,0,2,0,0,0,
         0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
         0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
         0,0,0,0,0,0,0,0,0,0,0,3,0,1,0,1,0,1,0,0,0,0,0,1,0,1,
         2,2,0,0,2,0,0,0,2,2,0,0,2,0,0,0,3,3,0,1,0,1,0,1,0,0,
         0,0,0,0,0,0,2,2,0,0,2,0,0,0,2,2,0,0,2,0,0,0]
    mask = np.array([[ 1, 2, 4],
                     [128, 0, 8],
                     [ 64, 32, 16]], np.uint8)

    skeleton = np.array(img).astype(np.uint8)
    
    done = False
    while(not(done)):
        # assign each pixel a unique value based on its foreground neighbours
        neighbours = ndimage.correlate(skeleton, mask, mode='constant')
        # ignore background
        neighbours *= skeleton
        # use LUT to extract code for deletion stages of thinning algorithm
        codes = np.take(LUT, neighbours)

        done = True
        # pass 1 - remove the 1's
        code_mask = (codes == 1)
        if np.any(code_mask):
            done = False
            skeleton[code_mask] = 0

        neighbours = ndimage.correlate(skeleton, mask, mode='constant')
        # ignore background
        neighbours *= skeleton
        # use LUT to extract code for deletion stages of thinning algorithm
        codes = np.take(LUT, neighbours)

        # pass 2, delete the 2's
        code_mask = (codes > 1)
        if np.any(code_mask):
            done = False
            skeleton[code_mask] = 0

        n_iter -= 1
        if n_iter == 0:
            done = True

    return skeleton.astype(np.bool)
Esempio n. 30
0
def _mean_std(image, w):
    """Return local mean and standard deviation of each pixel using a
    neighborhood defined by a rectangular window with size w times w.
    The algorithm uses integral images to speedup computation. This is
    used by threshold_niblack and threshold_sauvola.

    Parameters
    ----------
    image : ndarray
        Input image.
    w : int
        Odd window size (e.g. 3, 5, 7, ..., 21, ...).

    Returns
    -------
    m : 2-D array of same size of image with local mean values.
    s : 2-D array of same size of image with local standard
        deviation values.

    References
    ----------
    .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient
           implementation of local adaptive thresholding techniques
           using integral images." in Document Recognition and
           Retrieval XV, (San Jose, USA), Jan. 2008.
           DOI:10.1117/12.767755
    """
    if w == 1 or w % 2 == 0:
        raise ValueError("Window size w = %s must be odd and greater than 1." %
                         w)

    left_pad = w // 2 + 1
    right_pad = w // 2
    padded = np.pad(image.astype('float'), (left_pad, right_pad),
                    mode='reflect')
    padded_sq = padded * padded

    integral = integral_image(padded)
    integral_sq = integral_image(padded_sq)

    kern = np.zeros((w + 1, ) * image.ndim)
    for indices in itertools.product(*([[0, -1]] * image.ndim)):
        kern[indices] = (-1)**(image.ndim % 2 != np.sum(indices) % 2)

    sum_full = ndi.correlate(integral, kern, mode='constant')
    m = crop(sum_full, (left_pad, right_pad)) / (w**image.ndim)
    sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant')
    g2 = crop(sum_sq_full, (left_pad, right_pad)) / (w**image.ndim)
    # Note: we use np.clip because g2 is not guaranteed to be greater than
    # m*m when floating point error is considered
    s = np.sqrt(np.clip(g2 - m * m, 0, None))
    return m, s
Esempio n. 31
0
def _mean_std(image, w):
    """Return local mean and standard deviation of each pixel using a
    neighborhood defined by a rectangular window with size w times w.
    The algorithm uses integral images to speedup computation. This is
    used by threshold_niblack and threshold_sauvola.

    Parameters
    ----------
    image : ndarray
        Input image.
    w : int
        Odd window size (e.g. 3, 5, 7, ..., 21, ...).

    Returns
    -------
    m : 2-D array of same size of image with local mean values.
    s : 2-D array of same size of image with local standard
        deviation values.

    References
    ----------
    .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient
           implementation of local adaptive thresholding techniques
           using integral images." in Document Recognition and
           Retrieval XV, (San Jose, USA), Jan. 2008.
           DOI:10.1117/12.767755
    """
    if w == 1 or w % 2 == 0:
        raise ValueError(
            "Window size w = %s must be odd and greater than 1." % w)

    left_pad = w // 2 + 1
    right_pad = w // 2
    padded = np.pad(image.astype('float'), (left_pad, right_pad),
                    mode='reflect')
    padded_sq = padded * padded

    integral = integral_image(padded)
    integral_sq = integral_image(padded_sq)

    kern = np.zeros((w + 1,) * image.ndim)
    for indices in itertools.product(*([[0, -1]] * image.ndim)):
        kern[indices] = (-1) ** (image.ndim % 2 != np.sum(indices) % 2)

    sum_full = ndi.correlate(integral, kern, mode='constant')
    m = crop(sum_full, (left_pad, right_pad)) / (w ** image.ndim)
    sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant')
    g2 = crop(sum_sq_full, (left_pad, right_pad)) / (w ** image.ndim)
    # Note: we use np.clip because g2 is not guaranteed to be greater than
    # m*m when floating point error is considered
    s = np.sqrt(np.clip(g2 - m * m, 0, None))
    return m, s
    def adjoint(self, x, out=None):
        '''Returns D^{*}(y). The adjoint of convolution is convolution with 
        the PSF rotated by 180 degrees, or equivalently correlation by the PSF
        itself.'''

        if out is None:
            result = self.domain_geometry().allocate()
            result.fill(correlate(x.as_array(), self.PSF, mode='reflect'))
            return result
        else:
            outarr = out.as_array()
            correlate(x.as_array(), self.PSF, output=outarr, mode='reflect')
            out.fill(outarr)
Esempio n. 33
0
def animate(i):
    global X, Y, dp, gnd_p, sigma, all_patches, patch, cropax, all_patchax, N

    if i < N:  # If the animation is still running
        xn = imwarp(dp[i, :] + gnd_p)
        X[:, i] = xn.reshape(-1)
        Y[i] = np.exp(-np.dot(dp[i, :], dp[i, :]) / sigma)
        all_patches[(i * dsize[0]):((i + 1) * dsize[0]), :] = xn
        cropax.set_data(xn)
        all_patchax.set_data(all_patches.copy())
        all_patchax.autoscale()
        patch.set_xy(dp[i, :] + gnd_p)
        return [cropax, patch, all_patchax]
    else:  # Stuff to do after the animation ends
        # fig3d = plt.figure()
        # ax3d = fig3d.add_subplot(111, projection='3d')
        # ax3d.plot_surface(dpx.reshape(dsize), dpy.reshape(dsize),
        #                   Y.reshape(dsize), cmap=plt.get_cmap('coolwarm'))

        # Place your solution code for question 4.3 here
        # plt.show()
        data = np.load('X_Y_pairs.npz', X, Y)
        X = data['arr_0']
        Y = data['arr_1']
        pdb.set_trace()
        lamb = 0
        g = np.dot(np.linalg.inv(np.dot(X.T, X) + lamb * np.eye(X.shape[0])),
                   np.dot(X.T, Y))
        gr0 = g.reshape(dsize)
        output0 = correlate(img, gr0)
        lamb = 1
        g = np.dot(np.linalg.inv(np.dot(X.T, X) + lamb * np.eye(X.shape[0])),
                   np.dot(X.T, Y))
        gr1 = g.reshape(dsize)
        output1 = correlate(img, gr1)
        fig, axs = plt.subplots(1, 4, figsize=(6, 2))
        axs[0].imshow(gr0)
        axs[1].imshow(output0, cmap='gray')
        axs[2].imshow(gr1)
        axs[3].imshow(output1, cmap='gray')
        plt.show()

        output2 = convolve(img, gr0)
        output3 = convolve(img, gr1)
        fig, axs = plt.subplots(1, 2, figsize=(4, 2))
        axs[0].imshow(output2, cmap='gray')
        axs[1].imshow(output3, cmap='gray')
        plt.show()
        return []
Esempio n. 34
0
def SpatialCues(im):
    g = fgaussian([13, 13], np.sqrt(13))
    dy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
    vf = signal.convolve2d(g, dy, boundary='symm', mode='valid')
    sz = im.shape
    vC = np.full(sz[1:2], 0)
    hC = vC
    for b in range(sz[2]):
        vC = np.maximum(
            vC, abs(ndimage.correlate(im[:, :, b], vf, mode='nearest')))
        hC = np.maximum(
            hC,
            abs(ndimage.correlate(im[:, :, b], vf.transpose(),
                                  mode='nearest')))
    return hC, vC
Esempio n. 35
0
 def fbcorr(im, f, mode, stride, plugin=None, plugin_kwargs=None):
     assert mode=='same'
     if f.ndim == 3 and f.shape[0] == 1:
         f = f[0, :, :]
         output = correlate(im, f, mode='constant')[::stride, ::stride]
         return output
     elif f.ndim == 4:
         if f.shape[3] == 1:
             f = f[:, :, :, 0]
             output = [correlate(im, fi, mode='nearest')
                     [::stride, ::stride]
                 for fi in f]
             return np.asarray(output).transpose(1, 2, 0)
         else:
             raise NotImplementedError()
Esempio n. 36
0
File: deblur.py Progetto: 1zb/deconv
    def matvec(self, x):
        x = x.copy()
        x.resize(self.spatial_size)

        Dh = convolve(x, self.kernel_h)
        Dv = convolve(x, self.kernel_v)

        W_Dh = np.multiply(self.weight, Dh)
        Dht_W_Dh = correlate(W_Dh, self.kernel_h)

        W_Dv = np.multiply(self.weight, Dv)
        Dvt_W_Dv = correlate(W_Dv, self.kernel_v)

        return (correlate(convolve(x, self.kernel), self.kernel) + self.lmbda *
                (Dht_W_Dh + Dvt_W_Dv)).flatten()
Esempio n. 37
0
	def run(self, im, skin_thresh=[-1,1], n_peaks=3):
		'''
		im : color image
		'''
		im_skin = rgb2lab(im.astype(np.int16))[:,:,2]
		self.im_skin = im_skin
		# im_skin = skimage.exposure.equalize_hist(im_skin)
		# im_skin = skimage.exposure.rescale_intensity(im_skin, out_range=[0,1])
		im_skin *= im_skin > skin_thresh[0]
		im_skin *= im_skin < skin_thresh[1]

		skin_match_c = nd.correlate(-im_skin, self.hand_template)
		self.skin_match = skin_match_c

		# Display Predictions - Color Based matching
		optima = peak_local_max(skin_match_c, min_distance=20, num_peaks=n_peaks, exclude_border=False)
		# Visualize
		if len(optima) > 0:
			optima_values = skin_match_c[optima[:,0], optima[:,1]]
			optima_thresh = np.max(optima_values) / 2
			optima = optima.tolist()

			for i,o in enumerate(optima):
				if optima_values[i] < optima_thresh:
					optima.pop(i)
					break
		self.markers = optima

		return self.markers
Esempio n. 38
0
  def scipy_convolution( self ):
    """Performs the convolution without OpenCL, as a comparison"""

    im = self.im
    if self.larger_buffer:
      if self.dim == 1:
        im = self.im[self.offset:-self.offset]
      elif self.dim == 2:
        im = self.im[self.offset:-self.offset,self.offset:-self.offset]
      elif self.dim == 3:
        im = self.im[self.offset:-self.offset,self.offset:-self.offset,self.offset:-self.offset]
    
    tstart = time.time()
    
    if self.sep:
      if self.dim == 1:
        out = ndimage.correlate1d( input=im, weights=self.fil.tolist(), axis=0, mode='wrap', origin=0 )
      elif self.dim == 2:
        out = ndimage.correlate1d( input=im, weights=self.fil.tolist(), axis=0, mode='wrap', origin=0 )
        out = ndimage.correlate1d( input=out, weights=self.fil.tolist(), axis=1, mode='wrap', origin=0 )
      elif self.dim == 3:
        out = ndimage.correlate1d( input=im, weights=self.fil.tolist(), axis=0, mode='wrap', origin=0 )
        out = ndimage.correlate1d( input=out, weights=self.fil.tolist(), axis=1, mode='wrap', origin=0 )
        out = ndimage.correlate1d( input=out, weights=self.fil.tolist(), axis=2, mode='wrap', origin=0 )
    else:
      out = ndimage.correlate( im, self.fil, mode='wrap' )
    print "filtered scipy image", time.time() - tstart, "\n", out
    
    assert numpy.array_equal( out, self.out ), "The PyOpenCL result does not match with Scipy"
    def test_skeletonize_num_neighbours(self):
        # an empty image
        image = np.zeros((300, 300))

        # foreground object 1
        image[10:-10, 10:100] = 1
        image[-100:-10, 10:-10] = 1
        image[10:-10, -100:-10] = 1

        # foreground object 2
        rs, cs = draw.bresenham(250, 150, 10, 280)
        for i in range(10):
            image[rs + i, cs] = 1
        rs, cs = draw.bresenham(10, 150, 250, 280)
        for i in range(20):
            image[rs + i, cs] = 1

        # foreground object 3
        ir, ic = np.indices(image.shape)
        circle1 = (ic - 135)**2 + (ir - 150)**2 < 30**2
        circle2 = (ic - 135)**2 + (ir - 150)**2 < 20**2
        image[circle1] = 1
        image[circle2] = 0
        result = skeletonize(image)

        # there should never be a 2x2 block of foreground pixels in a skeleton
        mask = np.array([[1,  1],
                         [1,  1]], np.uint8)
        blocks = correlate(result, mask, mode='constant')
        assert not numpy.any(blocks == 4)
Esempio n. 40
0
    def compute(self, dataset_pool):
        constants = dataset_pool.get_dataset('constants')
        footprint = constants["FOOTPRINT"]
        covertypes_of_interest = self._cover_type_translation(constants)
        lct = ma.filled(self.get_dataset().get_2d_attribute(self.land_cover_type), 0)
        is_lct_of_interest = reduce(lambda prev_answer, lct_num: logical_or(prev_answer, lct==lct_num),
                                    covertypes_of_interest,
                                    zeros(shape=lct.shape, dtype=int32))
        # maxg: maximum number of possible like-adjacencies within a window
        # a_i: # of cells of target covertypes within moving window
        a_i = correlate(ma.filled(is_lct_of_interest.astype(int32), 0),
                               footprint, mode="reflect", cval=0.0)
        n = floor(sqrt(a_i))
        m = a_i - power(n, 2)
        maxg = 2*n*(n-1)
        maxg += where(m==0, 0, where(m<=n, (2*m-1), where(m>n, (2*m-2), 0)))

        # detect all adjacencies by systematically checking each possibility
        # within the 5x5 footprint window
        gii = ma.masked_array(zeros(shape=lct.shape, dtype=int32))
        for ri in range(0, footprint.shape[0]):
            for ci in range(0, footprint.shape[1]-1):
                gii += self._find_adjacency(is_lct_of_interest, (ri,ci), (ri,ci+1), footprint)

        for ri in range(0, footprint.shape[0]-1):
            for ci in range(0, footprint.shape[1]):
                gii += self._find_adjacency(is_lct_of_interest, (ri,ci), (ri+1,ci), footprint)

        #assert ma.alltrue(ravel(gii<=maxg))
        return self.get_dataset().flatten_by_id(arcsin(sqrt(ma.filled(gii/maxg, 0))))
Esempio n. 41
0
def fonts_template(fn=None,ttf=None):
    ttf = ttf or 'c:/windows/fonts/ariali.ttf'
    fn = fn or 'd:/temp/fonts.arrs'

    m = dict()
    font = ImageFont.truetype(ttf, 18)
    for e in  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        arr = font.getmask(e, mode='L')
        arr = Image.Image()._new(arr)
        arr = misc.fromimage(arr)
        h,w = arr.shape
        
        print '%s:<%s,%s> %s'%(e, h,w, arr[0,0])
        if w<10:
            tmp = numpy.ndarray( (h,10), dtype=arr.dtype )
            tmp.fill(0)
            i = (10-w)/2
            tmp[:,i:i+w] = arr
            arr = tmp
        #arr = arr[3:18,:]
        arr = arr[2:19,:]
        arr = im_norm(arr)
        rs = ndimage.correlate(arr,arr, mode='constant', cval=0.0)
        limit = numpy.max(rs)
        m[e] = (limit,arr)

    cPickle.dump(m, open(fn,'wb'))
Esempio n. 42
0
 def compute(self, dataset_pool):
     cellsize = dataset_pool.get_dataset('constants')['CELLSIZE']
     fpdimension = int(self.footprint_width / cellsize)
     fp = ones((fpdimension, fpdimension), dtype="int32")
     summed = correlate( ma.filled( self.get_dataset().get_2d_attribute( self.comm_add ), 0.0 ), \
                         fp, mode="reflect" )
     return ln(self.get_dataset().flatten_by_id(summed)+1)/10.0
def get_part_scores(E,log_part_blocks,log_invpart_blocks):
    part_block_shape = log_part_blocks[0].shape
    t_start = part_block_shape[0]/2
    t_end = t_start + E.shape[0] - part_block_shape[0] + 1
    f_start = part_block_shape[1]/2
    f_end = f_start + E.shape[1] - part_block_shape[1] + 1
    e_pos = part_block_shape[2]/2
    return np.array([
            (ndimage.correlate(E,log_part_block)[t_start:t_end,
                                            f_start:f_end,
                                            e_pos]
             +ndimage.correlate(1-E,log_invpart_block)[t_start:t_end,
                                            f_start:f_end,
                                            e_pos])
            for log_part_block,log_invpart_block in zip(log_part_blocks,
                                                        log_invpart_blocks)])
Esempio n. 44
0
def conway_4d(init: List[str], cycles=6, dim=4) -> int:
    shape = np.array([len(init), len(init[0])])
    shape = np.pad(shape, (dim - shape.size, 0), constant_values=1)

    space = np.array([list(l) for l in init])
    space = (space == "#").astype(int)
    space = np.reshape(space, shape)
    space = np.pad(space, cycles)

    kernel = np.array([0])
    kernel = np.reshape(kernel, np.ones(dim, dtype=int))
    kernel = np.pad(kernel, 1, constant_values=1)

    for _ in range(cycles):
        count = ndimage.correlate(space, kernel, mode="constant", cval=0)

        c2 = count == 2
        c3 = count == 3
        c23 = np.logical_or(c2, c3)

        # 1 -> 1
        c11 = np.logical_and(space == 1, c23)
        # 0 -> 1
        c01 = np.logical_and(space == 0, c3)

        space = np.logical_or(c11, c01).astype(int)

    return np.count_nonzero(space == 1)
 def compute(self, dataset_pool):
     constants = dataset_pool.get_dataset('constants')
     footprint = constants["FOOTPRINT"]
     lct = ma.filled(self.get_dataset().get_2d_attribute(self.land_cover_types), 0)
     temp = equal(lct, constants[self.lct_type.upper()])
     values = correlate(temp.astype(int32), footprint, mode="reflect")
     return self.get_dataset().flatten_by_id(ma.filled(values, 0))
Esempio n. 46
0
    def test_my_inputs(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='land_covers',
                            table_data={
                                'relative_x': array([1, 2, 1, 2]),
                                'relative_y': array([1, 1, 2, 2]),
                                "lct": array([10, 10, 4, 3])
                            })

        dataset_pool = DatasetPool(package_order=['biocomplexity'],
                                   storage=storage)
        footprint = array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
        dataset_pool._add_dataset('constant', {
            "FOOTPRINT": footprint,
            'AG': 10,
        })

        gridcell = dataset_pool.get_dataset('land_cover')
        gridcell.compute_variables(self.variable_name,
                                   dataset_pool=dataset_pool)
        values = gridcell.get_attribute(self.variable_name)

        should_be = array([[1, 1], [0, 0]])
        should_be = correlate(ma.filled(should_be.astype(int32), 0),
                              footprint,
                              mode="reflect")
        should_be = less_equal((should_be / 5.0), 400)
        should_be = ln(distance_transform_edt(should_be) +
                       1) / dag.standardization_constant_distance
        should_be = ravel(transpose(should_be))  # flatten by id

        self.assert_(ma.allclose(values, should_be, rtol=1e-7),
                     msg="Error in " + self.variable_name)
Esempio n. 47
0
    def test_skeletonize_num_neighbours(self):
        # an empty image
        image = np.zeros((300, 300))

        # foreground object 1
        image[10:-10, 10:100] = 1
        image[-100:-10, 10:-10] = 1
        image[10:-10, -100:-10] = 1

        # foreground object 2
        rs, cs = draw.line(250, 150, 10, 280)
        for i in range(10):
            image[rs + i, cs] = 1
        rs, cs = draw.line(10, 150, 250, 280)
        for i in range(20):
            image[rs + i, cs] = 1

        # foreground object 3
        ir, ic = np.indices(image.shape)
        circle1 = (ic - 135)**2 + (ir - 150)**2 < 30**2
        circle2 = (ic - 135)**2 + (ir - 150)**2 < 20**2
        image[circle1] = 1
        image[circle2] = 0
        result = skeletonize(image)

        # there should never be a 2x2 block of foreground pixels in a skeleton
        mask = np.array([[1, 1], [1, 1]], np.uint8)
        blocks = correlate(result, mask, mode='constant')
        assert not np.any(blocks == 4)
Esempio n. 48
0
    def Ahfunc(self, f):
        """Conjugate transform - convolve with conj. PSF"""
        fs = np.reshape(f, (self.height, self.width, self.depth))

        d = ndimage.correlate(fs, self.g)

        return np.ravel(d)
Esempio n. 49
0
    def Ahfunc(self, f):
        '''Conjugate transform - convolve with conj. PSF'''
        fs = reshape(f, (self.height, self.width, self.depth))

        d = ndimage.correlate(fs, self.g)
        
        return ravel(d)
Esempio n. 50
0
    def test_combo(self):
        """Test subsampling followed by convolution.
        """
        # Forward.
        var = Variable((2,3))
        kernel = np.array([[1,2,3]]) # 2x3
        fn = vstack([conv(kernel, subsample(var, (2,1)))])
        fn = CompGraph(fn)
        x = np.arange(6)*1.0
        x = np.reshape(x, (2, 3))
        out = np.zeros(fn.output_size)
        fn.forward(x.flatten(), out)
        y = np.zeros((1,3))

        xsub = x[::2,::1]
        y = ndimage.convolve(xsub, kernel, mode='wrap')

        self.assertItemsAlmostEqual(np.reshape(out, y.shape), y)

        # Adjoint.
        x = np.arange(3)*1.0
        x = np.reshape(x, (1, 3))
        out = np.zeros(var.size)
        fn.adjoint(x.flatten(), out)

        y = ndimage.correlate(x, kernel, mode='wrap')
        y2 = np.zeros((2,3))
        y2[::2,:] = y

        self.assertItemsAlmostEqual(np.reshape(out, y2.shape), y2)
        out = np.zeros(var.size)
        fn.adjoint(x.flatten(), out)
        self.assertItemsAlmostEqual(np.reshape(out, y2.shape), y2)
Esempio n. 51
0
def main():
    print 'loading data ...'
    ds = Util.loadData(r'E:\yalongli\Projects\RecoverOrientationData\ds_nx32ny32nz6.dat')
    qs = Util.loadData(r'E:\yalongli\Projects\RecoverOrientationData\qs_h12s4t3.dat')
    f = Util.loadData(r'E:\yalongli\Projects\RecoverOrientationData\volumeBin_04.dat')
    
    # small test volume
    f = f[:,:,75:85]
    print f.shape
    
    # Calculate 'J' on these 'q's one by one and store the max_J and max_q in
    # non-empty cells
    print 'initialzing J_max, d_max ...'
    inf = -999999.0
    J_max = np.ones_like(f) * inf
    d_max = np.zeros((f.shape[0], f.shape[1], f.shape[2], 3))
    
    print 'calculating J, d ...'
    for i in range(qs.shape[0]):
        J = ndimage.correlate(f, qs[i], mode='constant', cval=1.0) # background = 1.0!
        update = J > J_max
        J_max[update] = J[update]
        d_max[update] = ds[i]
        if i%100 == 0:
            print i, '...'
    
    # save non-empty cells' result, set empyt cells' direction to zero
    background = (f > 0.9)     
    d_max[background] = 0.0
    
    print 'saving ...'
    ori_p = r'E:\yalongli\Projects\RecoverOrientationData\orientationXYZ7585.dat'
    J_p = r'E:\yalongli\Projects\RecoverOrientationData\JXYZ7585.dat'
    Util.dumpData(d_max, ori_p)
    Util.dumpData(J_max, J_p)
Esempio n. 52
0
def calculate_vector(data1, data2):
    """
    Return translation vector based on correlation.
    """
    if data1.shape != data2.shape:
        raise ValueError("arrays must have equal shape.")
    data3 = ndimage.correlate(data1, data2, mode="constant")
    return [i - int(s / 2) for s, i in zip(data3.shape, np.unravel_index(data3.argmax(), data3.shape))]
Esempio n. 53
0
    def _correlate(self,arr, k, alpha=0.6):
        limit,V = self.fonts[k]
        rs = ndimage.correlate(arr,V, mode='constant', cval=0.0)
        h,w = rs.shape

        for ij in ( (i,j) for i in xrange(h) for j in xrange(w)):
            rs[ij] = rs[ij] if rs[ij]>limit*alpha else 0
        return ndimage.gaussian_filter(rs, 2)
Esempio n. 54
0
    def _compute_patch_size_of_cover_types(self, lct, footprint):
        """Computes the mean size of all patches of covertypes of interest
        that are (partially) within each cell's footprint"""
        eightway_structure = ones((3,3), dtype="int32") # put this to class variable will result in error
        patchsizes = zeros(shape=lct.shape, dtype=float32)
        patchcount = zeros(shape=lct.shape, dtype=float32)

        labels, n  = label(lct, eightway_structure)
        slices = find_objects(labels)
        # Summing the 0/1 mask gives the patch size
        for ip in range(n):
            locmask = where(labels[slices[ip]]==(ip+1),lct[slices[ip]],0)
            patchcount[slices[ip]] += locmask
            patchsizes[slices[ip]] += locmask.sum()*locmask
        pcount_corr = ma.masked_array(correlate(patchcount, footprint, mode="reflect", cval=0.0))
        psize_corr = correlate(patchsizes, footprint, mode="reflect", cval=0.0)
        return ma.filled(psize_corr / pcount_corr, 0)
Esempio n. 55
0
def abstract_within_walking_distance_gridcells(attribute, gridcells, filled_value=0, 
                        mode="reflect", x_name='relative_x', y_name='relative_y', **kwargs):
    wd_footprint = set_walking_distance_footprint(**kwargs)
    attr2d, index2d = get2Dattribute( attribute, gridcells, x_name=x_name, y_name=y_name)
    summed = ndi.correlate( np.ma.filled(attr2d, filled_value ), wd_footprint, mode=mode)
    attr2d[:] = summed
    res = pd.Series(attr2d.stack().reindex(index2d)["value"].values, index=gridcells.index)
    res[np.isnan(res)] = filled_value
    return res
Esempio n. 56
0
def find_perimeter(B):
    B = np.array(B).astype(np.bool) * 1
    """find boundaries via erosion and logical and,
    using four-connectivity"""
    #return B & np.invert(binary_erosion(B,FOUR))
    S = np.array([[ 0,-1, 0],
                  [-1, 4,-1],
                  [ 0,-1, 0]])
    return correlate(B,S) > 0
Esempio n. 57
0
 def _find_adjacency(self, lctmask, p1, p2, footprint):
     """helper function for aggregation index computation
     Detects a specific one-way adjacency within each cell's neighborhood,
     between the two points specified by the tuples p1 and p2."""
     adjfp = zeros(shape=footprint.shape, dtype=float32)
     adjfp[p1] = 0.5     # correlate with weights of 0.5 to count
     adjfp[p2] = 0.5        # adjacencies in one direction only
     # do correlation; setting output_type=int32 truncates result, skipping
     # pairs that are not "real" adjacencies
     return correlate(lctmask, adjfp, mode="constant", cval=0.0).astype(int32)
Esempio n. 58
0
def bwmorph_thin(B, n_iter=1):
    mask = np.array([[ 8,  4,  2],
                     [16,  0,  1],
                     [32, 64,128]],dtype=np.uint8)
    skel = np.array(B).astype(np.bool).astype(np.uint8)
    for n in range(n_iter):
        for lut in [G123_LUT, G123P_LUT]:
            N = correlate(skel, mask, mode='constant')
            D = np.take(lut,N)
            skel[D]=0
    return skel.astype(np.bool)
Esempio n. 59
0
def convolution(radius, image):
    '''Apply convolution to the image.'''  
    radius_sq = radius ** 2
    filt = zeros((radius * 2 + 1, radius * 2 + 1), dtype='int')
    
    for i in range(filt.shape[0]):
        for j in range(filt.shape[1]):
            if (i - radius) ** 2 + (j - radius) ** 2 < radius_sq:
                filt[i][j] = 1
    
    return correlate(image, filt.astype(float) / filt.sum(), mode='nearest')
Esempio n. 60
0
def computeFiberOrientation(v, ds, qs):
    neg_inf = -999999.0
    J_max = np.ones_like(v, dtype='float32') * neg_inf
    d_max = np.zeros((v.shape[0], v.shape[1], v.shape[2], 3), dtype='float32')

    for i in range(qs.shape[0]):
        J = ndimage.correlate(v, qs[i], mode='constant', cval=1.0)
        update = J > J_max
        J_max[update] = J[update]
        d_max[update] = ds[i]

    return (J_max, d_max)