Пример #1
0
def vectorization(N_theta=N_theta,
                  N_azimuth=N_azimuth,
                  N_eccentricity=N_eccentricity,
                  N_phase=N_phase,
                  N_X=N_X,
                  N_Y=N_Y,
                  rho=rho,
                  ecc_max=.8,
                  B_sf=.4,
                  B_theta=np.pi / N_theta / 2,
                  figure_type='',
                  save=False):
    retina = np.zeros((N_theta, N_azimuth, N_eccentricity, N_phase, N_X * N_Y))
    parameterfile = 'https://raw.githubusercontent.com/bicv/LogGabor/master/default_param.py'
    lg = LogGabor(parameterfile)
    lg.set_size((N_X, N_Y))
    # params = {'sf_0': .1, 'B_sf': lg.pe.B_sf,
    #           'theta': np.pi * 5 / 7., 'B_theta': lg.pe.B_theta}
    # phase = np.pi/4
    # edge = lg.normalize(lg.invert(lg.loggabor(
    #     N_X/3, 3*N_Y/4, **params)*np.exp(-1j*phase)))

    for i_theta in range(N_theta):
        for i_azimuth in range(N_azimuth):
            for i_eccentricity in range(N_eccentricity):
                ecc = ecc_max * (1 / rho)**(N_eccentricity - i_eccentricity)
                r = np.sqrt(N_X**2 + N_Y**2) / 2 * ecc  # radius
                sf_0 = 0.5 * 0.03 / ecc
                x = N_X/2 + r * \
                    np.cos((i_azimuth+(i_eccentricity % 2)*.5)*np.pi*2 / N_azimuth)
                y = N_Y/2 + r * \
                    np.sin((i_azimuth+(i_eccentricity % 2)*.5)*np.pi*2 / N_azimuth)
                for i_phase in range(N_phase):
                    params = {
                        'sf_0': sf_0,
                        'B_sf': B_sf,
                        'theta': i_theta * np.pi / N_theta,
                        'B_theta': B_theta
                    }
                    phase = i_phase * np.pi / 2
                    # print(r, x, y, phase, params)

                    retina[i_theta, i_azimuth, i_eccentricity,
                           i_phase, :] = lg.normalize(
                               lg.invert(
                                   lg.loggabor(x, y, **params) *
                                   np.exp(-1j * phase))).ravel()
    if figure_type == 'retina':
        FIG_WIDTH = 10
        fig, ax = plt.subplots(figsize=(FIG_WIDTH, FIG_WIDTH))
        for i_theta in range(N_theta):
            for i_azimuth in range(N_azimuth):
                for i_eccentricity in range(N_eccentricity):
                    env = np.sqrt(retina[i_theta, i_azimuth, i_eccentricity,
                                         0, :]**2 +
                                  retina[i_theta, i_azimuth, i_eccentricity,
                                         1, :]**2).reshape((N_X, N_Y))
                    ax.contourf(env,
                                levels=[env.max() / 1.2,
                                        env.max() / 1.00001],
                                lw=1,
                                colors=[plt.cm.viridis(i_theta / (N_theta))],
                                alpha=.1)
        fig.suptitle('Tiling of visual space using the retinal filters')
        ax.set_xlabel(r'$Y$')
        ax.set_ylabel(r'$X$')
        ax.axis('equal')
        if save: plt.savefig('retina_filter.pdf')
        plt.tight_layout()
        return fig, ax
    elif figure_type == 'colliculus':
        FIG_WIDTH = 10
        fig, ax = plt.subplots(figsize=(FIG_WIDTH, FIG_WIDTH))
        for i_azimuth in range(N_azimuth):
            for i_eccentricity in range(N_eccentricity):
                env = np.sqrt(colliculus[i_azimuth,
                                         i_eccentricity, :]**2.5).reshape(
                                             (N_X, N_Y))
                #ax.contour(colliculus[i_azimuth, i_eccentricity, :].reshape((N_X, N_Y)), levels=[env.max()/2], lw=1, colors=[plt.cm.viridis(i_theta/(N_theta))])
                ax.contourf(
                    env,
                    levels=[env.max() / 1.2,
                            env.max() / 1.00001],
                    lw=1,
                    colors=[plt.cm.viridis(i_eccentricity / (N_eccentricity))],
                    alpha=.1)
        fig.suptitle('Tiling of visual space using energy')
        ax.set_xlabel(r'$Y$')
        ax.set_ylabel(r'$X$')
        ax.axis('equal')
        plt.tight_layout()
        if save: plt.savefig('colliculus_filter.pdf')
        return fig, ax
    else:
        return retina
Пример #2
0
def generate_gabors_coordinates(theta, params, N_X, N_Y, centers_coordinates,
                                B_theta=15, sf_0=.05, B_sf=.5,
                                distrib_size=8, grid_res=3,
                                on_thresh=.1, off_thresh=-.1,
                                verbose=True):
    '''
    Given some gabor parameters, a set of coordinates for centering gabors, returns a set of 
    coordinates for filters belonging into the gabors


    Params :
        theta : gabor theta angle
        params : the default parameters dictionnary for the gabor generation
        N_X, N_Y : Gabor size, usually the same as the video

        centers_coordinates : a 2D array giving the centers of each gabor

        B_theta, sf_0, B_sf : Parameters for the LogGabor shape
                            B_theta is the opening of the gabor, 
                            sf_0 is the spatial frequency 
                            b_sf is the bandwidth frequency

        distrib_size : the size of each group of filters, in image coordinates
        grid_res : resolution of the group of filters, passed in a np.mgrid

        on_thresh, off_thresh : threshold at which a filter is selected to 
                                be on/off, by scanning the Gabor phi-space

        verbose : display the filter size as a sanity check  
    '''

    xs = centers_coordinates[0]
    ys = centers_coordinates[1]
    nbr_gabors = len(xs)

    N_X = int(N_X)
    N_Y = int(N_Y)
    N_phase = 2

    lg = LogGabor(params)
    lg.set_size((N_X, N_Y))

    B_theta = B_theta / 180 * np.pi
    params = {'sf_0': sf_0, 'B_sf': B_sf, 'B_theta': B_theta}
    params.update(theta=theta)

    phi = np.zeros((1, N_phase, N_X, N_Y))

    filters_per_gab = []
    for gab in range(nbr_gabors):
        x = xs[gab]
        y = ys[gab]

        for i_phase in range(N_phase):
            phase = i_phase * np.pi/2
            kernel = lg.invert(lg.loggabor(
                x, y, **params)*np.exp(-1j*phase))
            phi[0, i_phase, :] = lg.normalize(kernel)

        fx_min = x - distrib_size
        fx_max = x + distrib_size
        fy_min = y - distrib_size
        fy_max = y + distrib_size
        filters_coordinates = np.mgrid[fx_min:fx_max:grid_res,
                                       fy_min:fy_max:grid_res].reshape(2, -1).T
        if verbose and gab == 0:
            print('Thread started !\nFilter grid shape',
                  filters_coordinates.shape, '\n')

        filters_in_gabor = gabor_connectivity(filters=filters_coordinates,
                                              phi=phi, theta=0, threshold=on_thresh)
        off_filters_in_gabor = gabor_connectivity(filters=filters_coordinates,
                                                  phi=phi, theta=0, threshold=off_thresh, on=False)

        filters_per_gab.append((filters_in_gabor, off_filters_in_gabor))

    return filters_per_gab