Example #1
0
def convolve_sep3(data, hx, hy, hz, res_g = None, sub_blocks = (1,1,1)):
    """convolves 3d data with kernel h = outer(hx,hy, hz)
    boundary conditions are clamping to edge.

    data, hx, hy.... are either np array or a gpu buffer (OCLArray)

    """

    if isinstance(data,np.ndarray):
        if sub_blocks == (1, 1, 1) or sub_blocks is None:
            return _convolve_sep3_numpy(data, hx, hy, hz)
        else:
            # cut the image into tile and operate on every of them
            N_sub = [int(np.ceil(1.*n/s)) for n,s  in zip(data.shape,sub_blocks)]
            Npads = [int(len(_h)/2) for _h in [hz,hy,hx]]
            res = np.empty(data.shape, np.float32)
            for i,(data_tile, data_s_src, data_s_dest)\
                in enumerate(tile_iterator(data,blocksize=N_sub,
                                     padsize=Npads,
                                     mode = "constant")):

                res_tile = _convolve_sep3_numpy(data_tile.copy(),
                                              hx,hy, hz)
                res[data_s_src] = res_tile[data_s_dest]
            return res


    elif isinstance(data,OCLArray):
        return _convolve_sep3_gpu(data,hx, hy, hz, res_g = res_g)
    else:
        raise TypeError("array argument (1) has bad type: %s"%type(data))
Example #2
0
def max_filter(data, size=10, res_g=None, sub_blocks=(1, 1, 1)):
    """
        maximum filter of given size

    Parameters
    ----------
    data: 2 or 3 dimensional ndarray or OCLArray of type float32
        input data
    size: scalar, tuple
        the size of the patch to consider
    res_g: OCLArray
        store result in buffer if given
    sub_blocks:
        perform over subblock tiling (only if data is ndarray)

    Returns
    -------
        filtered image or None (if OCLArray)
    """

    if np.isscalar(size):
        size = (size, ) * len(data.shape)

    if isinstance(data, np.ndarray):
        data = np.ascontiguousarray(data)
        if set(sub_blocks) == {1} or sub_blocks is None:
            return _max_filter_numpy(data, size)
        else:
            # cut the image into tile and operate on every of them
            N_sub = [
                int(np.ceil(1. * n / s))
                for n, s in zip(data.shape, sub_blocks)
            ]
            Npads = tuple(map(lambda x: x // 2, size))
            res = np.empty(data.shape, np.float32)
            for i, (data_tile, data_s_src, data_s_dest) \
                    in enumerate(tile_iterator(data, blocksize=N_sub,
                                               padsize=Npads,
                                               mode="constant")):
                res_tile = _max_filter_numpy(data_tile.copy(), size)
                res[data_s_src] = res_tile[data_s_dest]
            return res

    elif isinstance(data, OCLArray):
        return _max_filter_gpu(data, size=size, res_g=res_g)
    else:
        raise TypeError("array argument (1) has bad type: %s" % type(data))
Example #3
0
def convolve(data, h, res_g=None, sub_blocks=None, mode='constant'):
    """
    convolves 1d-3d data with kernel h 

    data and h can either be numpy arrays or gpu buffer objects (OCLArray, 
    which must be float32 then)

    boundary conditions are clamping to zero at edge.
    
    """
    sub_blocks = sub_blocks or (1, ) * data.ndim

    if not len(data.shape) in [1, 2, 3]:
        raise ValueError("dim = %s not supported" % (len(data.shape)))

    if len(data.shape) != len(h.shape):
        raise ValueError("dimemnsion of data (%s) and h (%s) are different" %
                         (len(data.shape), len(h.shape)))

    if isinstance(data, OCLArray) and isinstance(h, OCLArray):
        return _convolve_buf(data, h, res_g)
    elif isinstance(data, np.ndarray) and isinstance(h, np.ndarray):
        if sub_blocks == (1, ) * data.ndim and mode == 'constant':
            res = _convolve_np(data, h)
        else:
            # cut the image into tile and operate on every of them
            N_sub = [
                int(np.ceil(1. * n / s))
                for n, s in zip(data.shape, sub_blocks)
            ]
            Npads = [int(s / 2) for s in h.shape]
            res = np.empty(data.shape, np.float32)
            for data_tile, data_s_src, data_s_dest \
                    in tile_iterator(data, blocksize=N_sub,
                                     padsize=Npads,
                                     mode=mode):
                res_tile = _convolve_np(data_tile.copy(), h)
                res[data_s_src] = res_tile[data_s_dest]
        return res
    else:
        raise TypeError("unknown types (%s, %s)" % (type(data), type(h)))
Example #4
0
def convolve(data,h , res_g = None, sub_blocks = None):
    """
    convolves 1d-3d data with kernel h 

    data and h can either be numpy arrays or gpu buffer objects (OCLArray, 
    which must be float32 then)

    boundary conditions are clamping to zero at edge.
    
    """

    if not len(data.shape) in [1,2,3]:
        raise ValueError("dim = %s not supported"%(len(data.shape)))

    if len(data.shape) != len(h.shape):
        raise ValueError("dimemnsion of data (%s) and h (%s) are different"%(len(data.shape), len(h.shape)))

    if isinstance(data,OCLArray) and  isinstance(h,OCLArray):
        return _convolve_buf(data,h, res_g)
    elif isinstance(data,np.ndarray) and  isinstance(h,np.ndarray):
        if sub_blocks == (1,)*len(data.shape) or sub_blocks is None:
            return _convolve_np(data,h)
        else:
            # cut the image into tile and operate on every of them
            N_sub = [int(np.ceil(1.*n/s)) for n,s  in zip(data.shape,sub_blocks)]
            Npads = [int(s/2) for s in h.shape]
            res = np.empty(data.shape, np.float32)
            for data_tile, data_s_src, data_s_dest\
                in tile_iterator(data,blocksize=N_sub,
                                     padsize=Npads,
                                     mode = "constant"):

                res_tile = _convolve_np(data_tile.copy(),
                                        h)
                res[data_s_src] = res_tile[data_s_dest]
            return res
    else:
        raise TypeError("unknown types (%s, %s)"%(type(data),type(h)))