Exemplo n.º 1
0
def vandermonde(X, M):
    xx = cp.ndarray(shape=(cp.size(X), M), dtype=float)
    for i in range(cp.size(X)):
        cnt = 1.0
        for j in range(M):
            xx[i, j] = float(cnt)
            cnt = cnt * X[i]
    return xx
Exemplo n.º 2
0
def spherical_cosmask(n,mask_radius, edge_width, origin=None):
    """mask = spherical_cosmask(n, mask_radius, edge_width, origin)
    """

    if type(n) is int:
        n = np.array([n])

    sz = np.array([1, 1, 1])
    sz[0:np.size(n)] = n[:]

    szl = -np.floor(sz/2)
    szh = szl + sz

    x,y,z = np.meshgrid( np.arange(szl[0],szh[0]), 
                         np.arange(szl[1],szh[1]), 
                         np.arange(szl[2],szh[2]), indexing='ij', sparse=True)

    r = np.sqrt(x*x + y*y + z*z)

    m = np.zeros(sz.tolist())

#    edgezone = np.where( (x*x + y*y + z*z >= mask_radius) & (x*x + y*y + z*z <= np.square(mask_radius + edge_width)))

    edgezone = np.all( [ (x*x + y*y + z*z >= mask_radius), (x*x + y*y + z*z <= np.square(mask_radius + edge_width))], axis=0)
    m[edgezone] = 0.5 + 0.5*np.cos( 2*np.pi*(r[edgezone] - mask_radius) / (2*edge_width))
    m[ np.all( [ (x*x + y*y + z*z <= mask_radius*mask_radius) ], axis=0 ) ] = 1

#    m[ np.where(x*x + y*y + z*z <= mask_radius*mask_radius)] = 1

    return m
def prepare_morph(img, p, operation):
    window_size = 2 * p - 1

    pad_size = int((p - 1) / 2)

    if operation == 'dilation':
        pad_value = 0
    else:
        pad_value = 255

    img = cp.pad(img, ((0, 0), (pad_size, pad_size)),
                 constant_values=pad_value)

    reconstruction_shape = (img.shape[0], img.shape[1])
    img = img.reshape(-1)
    n_window = int(np.floor(img.shape[0] / p))
    out = cp.zeros_like(img)

    if p % 2 == 0 and operation == 'erosion':
        img = cp.pad(img, (int(p / 2) - pad_size, 0),
                     constant_values=pad_value)

    required_padding = (2 * p - 1) - cp.size(img[(p * n_window - 1):-1])

    if required_padding > 0:
        img = cp.pad(img, (0, required_padding), constant_values=pad_value)

    required_blocks = int((n_window / 512) + 1)

    original_num_window = n_window
    if n_window > 512:
        thread_per_block = 512
        n_window = 512
    else:
        thread_per_block = n_window

    if 2 * n_window * p * 4 > dilation_cuda.max_dynamic_shared_size_bytes:
        max_window = int(
            np.floor(dilation_cuda.max_dynamic_shared_size_bytes /
                     (2 * p * 4)))
        required_blocks = int((original_num_window / max_window) + 1)
        n_window = max_window
        thread_per_block = max_window

    return [
        img, window_size, reconstruction_shape, pad_size, n_window, out,
        required_blocks, thread_per_block
    ]
