Пример #1
0
 def theta_E(self, im, X_, Y_, w):
     try:
         assert (self.slip.N_X == im.shape[1])
     except:
         from NeuroTools.parameters import ParameterSet
         from SLIP import Image
         from LogGabor import LogGabor
         self.slip = Image(
             ParameterSet({
                 'N_X': im.shape[1],
                 'N_Y': im.shape[0]
             }))
         self.lg = LogGabor(self.slip)
     im_ = im.sum(axis=-1)
     im_ = im_ * np.exp(-.5 * ((.5 + .5 * self.slip.x - Y_)**2 +
                               (.5 + .5 * self.slip.y - X_)**2) / w**2)
     E = np.zeros((self.N_theta, ))
     for i_theta, theta in enumerate(self.thetas):
         params = {
             'sf_0': self.sf_0,
             'B_sf': self.B_sf,
             'theta': theta,
             'B_theta': np.pi / self.N_theta
         }
         FT_lg = self.lg.loggabor(0, 0, **params)
         E[i_theta] = np.sum(
             np.absolute(
                 self.slip.FTfilter(np.rot90(im_, -1), FT_lg,
                                    full=True))**2)
     return E
Пример #2
0
def apply_gabor(image) -> np.array:
    lg = LogGabor("parameters.py")
    lg.set_size(image)
    image[:, :, 0] = image[:, :, 0]*lg.mask
    image[:, :, 1] = image[:, :, 1]*lg.mask
    image[:, :, 2] = image[:, :, 2]*lg.mask
    return image
Пример #3
0
def get_shape_of_filtered_image(image_file_path):
    image = imread(image_file_path)
    lg = LogGabor('default_param.py')
    lg.set_size(image)
    i_level = 0
    theta = 0
    params = {
        'sf_0': 1. / (2**i_level),
        'B_sf': lg.pe.B_sf,
        'theta': theta,
        'B_theta': lg.pe.B_theta
    }
    FT_lg = lg.loggabor(0, 0, **params)
    filtered_img_mat = lg.FTfilter(image, FT_lg, full=True)
    return filtered_img_mat.shape
Пример #4
0
def get_gabor_features(image_file_path, num_levels, num_orientations):
    image = imread(image_file_path)
    opts = {
        'vmin': 0.,
        'vmax': 1.,
        'interpolation': 'nearest',
        'origin': 'upper'
    }
    # image = skimage.io.imread
    lg = LogGabor('default_param.py')
    lg.set_size(image)
    # num_features = num_levels*num_orientations*2
    # feature_vec = np.zeros((num_features,1))
    # phi = (np.sqrt(5) +1.)/2. # golden number
    # fig = plt.figure(figsize=(fig_width, fig_width/phi))
    # xmin, ymin, size = 0, 0, 1.
    i = 0
    for i_level in range(num_levels):
        # for theta in np.linspace(0, np.pi, num_orientations, endpoint=False):
        for theta in np.linspace(0, np.pi, num_orientations, endpoint=False):
            params = {
                'sf_0': 1. / (2**i_level),
                'B_sf': lg.pe.B_sf,
                'theta': theta,
                'B_theta': lg.pe.B_theta
            }
            # loggabor takes as args: u, v, sf_0, B_sf, theta, B_theta)
            FT_lg = lg.loggabor(0, 0, **params)
            filtered_img_mat = lg.FTfilter(image, FT_lg, full=True)
            # print "SHAPE OF FILTERED IMAGE IS (%s, %s)" % filtered_img_mat.shape
            im_abs_feature = np.absolute(filtered_img_mat).flatten()
            # im_abs_feature = np.sum(np.absolute(filtered_img_mat))
            # im_sqr_feature = np.sum(np.square(np.real(filtered_img_mat)))
            # im_sqr_feature = np.sum(np.square(filtered_img_mat))
            if i == 0:
                feature_vec = im_abs_feature
            else:
                feature_vec = np.hstack((feature_vec, im_abs_feature))
            i += 1
    print
    return feature_vec
Пример #5
0
    0,
    '/home/qiang/QiangLi/Python_Utils_Functional/FirstVersion-BioMulti-L-NL-Model-ongoing/SLIP/SLIP'
)
from SLIP import Image

