def t_interp_laplacian(t_est, conf, im):
    '''
    interpolate the t_est
    '''
    [nrow, ncol, nch] = im.shape
    numpixel = nrow * ncol

    L = get_laplacian_4neigh(im, longrange=True)
    # L is a sparse matrix not np.ndarray
    # it has zero at places with no estimates, i.e. the discarded patches
    sigma = sparse.dia_matrix((conf.flatten(), [0]),
                              shape=(numpixel, numpixel),
                              dtype='float32')

    # this is faster than spsolve
    chol = cholesky(sigma + alpha * L)
    t_interp_col = chol.solve_A(sigma * t_est.flatten())
    t_interp = t_interp_col.reshape((nrow, ncol))

    t_interp = np.clip(t_interp, 0, 1)

    return t_interp
Ejemplo n.º 2
0
def dehaze_comp_serial(im, A, comp_model, patch_X, patch_Y, stride_X, stride_Y,
                       var_thr, edge_thr, angle_thr, angle_test_pixel_frac,
                       alpha, start_t, end_t, t_tol):
    '''
    Will return the predicted t(x) and the dehazed image using binary search
    '''

    [nrow, ncol, nch] = im.shape
    numpixel = nrow * ncol

    # preallocate
    t_est = np.zeros((nrow, ncol), dtype='float32')
    count = np.zeros((nrow, ncol), dtype='float32')
    conf = np.zeros((nrow, ncol), dtype='float32')

    [x, y] = get_patch_indices(nrow, ncol, patch_X, patch_Y, stride_X,
                               stride_Y)

    # estimate t in each patch
    for idx in xrange(x.size):
        for idy in xrange(y.size):
            patch = im[x[idx]:x[idx] + patch_X,
                       y[idy]:y[idy] + patch_Y, :].copy()

            var_test = patch_variance_test(patch, var_thr)
            edge_test = patch_edge_test(patch, edge_thr)
            (airlight_angle_test, airlight_cos_dist) \
                = patch_airlight_angle_test(patch, var_thr, angle_thr,
                                            A, angle_test_pixel_frac)

            if airlight_angle_test:
                pass

            if not (var_test and not edge_test and airlight_angle_test):
                continue

            (t_out, nconf) = search_t_binary(patch, comp_model, start_t, end_t,
                                             A, t_tol)

            # computed_conf = nconf*airlight_cos_dist
            computed_conf = nconf

            est_patch = t_est[x[idx]:x[idx] + patch_X,
                              y[idy]:y[idy] + patch_Y].copy()

            c_patch = count[x[idx]:x[idx] + patch_X,
                            y[idy]:y[idy] + patch_Y].copy()
            est_patch += t_out
            c_patch += 1

            t_est[x[idx]:x[idx] + patch_X, y[idy]:y[idy] + patch_Y] = est_patch
            count[x[idx]:x[idx] + patch_X, y[idy]:y[idy] + patch_Y] = c_patch

            conf_patch = conf[x[idx]:x[idx] + patch_X,
                              y[idy]:y[idy] + patch_Y].copy()

            conf_patch = np.maximum(conf_patch, computed_conf)
            conf[x[idx]:x[idx] + patch_X, y[idy]:y[idy] + patch_Y] = conf_patch

    nzidx = (count != 0)
    t_est[nzidx] = t_est[nzidx] / count[nzidx]

    # Now need to interpolate t
    L = get_laplacian_4neigh(im, longrange=True)
    # L is a sparse matrix not np.ndarray
    # it has zero at places with no estimates, i.e. the discarded patches
    sigma = sparse.dia_matrix((conf.flatten(), [0]),
                              shape=(numpixel, numpixel),
                              dtype='float32')

    # t_interp_col = splinalg.spsolve(sigma + alpha * L, sigma*t_est.flatten())
    # cholesky is to be faster than spsolve
    chol = cholesky(sigma + alpha * L)
    t_interp_col = chol.solve_A(sigma * t_est.flatten())
    t_interp = t_interp_col.reshape((nrow, ncol))

    t_interp = np.clip(t_interp, 0, 1)
    t_est = np.clip(t_est, 0, 1)

    # recover
    A = A.reshape((1, 1, 3))
    out_im = A + (im - A) / np.tile(t_interp[:, :, np.newaxis], (1, 1, 3))

    out_im = np.clip(out_im, 0, 1)

    return (out_im, t_est, t_interp)