Exemplo n.º 4
0
def RenderingUserViewLF_AllinOne5K(LF=None,
                                   LFDisparity=None,
                                   FB=None,
                                   viewpoint=None,
                                   DIR=None):

    sphereW = Params.WIDTH
    sphereH = Params.HEIGHT

    CENTERx = viewpoint.lon
    CENTERy = viewpoint.lat

    # output view is 3:4 ratio
    new_imgW = cp.floor(viewpoint.diag * 4 / 5 + 0.5)
    new_imgH = cp.floor(viewpoint.diag * 3 / 5 + 0.5)

    new_imgW = int(new_imgW)
    new_imgH = int(new_imgH)

    OutView = cp.zeros((new_imgH, new_imgW, 3))
    TYwarp, TXwarp = cp.mgrid[0:new_imgH, 0:new_imgW]

    TX = TXwarp
    TY = TYwarp
    TX = (TX - 0.5 - new_imgW / 2)
    TY = (TY - 0.5 - new_imgH / 2)

    #의심

    TX = TX + 1
    TY = TY + 1

    r = (viewpoint.diag / 2) / cp.tan(viewpoint.fov / 2)
    R = cp.sqrt(TY**2 + r**2)
    # Calculate LF_n
    ANGy = cp.arctan(-TY / r)
    ANGy = ANGy + CENTERy

    if (FB == 1):
        ANGn = cp.cos(ANGy) * cp.arctan(TX / r)
        ANGn = ANGn + CENTERx
        Pn = (Params.LFU_W / 2 - viewpoint.pos_y
              ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2)
    elif (FB == 2):
        ANGn = cp.cos(ANGy) * cp.arctan(-TX / r)
        ANGn = ANGn - CENTERx
        Pn = (Params.LFU_W / 2 + viewpoint.pos_y
              ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2)

    X = cp.sin(ANGy) * R
    Y = -cp.cos(ANGy) * R
    Z = TX

    ANGx = cp.arctan2(Z, -Y)
    RZY = cp.sqrt(Z**2 + Y**2)
    ANGy = cp.arctan(X / RZY)  #or ANGy = atan2(X, RZY);

    RATIO = 1
    ANGy = ANGy * RATIO

    ANGx = ANGx + CENTERx

    ANGx[abs(ANGy) > pi / 2] = ANGx[abs(ANGy) > pi / 2] + pi
    ANGx[ANGx > pi] = ANGx[ANGx > pi] - 2 * pi

    ANGy[ANGy > pi / 2] = pi / 2 - (ANGy[ANGy > pi / 2] - pi / 2)
    ANGy[ANGy < -pi / 2] = -pi / 2 + (ANGy[ANGy < -pi / 2] + pi / 2)

    Px = (ANGx + pi) / (2 * pi) * sphereW + 0.5
    Py = ((-ANGy) + pi / 2) / pi * sphereH + 0.5

    if (DIR == 2):
        Px = Px + Params.WIDTH / 4
    elif (DIR == 3):
        Px = Px + Params.WIDTH / 2
    elif (DIR == 4):
        Px = Px - Params.WIDTH / 4

    Px[Px < 1] = Px[Px < 1] + Params.WIDTH
    Px[Px > Params.WIDTH] = Px[Px > Params.WIDTH] - Params.WIDTH

    INDxx = cp.argwhere(Px < 1)
    Px[INDxx] = Px[INDxx] + sphereW

    Pn0 = cp.floor(Pn)
    Pn1 = cp.ceil(Pn)
    Pnr = Pn - Pn0

    Px0 = cp.floor(Px)
    Px1 = cp.ceil(Px)
    Pxr = Px - Px0

    Py0 = cp.floor(Py)
    Py1 = cp.ceil(Py)
    Pyr = Py - Py0

    Pnr = cp.rint((Pnr * 10000))
    Pnr = Pnr / 10000

    Pxr = cp.rint((Pxr * 10000))
    Pxr = Pxr / 10000

    Pyr = cp.rint((Pyr * 10000))
    Pyr = Pyr / 10000

    #210->012 rgb
    #cv2 사용 안하면 그대로
    OutView[:, :, 2] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0,
                                    Py1, 1)
    OutView[:, :, 1] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0,
                                    Py1, 2)
    OutView[:, :, 0] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0,
                                    Py1, 3)
    OutFlow = inter8_mat_flow5K(LFDisparity, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr,
                                Py0, Py1)

    Py = cp.pad(Py, [(1, 1), (0, 0)], mode='edge')
    Py = cp.ceil((Py * 10000))
    Py = Py / 10000

    My = 2 / (Py[2:cp.size(Py, 0), :] - Py[0:(cp.size(Py, 0) - 2), :])

    My[0, :] = My[0, :] / 2

    My[cp.size(My, 0) - 1, :] = My[cp.size(My, 0) - 1, :] / 2

    OutFlow = My * OutFlow

    return OutView, OutFlow
Exemplo n.º 5
0
def lfiltic(b, a, y, x=None):
    """
    Construct initial conditions for lfilter given input and output vectors.

    Given a linear filter (b, a) and initial conditions on the output `y`
    and the input `x`, return the initial conditions on the state vector zi
    which is used by `lfilter` to generate the output given the input.

    Parameters
    ----------
    b : array_like
        Linear filter term.
    a : array_like
        Linear filter term.
    y : array_like
        Initial conditions.

        If ``N = len(a) - 1``, then ``y = {y[-1], y[-2], ..., y[-N]}``.

        If `y` is too short, it is padded with zeros.
    x : array_like, optional
        Initial conditions.

        If ``M = len(b) - 1``, then ``x = {x[-1], x[-2], ..., x[-M]}``.

        If `x` is not given, its initial conditions are assumed zero.

        If `x` is too short, it is padded with zeros.

    Returns
    -------
    zi : ndarray
        The state vector ``zi = {z_0[-1], z_1[-1], ..., z_K-1[-1]}``,
        where ``K = max(M, N)``.

    See Also
    --------
    lfilter, lfilter_zi

    """
    N = cp.size(a) - 1
    M = cp.size(b) - 1
    K = cp.max(M, N)
    y = asarray(y)
    if y.dtype.kind in "bui":
        # ensure calculations are floating point
        y = y.astype(cp.float64)
    zi = zeros(K, y.dtype)
    if x is None:
        x = zeros(M, y.dtype)
    else:
        x = asarray(x)
        L = cp.size(x)
        if L < M:
            x = r_[x, zeros(M - L)]
    L = cp.size(y)
    if L < N:
        y = r_[y, zeros(N - L)]

    for m in range(M):
        zi[m] = cp.sum(b[m + 1:] * x[:M - m], axis=0)

    for m in range(N):
        zi[m] -= cp.sum(a[m + 1:] * y[:N - m], axis=0)

    return zi
Exemplo n.º 6
0
def pattern_changes_vec(W, W_0, x):
    return cp.divide(cp.sum(cp.sign(cp.dot(W, cp.transpose(x))) != cp.sign(cp.dot(W_0, cp.transpose(x)))), cp.size(W))