#LogGabor can be install in the first time but when you second time install it,
#It will cause error. Here for how to fix it.
#!pip3 install --upgrade pip setuptools wheel
#!sudo apt-get install libpq-dev
sys.path.insert(
    0,
    '/home/qiang/QiangLi/Python_Utils_Functional/FirstVersion-BioMulti-L-NL-Model-ongoing/LogGabor/LogGabor'
)
from LogGabor import LogGabor
parameterfile = '/home/qiang/QiangLi/Python_Utils_Functional/FirstVersion-BioMulti-L-NL-Model-ongoing/LogGabor/default_param.py'
lg = LogGabor(parameterfile)
print('It works on 23 April2020')

#DWT wavelet filters
sys.path.insert(
    0,
    '/home/qiang/QiangLi/Python_Utils_Functional/FirstVersion-BioMulti-L-NL-Model-ongoing/'
)

from nt_toolbox.general import *
from nt_toolbox.signal import *
from nt_toolbox.compute_wavelet_filter import *
print('It works on 27 April2020')

warnings.filterwarnings('ignore')
#%matplotlib inline
Пример #6
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
Пример #7
0
def generate_spectrum_from_RDC(filename,
                               numFrames=500,
                               numADCSamples=128,
                               numTxAntennas=3,
                               numRxAntennas=4,
                               numLoopsPerFrame=128,
                               numAngleBins=64,
                               chirpPeriod=0.06,
                               logGabor=False,
                               accumulate=True,
                               save_full=False):
    numChirpsPerFrame = numTxAntennas * numLoopsPerFrame

    # =============================================================================
    #     numADCSamples = number of range bins
    #     numLoopsPerFrame = number of doppler bins
    # =============================================================================

    range_resolution, bandwidth = dsp.range_resolution(numADCSamples)
    doppler_resolution = dsp.doppler_resolution(bandwidth)

    if filename[-4:] != '.bin':
        filename += '.bin'

    adc_data = np.fromfile(filename, dtype=np.int16)
    adc_data = adc_data.reshape(numFrames, -1)
    adc_data = np.apply_along_axis(DCA1000.organize,
                                   1,
                                   adc_data,
                                   num_chirps=numChirpsPerFrame,
                                   num_rx=numRxAntennas,
                                   num_samples=numADCSamples)
    print("Data Loaded!")

    dataCube = adc_data
    micro_doppler_data = np.zeros((numFrames, numLoopsPerFrame, numADCSamples),
                                  dtype=np.float64)
    theta_data = np.zeros((numFrames, numLoopsPerFrame,
                           numTxAntennas * numRxAntennas, numADCSamples),
                          dtype=np.complex)

    for i, frame in enumerate(dataCube):
        # (2) Range Processing
        from mmwave.dsp.utils import Window

        radar_cube = dsp.range_processing(frame,
                                          window_type_1d=Window.BLACKMAN)
        assert radar_cube.shape == (
            numChirpsPerFrame, numRxAntennas,
            numADCSamples), "[ERROR] Radar cube is not the correct shape!"

        # (3) Doppler Processing
        det_matrix, theta_data[i] = dsp.doppler_processing(
            radar_cube,
            num_tx_antennas=3,
            clutter_removal_enabled=True,
            window_type_2d=Window.HAMMING)

        # --- Shifts & Store
        det_matrix_vis = np.fft.fftshift(det_matrix, axes=1)
        micro_doppler_data[i, :, :] = det_matrix_vis
        # Data should now be ready. Needs to be in micro_doppler_data, a 3D-numpy array with shape [numDoppler, numRanges, numFrames]

        # LOG GABOR
        if logGabor:
            if accumulate:
                image = micro_doppler_data.sum(axis=1).T
            else:
                image = micro_doppler_data.T

            from LogGabor import LogGabor
            import holoviews as hv

            lg = LogGabor("default_param.py")
            lg.set_size(image)
            lg.pe.datapath = 'database/'

            image = lg.normalize(image, center=True)

            # display input image
            # hv.Image(image)

            # display log gabor'd image
            image = lg.whitening(image) * lg.mask
            hv.Image(image)

            uDoppler = image
        elif accumulate:
            uDoppler = micro_doppler_data.sum(axis=1).T
        else:
            uDoppler = micro_doppler_data.T

    if save_full:
        return range_resolution, doppler_resolution, uDoppler, theta_data
    else:
        return range_resolution, doppler_resolution, uDoppler
