Пример #1
0
def lowpass(img, ksize, sigma):
    lp = np.atleast_2d(
        np.exp(-0.5 * np.square(np.arange(-ksize / 2, ksize / 2, 1) / sigma)))
    lp = lp / np.sum(lp)
    lp_img = conv2(conv2(img, lp, mode='same'), lp.T, mode='same')

    return lp, lp_img
Пример #2
0
def estimate_e(Ig, Jg, Gdx, Gdy, window_size):
    e = np.empty(np.shape(Gdx) + (2, ))
    window = np.ones(window_size)
    ij = Ig - Jg
    e[..., 0] = conv2(np.multiply(ij, Gdx), window, mode="same")
    e[..., 1] = conv2(np.multiply(ij, Gdy), window, mode="same")
    return e
Пример #3
0
def pairwise(img, opt):
    """Get edge weightings for LR-UD-DR-DL pairs"""

    img = np.mean(img.astype(np.float32), -1)
    img = np.pad(img, [[opt.fsz//2-1, opt.fsz//2],
                       [opt.fsz//2-1, opt.fsz//2]],
                 'symmetric')
    _x = np.arange(opt.fsz, dtype=np.float32) - (opt.fsz-1)/2
    _x, _y = np.meshgrid(_x, _x)

    gfx = _x*np.exp(-(_x**2+_y**2)/2/opt.fsgm)
    gfy = np.transpose(gfx)

    imx = conv2(img, gfx, 'valid')
    imy = conv2(img, gfy, 'valid')
    imdr, imdl = (imx+imy)**2, (imx-imy)**2
    imx, imy = imx**2, imy**2

    means = [np.mean(imx), np.mean(imy), np.mean(imdr), np.mean(imdl)]
    imx = np.exp(-imx / means[0] / opt.fsensitivity)
    imy = np.exp(-imy / means[1] / opt.fsensitivity)
    imdr = 0.5*np.exp(-imdr / means[2] / opt.fsensitivity)
    imdl = 0.5*np.exp(-imdl / means[3] / opt.fsensitivity)

    return imy[:-1, :], imx[:, :-1], imdr[:-1, :-1], imdl[:-1, 1:]
Пример #4
0
def dxdy_hist(img_gray, num_bins):
    assert len(img_gray.shape) == 2, 'image dimension mismatch'
    assert img_gray.dtype == 'float', 'incorrect image type'

    sigma, caps = 3, (-6, 6)
    kernel = gauss_module.gaussdx(sigma)[0]
    kernel = kernel[int((len(kernel) - 1) / 2) + caps[0]: int((len(kernel) + 1) / 2) + caps[1]]
    kernel = (kernel / kernel.sum()).reshape(1, kernel.shape[0])

    imgDx = conv2(img_gray, kernel, 'same')
    imgDy = conv2(img_gray, kernel.transpose(), 'same')

    # rescaling to [0, 255]
    def scale(img):
        img_flat = img.flatten()
        img_scaled = ((img - min(img_flat)) / (max(img_flat) - min(img_flat))) * 255
        return img_scaled

    imgDx, imgDy, imgDz = scale(imgDx), scale(imgDy), np.zeros_like(imgDx)
    img_concat = np.zeros(shape=(imgDx.shape[0], imgDx.shape[1], 3))
    img_concat[:, :, 0], img_concat[:, :, 1], img_concat[:, :, 2] = imgDx, imgDy, imgDz

    # Define a 2D histogram  with "num_bins^2" number of entries
    hists = rg_hist(img_concat.astype('double'), num_bins)

    return hists
Пример #5
0
def image_gradient(Im, ksize, sigma):
    (x,y) = np.meshgrid(np.arange(-(ksize-1)/2,(ksize-1)/2+1,1),np.arange(-(ksize-1)/2,(ksize-1)/2+1,1))
    g = 1/(2*np.pi*sigma**2)*np.exp(-(x**2+y**2)/(2*sigma**2))
    gdx = -(x/sigma**2)*g
    gdy = -(y/sigma**2)*g
    Imdx = conv2(Im, gdx, mode='same')
    Imdy = conv2(Im, gdy, mode='same')
    return (Imdx, Imdy)
Пример #6
0
def gaussianfilter(img, sigma):
    # 2D kernel made from applying twice convolution of 1D kernel
    Gx = gauss(sigma)[0] # 1D-x filter
    Gx = Gx.reshape(1, Gx.size)
    Gy = Gx.T # 1D-y filter
    # to speed up computations, decrease number of x points in gauss function
    smooth_img = conv2(conv2(img, Gx, 'same'), Gy, 'same')
    return smooth_img
Пример #7
0
def ImConv(I0, kernel):
    m, n, z = I0.shape
    I = np.zeros((m, n, 3))
    I[:, :, 0] = conv2(I0[:, :, 0], kernel, boundary='symm', mode='same')
    I[:, :, 1] = conv2(I0[:, :, 1], kernel, boundary='symm', mode='same')
    I[:, :, 2] = conv2(I0[:, :, 2], kernel, boundary='symm', mode='same')
    I = I.astype('uint8')
    return I
Пример #8
0
def regularized_values(I, J, ksize, sigma):
    lp, Ig = lowpass(I, ksize, sigma)
    lp, Jg = lowpass(J, ksize, sigma)
    df = np.atleast_2d(-1.0 / np.square(sigma) *
                       np.arange(-ksize / 2, ksize / 2, 1) * lp)
    Jdgx = conv2(conv2(Jg, df, mode='same'), lp.T, mode='same')
    Jdgy = conv2(conv2(Jg, lp, mode='same'), df.T, mode='same')
    return Ig, Jg, Jdgx, Jdgy
Пример #9
0
def gaussderiv(img, sigma, cap=False, smoothen_image=True):
    dx, x = gaussdx(sigma, cap)
    dx = dx.reshape(1, dx.shape[0])
    dy = dx.reshape(dx.shape[1], dx.shape[0])
    if smoothen_image:
        img = gaussianfilter(img, sigma)
    imgDx = conv2(img, dx, mode='same')
    imgDy = conv2(img, np.array(dy), mode='same')
    return imgDx, imgDy
Пример #10
0
def gaussderiv(img, sigma):
    Dx, x = gaussdx(sigma)

    Dx = Dx.reshape(1, Dx.size)

    imgDx = conv2(img, Dx, 'same')
    imgDy = conv2(img, Dx.T, 'same')

    return imgDx, imgDy
Пример #11
0
def sobel(img):
    S_x = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
    S_y = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
    G_x = conv2(img, S_x)
    G_y = conv2(img, S_y)

    S = np.sqrt(np.square(G_x) + np.square(G_y))

    return S
Пример #12
0
def seperableConv2(A, k, *args):
    '''
	perform a 2D convolution, assuming that the convolution kernel k is
	square-seperable.  In this case we may just do two 1D convolutions.
	'''
    center = array(k.shape) // 2
    k0 = k[center[0]:center[0] + 1, :]
    k1 = k[:, center[1]:center[1] + 1]
    return conv2(conv2(A, k0, *args), k1, *args)
Пример #13
0
def seperableConv2(A, k, *args):
    '''
	perform a 2D convolution, assuming that the convolution kernel k is
	square-seperable.  In this case we may just do two 1D convolutions.
	'''
    center = array(k.shape) // 2
    k0 = k[center[0]:center[0] + 1, :]
    k1 = k[:, center[1]:center[1] + 1]
    return conv2(conv2(A, k0, *args), k1, *args)
Пример #14
0
def extractDescriptors(pointlist, img, rotation=False):
    if (rotation):
        #repeat procedure from harris detector (could theoretically be passed back from harris detector but oh well
        #calculates the x and y gradients of the image
        g1 = harris.fspecial_gaussian([9, 9], 1)  # Gaussian with sigma_d
        img_blur = conv2(img, g1, 'same')  # blur image with sigma_d
        img_blur = img
        Ix = conv2(img_blur, np.array([[-1, 0, 1]]),
                   'same')  # take x derivative
        Iy = conv2(img_blur, np.transpose(np.array([[-1, 0, 1]])),
                   'same')  # take y derivative
    for p in pointlist:
        if (rotation):
            #            if(p.x < 29 or p.x > img.shape[])
            #calculate gradient orientation
            theta = np.degrees(np.arctan(np.divide(Iy[p.y, p.x], Ix[p.y,
                                                                    p.x])))
            #window needs to be big enough so that even if it's rotated by 45 degrees, a 40x40 window can be taken from the center
            half_edge = 29  #int(np.ceil(40 / np.sqrt(2)))*2=58, close enough
            window = img[(p.y - half_edge):(p.y + half_edge),
                         (p.x - half_edge):(p.x + half_edge)]
            rotated = transform.rotate(window, -theta)

            window_rotated = rotated[9:49, 9:49]
            window_resized = transform.resize(window_rotated, (8, 8),
                                              anti_aliasing=True)
        else:
            window = img[(p.y - 20):(p.y + 20), (p.x - 20):(p.x + 20)]
            window_resized = transform.resize(window, (8, 8),
                                              anti_aliasing=True)

        mean = np.mean(window_resized)
        sigma = np.std(window_resized)
        window_resized = np.divide(np.subtract(window_resized, mean), sigma)
        p.window = window_resized

        #window_sampled = sample(window, 5)
        #mean = np.mean(window_sampled)
        #sigma = np.std(window_sampled)
        #window_sampled = np.divide(np.subtract(window_sampled, mean), sigma)
        #p.window = window_sampled

        #        window_orig = img[(p.y-20) : (p.y+20), (p.x-20) : (p.x+20)]
        #        window_resized_orig = transform.resize(window, (8,8), anti_aliasing=True)
        #
        #        f, axarr = plt.subplots(1,5)
        #        axarr[0].imshow(window_orig)
        #        axarr[1].imshow(window_resized_orig)
        #        window_res_rot = transform.rotate(window_resized_orig, -theta)
        #        axarr[2].imshow(window_res_rot)
        #        axarr[3].imshow(window)
        #        axarr[4].imshow(window_resized)
        #        axarr[5].imshow(window_rot_res)
        plt.show()

    return pointlist
Пример #15
0
def compute_HL(L):
    h11 = np.atleast_2d(np.array([1, -2, 1]))
    h12 = np.array([[1 / 2, 0, -1 / 2], [0, 0, 0], [-1 / 2, 0, 1 / 2]])
    h22 = h11.T
    HL = np.empty(np.shape(L) + (2, 2))
    HL[..., 0, 0] = conv2(L, h11, mode="same")
    HL[..., 0, 1] = conv2(L, h12, mode="same")
    HL[..., 1, 0] = HL[..., 0, 1]
    HL[..., 1, 1] = conv2(L, h22, mode="same")
    return HL
Пример #16
0
def gaussderiv(img, sigma):

    G = gauss(sigma)
    D = gaussdx(sigma)
    img1 = conv2(img, D, 'same')
    imgDx = conv2(img1, G, 'same')
    img2 = conv2(img, G, 'same')
    imgDy = conv2(img2, D, 'same')

    return imgDx, imgDy
Пример #17
0
def filter_images2D(img2D, filter1, filter2, stride):
    M1, N1 = img2D.shape
    M2, N2 = filter1.shape
    O1, O2 = (M1 - M2 + stride) // stride, (N1 - N2 + stride) // stride
    out = np.empty((O1, O2))

    out = conv2(img2D, filter1, 'valid')[::stride, ::stride]
    out2 = conv2(img2D, filter2, 'valid')[::stride, ::stride]

    return np.sqrt(out**2 + out2**2)
Пример #18
0
def get_gradients(im):
    if len(im.shape) > 2:
        im = np.mean(im, axis=2)
    df = np.float32([[1, 0, -1]])
    sf = np.float32([[1, 2, 1]])
    gx = conv2(im, sf.T, 'same', 'symm')
    gx = conv2(gx, df, 'same', 'symm')
    gy = conv2(im, sf, 'same', 'symm')
    gy = conv2(gy, df.T, 'same', 'symm')
    return np.sqrt(gx * gx + gy * gy)
Пример #19
0
def image_gradient(I, ksize, sigma):
    lp = np.atleast_2d(np.exp(-0.5 *
                              (np.arange(-ksize, ksize + 1) / sigma)**2))
    lp = lp / np.sum(lp)
    df = np.atleast_2d(-1.0 / np.square(sigma) * np.arange(-ksize, ksize + 1) *
                       lp)
    Ig = conv2(conv2(I, lp, mode="same"), lp.T, mode="same")
    Igdx = conv2(Ig, df, mode="same")
    Igdy = conv2(Ig, df.T, mode="same")
    return Ig, Igdx, Igdy
Пример #20
0
def jacobiConvolutionU(U0, X, bX, Y, bY, F, N):
    # Two iterations of Jacobi method implemented using
    # a sum of convolutions

    U = conv2(U0, kU, 'same')      + \
        conv2(F, kF, 'same')       + \
        conv2(X-bX, kX)[1:-1,1:-1] + \
        conv2(Y-bY, kY)[1:-1,1:-1]

    return U / N
Пример #21
0
def estimate_Z(Gdx, Gdy, window_size):
    Gdx2 = np.square(Gdx)
    Gdxy = np.multiply(Gdx, Gdy)
    Gdy2 = np.square(Gdy)
    window = np.ones(window_size)
    Z = np.empty(np.shape(Gdx) + (2, 2))
    Z[..., 0, 0] = conv2(Gdx2, window, mode="same")
    Z[..., 0, 1] = conv2(Gdxy, window, mode="same")
    Z[..., 1, 0] = Z[..., 0, 1]
    Z[..., 1, 1] = conv2(Gdy2, window, mode="same")
    return Z
Пример #22
0
def gaussderiv(img, sigma):
    Gx = gauss(sigma)[0] # Gaussian 1D filter
    Dx = gaussdx(sigma)[0] # Gaussian x-derivative 1D filter
    Gx = Gx.reshape(1, Gx.size)
    Dx = Dx.reshape(1, Gx.size)
    Dy = Dx.T # Gaussian y-derivative 1D filter

    # image smoothed with std sigma and derived in x and y directions
    imgDx = conv2(conv2(img, Gx, 'same'), Dx, 'same')
    imgDy = conv2(conv2(img, Gx, 'same'), Dy, 'same')
    return imgDx, imgDy
Пример #23
0
def regularized_values(img, ksize, sigma):
    lp = np.atleast_2d(
        np.exp(-0.5 *
               np.square(np.arange(-ksize // 2, ksize // 2 + 1, 1) / sigma)))
    lp = lp / np.sum(lp)
    df = np.atleast_2d(-1.0 / np.square(sigma) *
                       np.arange(-ksize // 2, ksize // 2 + 1, 1) * lp)

    img_dx = conv2(conv2(img, df, mode="same"), lp.T, mode="same")
    img_dy = conv2(conv2(img, lp, mode="same"), df.T, mode="same")

    return (img_dx, img_dy)
Пример #24
0
def gaussderiv(img, sigma):
    Gx = gauss(sigma)[0]
    Gx = (Gx / Gx.sum())
    Gx = Gx.reshape(1, Gx.size)
    Dx = gaussdx(sigma)[0]
    Dx = (Dx / Dx.sum())
    Dx = Dx.reshape(1, Dx.size)

    imgDx = conv2(conv2(img, Gx.T, 'same'), Dx, 'same')
    imgDy = conv2(conv2(img, Gx, 'same'), Dx.T, 'same')

    return imgDx, imgDy
Пример #25
0
def connect(weak, strong, rate=0.2):
    pure_kernel = np.ones((3, 3))
    pure_kernel[1, 1] = 0

    edge_connected = conv2(strong, pure_kernel, mode='same') * weak
    for _ in range(5):
        edge_connected = conv2(edge_connected, pure_kernel, mode='same') * weak

    edge_final = strong + np.clip(edge_connected, 0, 1)
    edge = np.where(edge_final > rate, 1, 0)

    return edge
Пример #26
0
def grads(X):
    Dx = [[1, 0, -1], [2, 0, -2], [1, 0, -1]]
    Dy = np.transpose(Dx)
    #placeholder
    H = np.zeros(X.shape, dtype=np.float32)
    theta = np.zeros(X.shape, dtype=np.float32)
    Ix = conv2(X, Dx, mode='same', boundary='symm')
    Iy = conv2(X, Dy, mode='same', boundary='symm')
    #base on lec3 slide
    H = np.sqrt(np.square(Ix) + np.square(Iy))
    theta = np.arctan2(Iy, Ix)

    return H, theta
Пример #27
0
def get_column_d_filtered(X, ha, hb):

    (n, p) = X.shape

    if n % 4 > 0:
        raise ValueError('No. of rows in X must be a multiple of 4!')

    m = ha.shape[0]

    if not m == hb.shape[0]:
        raise ValueError('Lengths of ha and hb must be the same!')

    if m % 2 > 0:
        raise ValueError('Lengths of ha and hb must be even!')

    m2 = int(m / 2)

    xe = dtcwtu.reflect(np.arange(1 - m, n + m + 1), 0.5, n + 0.5)

    hao = ha[0:m:2]
    hae = ha[1:m:2]
    hbo = hb[0:m:2]
    hbe = hb[1:m:2]
    begin_t = 5
    end_t = n + 2 * m - 2

    n2 = int(n / 2)
    Y = np.zeros((n2, p))
    begin2 = 0
    end2 = n2
    begin1 = 1
    end1 = n2 + 1

    if np.sum(ha * hb) > 0:
        begin2 = 1
        end2 = n2 + 1
        begin1 = 0
        end1 = n2
    """
    print( 'Y[begin1:end1:2,:]', Y[begin1:end1:2,:] )
    print( 'xe[begin_t-1:end_t-1:4]', xe[begin_t-1:end_t-1:4] )
    print( 'xe[begin_t-3:end_t-3:4]', xe[begin_t-3:end_t-3:4] )
    print( 'X[xe[begin_t-1:end_t-1:4],:]', X[xe[begin_t-1:end_t-1:4],:] )
    print( 'X[xe[begin_t-3:end_t-3:4],:]', X[xe[begin_t-3:end_t-3:4],:] )
    """
    Y[begin1:end1:2,:] = conv2(X[xe[begin_t-1:end_t-1:4],:], hao, 'valid') + \
            conv2(X[xe[begin_t-3:end_t-3:4],:], hae, 'valid')
    Y[begin2:end2:2,:] = conv2(X[xe[begin_t:end_t:4],:], hbo, 'valid') + \
            conv2(X[xe[begin_t-2:end_t-2:4],:], hbe, 'valid')

    return Y
Пример #28
0
def ntod(nrm, mask, lmda):
    size = mask.shape
    mask = np.where(mask)
    fx = np.array([[0, 0, 0], [0.5, 0, -0.5], [0, 0, 0]])
    fy = np.array([[0, -0.5, 0], [0, 0, 0], [0, 0.5, 0]])
    fr = np.array([[-1 / 9, -1 / 9, -1 / 9], [-1 / 9, 8 / 9, -1 / 9],
                   [-1 / 9, -1 / 9, -1 / 9]])
    gx, gy = np.zeros(size), np.zeros(size)
    gx[mask] = -nrm[mask][:, 0] / np.maximum(1e-10, nrm[mask][:, 2])
    gy[mask] = -nrm[mask][:, 1] / np.maximum(1e-10, nrm[mask][:, 2])
    w = np.zeros(size)
    w[mask] = nrm[mask][:, 2]**2
    # init param
    Z = np.zeros(size)
    b = conv2(gx * w, -fx, 'same') + conv2(gy * w, -fy, 'same')
    r = b.copy()
    p = r.copy()
    for _ in range(500):
        Qp = conv2(conv2(p, fx, 'same') * w, -fx, 'same') + conv2(conv2(p, fy, 'same') * w, -fy, 'same') + \
             lmda * conv2(conv2(p, fr, 'same'), fr, 'same')
        alpha = np.sum(r**2) / np.sum(p * Qp)
        Z = Z + alpha * p
        r_new = r - alpha * Qp
        beta = np.sum(r_new**2) / np.sum(r**2)
        r = r_new
        p = r + beta * p
    return Z
Пример #29
0
def image_gradient(J, I, ksize, sigma):
    #    g = np.zeros(ksize,ksize)
    #    g[ksize//2, ksize//2] = 1
    #    g = scipy.ndimage.filters.gaussian_filter(g,sigma)
    (x, y) = np.meshgrid(np.arange(-(ksize - 1) / 2, (ksize - 1) / 2 + 1, 1),
                         np.arange(-(ksize - 1) / 2, (ksize - 1) / 2 + 1, 1))
    g = 1 / (2 * np.pi * sigma**2) * np.exp(-(x**2 + y**2) / (2 * sigma**2))
    gdx = -(x / sigma**2) * g
    gdy = -(y / sigma**2) * g
    Ig = conv2(I, g, mode='same')
    Jg = conv2(J, g, mode='same')
    Jgdx = conv2(J, gdx, mode='same')
    Jgdy = conv2(J, gdy, mode='same')
    return (Ig, Jg, Jgdx, Jgdy)
Пример #30
0
def ntod(nrm, mask, lmda):
    fx = np.array([[0, 0, 0], [0.5, 0, -0.5], [0, 0, 0]])
    fy = np.array([[0, -0.5, 0], [0, 0, 0], [0, 0.5, 0]])
    fr = np.array([[-1 / 9, -1 / 9, -1 / 9], [-1 / 9, 8 / 9, -1 / 9],
                   [-1 / 9, -1 / 9, -1 / 9]])
    Z = np.zeros(mask.shape)
    nx, ny, nz = nrm[:, :, 0], nrm[:, :, 1], nrm[:, :, 2]
    gx = np.where(mask > 0, -1 * (nx / nz), 0)
    gy = np.where(mask > 0, -1 * (ny / nz), 0)
    w = np.where(mask > 0, np.power(nz, 2), 0)
    b = conv2((gx * w), -fx, mode='same') + conv2((gy * w), -fy, mode='same')
    r = b
    p = r
    k = 0
    niter = 100

    while k < niter:
        Qx = conv2(conv2(p, fx, mode='same') * w, -fx, mode='same')
        Qy = conv2(conv2(p, fy, mode='same') * w, -fy, mode='same')
        Qr = lmda * conv2(conv2(p, fr, mode='same'), -fr, mode='same')
        Qp = Qx + Qy + Qr
        alpha = np.sum(r**2) / np.sum(p * Qp)
        Z = Z + alpha * p
        r_next = r - alpha * Qp
        beta = np.sum(r_next**2) / np.sum(r**2)
        p = r_next + beta * p
        r = r_next
        k = k + 1

    return Z
Пример #31
0
def gradient(im):
    # Sobel operator
    op1 = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
    op2 = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

    Gx = conv2(im, op1, mode='same', boundary='symm')
    Gy = conv2(im, op2, mode='same', boundary='symm')

    G = np.sqrt(Gx**2 + Gy**2)
    # embed()
    # G[np.where(G < np.max(G) * 0.2)] = 0
    Theta = np.arctan2(Gy, Gx)
    # print(G)
    return G, Theta
Пример #32
0
def  WeinerFilter():
  e1 = cv2.getTickCount()
  astro = color.rgb2gray(data.imread(imagePath))
  psf = np.ones((5, 5)) / 25
  astro = conv2(astro, psf, 'same')
  astro += 0.1 * astro.std() * np.random.standard_normal(astro.shape)

  deconvolved, _ = restoration.unsupervised_wiener(astro, psf)

  fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 5), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})

  plt.gray()

  ax[0].imshow(astro, vmin=deconvolved.min(), vmax=deconvolved.max())
  ax[0].axis('off')
  ax[0].set_title('Original Picture')

  ax[1].imshow(deconvolved)
  ax[1].axis('off')
  ax[1].set_title('Self tuned restoration')

  fig.subplots_adjust(wspace=0.02, hspace=0.2,
                      top=0.9, bottom=0.05, left=0, right=1)
  e2 = cv2.getTickCount()
  time = (e2 - e1)/ cv2.getTickFrequency() + (numberThreads) + numberThreadsPerCore + numberCores
  msgbox(msg= str(time) +" seconds", title="Execution Time", ok_button="OK")
  plt.show()
Пример #33
0
def noise(x, freq, temp=0, ker=ker2):
    s1 = int(sqrt(shape(x)[-1]))
    
    T  = shape(x)[0]
    shape_x = (T, s1, s1)

    y  = zeros(shape_x,'d')

    moi = array(rand(T+temp,s1,s1)<freq,'d')
    noi = zeros(shape_x)

    for t in range(temp+1):
        noi+=moi[t:t+T]
    # python isn't too shabby. awesome.
    noi.putmask(1, noi>1)
    

    for t in range(T):
        y[t,:,:]  = conv2(noi[t,:,:], ker, 'same')

    y = y.reshape(shape(x))

    y+=x #x is the video to noise, after all :)

    y.putmask(1, y>1)

    return y 
Пример #34
0
def center(clean_pupil, radius):
    """Center is the point of max conv with a disk."""
    
    # Find the small region that contains the pupil
    left, right, up, down = 0, 0, 0, 0
    for i in range(clean_pupil.shape[0]):
        for j in range(clean_pupil.shape[1]):
            if clean_pupil[i,j] != 0:
                left = min(i, left)
                up = min(j, up)
                right = max(i, right)
                down = max(j, down)
    left = max(left - radius, 0)
    right = min(right + radius, clean_pupil.shape[0] - 1)
    up = max(up - radius, 0)
    down = min(down + radius, clean_pupil.shape[1] - 1)

    small_pupil = clean_pupil[left:right, up:down]

    # Convolution of cleaned pupil with a disk
    cp = np.zeros(small_pupil.shape)
    cp[small_pupil] = 1
    d = disk(radius)
    t4 = conv2(cp, d, mode='same')
    
    # Find center as the max of the convolution
    # product
    maxi = np.amax(t4)
    Y = []
    most_left, most_right = clean_pupil.shape[0], 0
    for i in range(t4.shape[0]):
        for j in range(t4.shape[1]):
            if t4[i,j] == maxi:
                Y.append(j)
            if clean_pupil[i,j] != 0:
                most_left = min(most_left, i)
                most_right = max(most_left, i)

    y_max = np.mean(Y)
    y_max += up
    x_max = (most_right - most_left) // 2 + most_left
    
    return x_max, y_max
Пример #35
0
def weiner_filter(img,psf):
    astro = color.rgb2gray(data.astronaut())

    print "running Weiner for a channel"

    out = np.ndarray(shape=img.shape, dtype=np.float64)  # redifine to correct type
    out = np.copy(img)
    min_intensity = np.amin(img)
    out = out - min_intensity
    max_intensity = np.amax(out)

    out = out * (1.0 / max_intensity)

    img = out
    #psf = np.ones((5, 5)) / 25.0
    astro = conv2(astro, psf, 'same')
    astro += 0.22 * astro.std() * np.random.standard_normal(astro.shape)

    #deconvolved, _ = restoration.unsupervised_wiener(img, psf)
    #deconvolved  = restoration.wiener(img, psf, 500)
    deconvolved  = restoration.richardson_lucy(img, psf, 200)

    return deconvolved
Пример #36
0
def regiongrow(crop, xs, ys):
    
        crop = numpy.int32(crop)
        # compute edge intensity
        eimg = sobel(crop)
        # define kernel for different orientation of edges
        k1 = numpy.asarray([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
        k2 = numpy.asarray([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
        k3 = numpy.asarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        k4 = numpy.asarray([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        # edge at different orientations
        c1 = conv2(eimg, k1, 'same')
        c2 = conv2(eimg, k2, 'same')
        c3 = conv2(eimg, k3, 'same')
        c4 = conv2(eimg, k4, 'same')
        # combine them
        combimg = numpy.maximum(c1, numpy.maximum(c2, numpy.maximum(c3, c4)))

        # color difference in difference directions
        d3 = [[-1, -1], [1, 1]]
        dis3 = numpy.absolute(conv2(crop, d3, 'same'))
        d4 = [[1, 1], [-1, -1]]
        dis4 = numpy.absolute(conv2(crop, d4, 'same'))
        d1 = [[-1, 1], [-1, 1]]
        dis1 = numpy.absolute(conv2(crop, d1, 'same'))
        d2 = [[1, -1], [1, -1]]
        dis2 = numpy.absolute(conv2(crop, d2, 'same'))

        # initialize label image
        label = numpy.zeros_like(crop)
        label[0, :] = 1
        label[-1, :] = 1
        label[:, 0] = 1
        label[:, -1] = 1
        label[xs, ys] = 2

        # dilation kernel for different directions    
        selem1 = [[0, 1]]
        selem2 = [[1, 1, 0]]
        selem3 = [[0], [1]]
        selem4 = [[1], [1], [0]]
        maxe = numpy.percentile(numpy.abs(eimg), 80)

        i = 0
        while 1:
                i += 1 
                # dilation image: new regions
                dulllabel = numpy.uint8(label>0)
                nimg1 = dilation(dulllabel, selem1)  
                nimg2 = dilation(dulllabel, selem2) 
                nimg3 = dilation(dulllabel, selem3) 
                nimg4 = dilation(dulllabel, selem4) 

                # color differences at new regions
                diffimg1 = numpy.multiply(dis1, nimg1)
                diffimg2 = numpy.multiply(dis2, nimg2)
                diffimg3 = numpy.multiply(dis3, nimg3)
                diffimg4 = numpy.multiply(dis4, nimg4)

                # total dilation image
                nimg = dilation(dulllabel) - dulllabel 
                # cost function
                tcost = eimg + diffimg1 + diffimg2 + diffimg3 + diffimg4 + (nimg == 0)*maxint

                gp = numpy.argmin(tcost)
                xp, yp = numpy.unravel_index(gp, tcost.shape)

                # grows
                seedlabel = max([label[xp-1, yp], label[xp+1, yp], label[xp, yp-1], label[xp, yp+1]])
                label[xp, yp] = seedlabel
                if not i%100:
                        if not numpy.sum(label == 2):
                                return numpy.zeros_like(label)                                

                        if numpy.sum(numpy.multiply(numpy.uint8(label==2), numpy.abs(eimg)))/numpy.sum(label==2) > maxe :
                                return label == 2
                        if numpy.sum(label == 0) == 0:
                                if numpy.sum(numpy.multiply(numpy.uint8(label==2), numpy.abs(eimg)))/numpy.sum(label==2) > maxe * 0.5:
                                        return label == 2
                                else:
                                        return numpy.zeros_like(label)                                
Пример #37
0
the noise power and the image frequency power.

.. [1] François Orieux, Jean-François Giovannelli, and Thomas
       Rodet, "Bayesian estimation of regularization and point
       spread function parameters for Wiener-Hunt deconvolution",
       J. Opt. Soc. Am. A 27, 1593-1607 (2010)
"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import color, data, restoration

lena = color.rgb2gray(data.lena())
from scipy.signal import convolve2d as conv2
psf = np.ones((5, 5)) / 25
lena = conv2(lena, psf, 'same')
lena += 0.1 * lena.std() * np.random.standard_normal(lena.shape)

deconvolved, _ = restoration.unsupervised_wiener(lena, psf)

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 5))

plt.gray()

ax[0].imshow(lena, vmin=deconvolved.min(), vmax=deconvolved.max())
ax[0].axis('off')
ax[0].set_title('Data')

ax[1].imshow(deconvolved)
ax[1].axis('off')
ax[1].set_title('Self tuned restoration')
Пример #38
0
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import numpy as np
from scipy.signal import convolve2d as conv2

z = np.zeros((8, 8), dtype="float")
z[0:8:2, 0:8:2] = 1
kernal = np.array([[0.25, 0.5, 0.25], [0.5, 1, 0.5], [0.25, 0.5, 0.25]])
# print kernal
# z1=conv2(z,kernal)
# print z1

# z2=conv2(z,kernal,boundary='fill', mode='same')
# print z2
# print z2.shape

z2 = conv2(z, kernal, boundary="wrap", mode="same")
print z
print z2
Пример #39
0
the noise power and the image frequency power.

.. [1] François Orieux, Jean-François Giovannelli, and Thomas
       Rodet, "Bayesian estimation of regularization and point
       spread function parameters for Wiener-Hunt deconvolution",
       J. Opt. Soc. Am. A 27, 1593-1607 (2010)
"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import color, data, restoration

astro = color.rgb2gray(data.astronaut())
from scipy.signal import convolve2d as conv2
psf = np.ones((5, 5)) / 25
astro = conv2(astro, psf, 'same')
astro += 0.1 * astro.std() * np.random.standard_normal(astro.shape)

deconvolved, _ = restoration.unsupervised_wiener(astro, psf)

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 5),
                       sharex=True, sharey=True,
                       subplot_kw={'adjustable': 'box-forced'})

plt.gray()

ax[0].imshow(astro, vmin=deconvolved.min(), vmax=deconvolved.max())
ax[0].axis('off')
ax[0].set_title('Data')

ax[1].imshow(deconvolved)
for t in range(num_steps):
    # get the parameters
    c = np.reshape(Theta[t,:Nk*(Ks**2*Kd-1)], (Nk, (Ks**2*Kd-1)))
    normalized_c = c / np.sqrt(np.sum(c**2, axis=0))
    k = B.dot(normalized_c)
    k = np.reshape(k.T, (Nk, Ks, Ks, Kd))
    w = np.reshape(Theta[t,Nk*(Ks**2*Kd-1):-1], (Nk, Nw))
    l = np.reshape(Theta[t,-1], (1,))

    tmp = np.zeros((Kd, u_t[t,0].shape[0]+2*padding, u_t[t,0].shape[1]+2*padding))
    for i in range(Nk):
        conv_k_sum = np.zeros((u_t[t,0].shape[0], u_t[t,0].shape[1]))
        for d in range(Kd):
            ki = k[i,:,:,d]
            u_conv_k = pad_zero(u_t[t,d], padding, padding)
            conv_k_sum += conv2(u_conv_k,ki,'valid')
        phi_u = RBFforward(conv_k_sum, w[i,:])
        for d in range(Kd):
            ki = k[i,:,:,d]
            tmp[d,:,:] += conv2(phi_u,ki[::-1,::-1],'full')
    u_t[t+1] = np.clip(u_t[t] - crop_zero(tmp, padding, padding) - l*bwd_bayer(fwd_bayer(u_t[t]) - f), 0.0,255.0)
    print '.',

#Evaluate
print "\nTest image: %d" % data_config['indices'][example]
#get the result
result = u_t[num_steps]
plt.figure(1)
plt.imshow(swapimdims_3HW_HW3(result).astype('uint8'), interpolation="none")
plt.show()
target = data.target[example]
Пример #41
0
.. [1] François Orieux, Jean-François Giovannelli, and Thomas
       Rodet, "Bayesian estimation of regularization and point
       spread function parameters for Wiener-Hunt deconvolution",
       J. Opt. Soc. Am. A 27, 1593-1607 (2010)
"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import color, data, restoration

astro = color.rgb2gray(data.astronaut())
from scipy.signal import convolve2d as conv2

psf = np.ones((5, 5)) / 25
astro = conv2(astro, psf, "same")
astro += 0.1 * astro.std() * np.random.standard_normal(astro.shape)

deconvolved, _ = restoration.unsupervised_wiener(astro, psf)

fig, ax = plt.subplots(
    nrows=1, ncols=2, figsize=(8, 5), sharex=True, sharey=True, subplot_kw={"adjustable": "box-forced"}
)

plt.gray()

ax[0].imshow(astro, vmin=deconvolved.min(), vmax=deconvolved.max())
ax[0].axis("off")
ax[0].set_title("Data")

ax[1].imshow(deconvolved)