Пример #8
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
    # LOG GABOR
    
    if logGabor:
        if accumulate:
            image = micro_doppler_data.sum(axis=1).T
        else:
            image = micro_doppler_data[:,120,:].T

        from LogGabor import LogGabor
        import holoviews as hv
        import os
        fig_width = 12
        figsize=(fig_width, .618*fig_width)

        lg = LogGabor("default_param.py")
        lg.set_size(image)
        lg.pe.datapath = 'database/'

        image = lg.normalize(image, center=True)

        # display input image
        # hv.Image(image)

        # display log gabor'd image
        image = lg.whitening(image)*lg.mask
        hv.Image(image)

        uDoppler = image
    elif accumulate:
        uDoppler = micro_doppler_data.sum(axis=1).T
Пример #10
0
def get_gabor_features_texture_classification(image_file_path, num_levels,
                                              num_orientations):
    # image = imread(image_file_path)
    # image = skimage.io.imread(image_file_path)
    print 'length of image'
    print len(image_file_path.shape)
    if (len(image_file_path.shape) == 3):
        image = image_file_path[:, :, 0]
    else:
        image = image_file_path[:, :]
    opts = {
        'vmin': 0.,
        'vmax': 1.,
        'interpolation': 'nearest',
        'origin': 'upper'
    }
    print image.shape

    # image = image_file_path
    # image = image[30:70,30:70]
    sum_sqr_num_feat = num_levels * num_orientations

    sum_sqr_feat_vec = np.zeros((1, sum_sqr_num_feat))
    print 'shape of sum sqr vec is:'
    print sum_sqr_feat_vec.shape
    lg = LogGabor('default_param.py')
    lg.set_size(image)
    # num_features = num_levels*num_orientations*2
    # feature_vec = np.zeros((num_features,1))
    # phi = (np.sqrt(5) +1.)/2. # golden number
    # fig = plt.figure(figsize=(fig_width, fig_width/phi))
    # xmin, ymin, size = 0, 0, 1.
    i = 0
    first_response = None
    for i_level in range(num_levels):
        # for theta in np.linspace(0, np.pi, num_orientations, endpoint=False):
        for theta in np.linspace(0, np.pi, num_orientations, endpoint=False):
            params = {
                'sf_0': 1. / (2**i_level),
                'B_sf': lg.pe.B_sf,
                'theta': theta,
                'B_theta': lg.pe.B_theta
            }
            # loggabor takes as args: u, v, sf_0, B_sf, theta, B_theta)
            FT_lg = lg.loggabor(0, 0, **params)
            filtered_img_mat = lg.FTfilter(image, FT_lg, full=True)
            # print "SHAPE OF FILTERED IMAGE IS (%s, %s)" % filtered_img_mat.shape
            im_abs_feature = np.absolute(filtered_img_mat).flatten()
            # im_abs_feature = np.sum(np.absolute(filtered_img_mat))
            # im_sqr_feature = np.sum(np.square(np.real(filtered_img_mat)))
            # im_sqr_feature = np.sum(np.square(filtered_img_mat))
            if i == 0:
                first_response = im_abs_feature.reshape(
                    1, im_abs_feature.shape[0])
            # print type(im_abs_feature)
            # print im_abs_feature.shape
            # print im_abs_feature
            # print LA.norm(im_abs_feature.reshape(1, im_abs_feature.shape[0]))
            sum_sqr_feat_vec[0, i] = LA.norm(
                im_abs_feature.reshape(1, im_abs_feature.shape[0]))

            # print 'L2 norm squared is:'
            # print sum_sqr_feat_vec[0,i]

            # if i == 0:
            #     feature_vec = im_abs_feature
            # else:
            #     feature_vec = np.hstack((feature_vec, im_abs_feature))
            i += 1
    print
    return sum_sqr_feat_vec