Пример #1
0
def curvelet_transform(x, num_bands, num_angles = 8, all_curvelets = True, as_complex = False):
    ndims = len(x.shape)

    # This file requires Curvelab and the PyCurveLab packages be installed on your system.
    try:
        import pyct
    except ImportError:
        raise NotImplementedError("Use of curvelets requires installation of CurveLab and the PyCurveLab package.\nSee: http://curvelet.org/  and  https://www.slim.eos.ubc.ca/SoftwareLicensed/")

    if ndims == 2:
        ct = pyct.fdct2( n = x.shape,
                         nbs = num_bands,   # Number of bands
                         nba = num_angles,  # Number of discrete angles
                         ac = all_curvelets,# Return curvelets at the finest detail level
                         vec = False,       # Return results as nested python vectors
                         cpx = as_complex)  # Disable complex-valued curvelets
    elif ndims == 3:
        ct = pyct.fdct3( n = x.shape,
                         nbs = num_bands,   # Number of bands
                         nba = num_angles,  # Number of discrete angles
                         ac = all_curvelets,# Return curvelets at the finest detail level
                         vec = False,       # Return results as nested python vectors
                         cpx = as_complex)  # Disable complex-valued curvelets
    else:
        raise NotImplementedError("%dD Curvelets are not supported." % (ndims))
    result = ct.fwd(x)
    del ct
    return result
Пример #2
0
def execute(image, data):
    transformation = fdct2(
            image.shape,
            data["config"]["curvelets"]["scales"],
            data["config"]["curvelets"]["angles"],
            False,
            norm=False,
            vec=False,
            )

    coefficients = transformation.fwd(image)
    coefficient_map = {}

    for scale, scale_data in enumerate(coefficients):
        if len(scale_data) > 1:
            angles = numpy.linspace(0, len(scale_data) / 2,\
                    data["config"]["curvelets"]["angles"], False)\
                    .astype(numpy.int)
            #for angle, angle_data in enumerate(\
                    #scale_data[:len(scale_data) / 2]):
            for angle, angle_index in enumerate(angles):
                angle_data = numpy.fabs(scale_data[angle_index])
                #angle_data = numpy.fabs(angle_data)[angles]
                coefficient_map["{},{}".format(scale, angle)] = angle_data
    return coefficient_map
Пример #3
0
def show_fdct(filename, channel=0, scales=4, angles=16):
    image = pyplot.imread(filename)[:, :, channel]
    transformation = fdct2(image.shape, scales, angles, True, norm=True)

    cl = transformation.fwd(image)

    #fig, axs = pyplot.subplots(angles, scales + 1)
    pyplot.spectral()
    fig = pyplot.figure(1)

    # display original image
    #img_orig = axs[0, 0].imshow(image)
    img_orig = pyplot.imshow(image)
    pyplot.colorbar(orientation="horizontal")
    #pyplot.colorbar(img_orig, ax=axs[0, 0], orientation="horizontal")

    #imgs = []
    angles = [1, ] + [angles] * (scales - 1)
    for scale in range(scales):
        fig = pyplot.figure(scale + 100)
        grid = ImageGrid(fig, 111, (1, angles[scale]))
        for angle in range(angles[scale]):
            print("displaying",scale,angle)
            cl_img = cl(scale, angle)
            print(cl_img.shape)
            grid[angle].imshow(cl_img)
            #img = axs[0, scale + 1].imshow(cl_img)
        #pyplot.colorbar(img, ax=axs[0, scale + 1], orientation="horizontal")
        #imgs.append(img)

    pyplot.show()
Пример #4
0
def test_FDCT2D_2dsignal(par):
    """
    Tests for FDCT2D operator for 2d signal.
    """
    x = np.random.normal(0., 1., (par['nx'], par['ny'])) + \
        np.random.normal(0., 1., (par['nx'], par['ny'])) * par['imag']

    FDCTop = FDCT2D(dims=(par['nx'], par['ny']), dtype=par['dtype'])

    assert dottest(FDCTop,
                   *FDCTop.shape,
                   tol=1e-12,
                   complexflag=0 if par['imag'] == 0 else 3)

    y = FDCTop * x.ravel()
    xinv = FDCTop.H * y
    np.testing.assert_array_almost_equal(xinv.reshape(*x.shape), x, decimal=14)

    if PYCT:
        FDCTct = ct.fdct2(x.shape,
                          FDCTop.nbscales,
                          FDCTop.nbangles_coarse,
                          FDCTop.allcurvelets,
                          cpx=False if par['imag'] == 0 else True)
        y_ct = np.array(FDCTct.fwd(x)).ravel()

        np.testing.assert_array_almost_equal(y, y_ct, decimal=64)
        assert y.dtype == y_ct.dtype
Пример #5
0
def normtest(dim=2, clen=10):
    for i in range(clen):
        print("-----------------------------------")
        if dim == 2:
            sz = np.arange(256, 513)
        else:
            sz = np.arange(64, 128)

        np.random.shuffle(sz)
        print(sz[:dim])

        iscplx = [True, False]
        np.random.shuffle(iscplx)

        isac = [True, False]
        np.random.shuffle(isac)
        if isac[0]:
            print("All curvelets")
        else:
            print("Wavelets at finest scale")

        if dim == 2:
            A = ct.fdct2(sz[:2], 6, 32, isac[0], norm=True, cpx=iscplx[0])
            pos = np.arange(A.range())
            np.random.shuffle(pos)
            if iscplx[0]:
                print("Complex input")
                x = np.zeros(A.range(), dtype='complex')
                v = np.random.rand()
                x[pos[0]] = v + np.sqrt(1 - v**2) * 1j
            else:
                print("Real input")
                x = np.zeros(A.range())
                x[pos[0]] = 1.
        elif dim == 3:
            A = ct.fdct3(sz[:3], 4, 8, isac[0], norm=True, cpx=iscplx[0])
            pos = np.arange(A.range())
            np.random.shuffle(pos)
            if iscplx[0]:
                print("Complex input")
                x = np.zeros(A.range(), dtype='complex')
                v = np.random.rand()
                x[pos[0]] = v + np.sqrt(1 - v**2) * 1j
            else:
                print("Real input")
                x = np.zeros(A.range())
                x[pos[0]] = 1.

        f = A.inv(x)

        if np.allclose(norm(f.flatten(), ord=2), norm(x, ord=2)):
            print('Norm check ok!')
        else:
            print('Problem w norm test')

        print("||f|| = ", norm(f.flatten(), ord=2), f.dtype)
        print("||x|| = ", norm(x, ord=2), x.dtype)
Пример #6
0
def main():
    # Create Curvelet object for 360x180 px
    A = ct.fdct2((360, 180),
                 nbs=n_scales,
                 nba=n_angles,
                 ac=True,
                 norm=False,
                 vec=True,
                 cpx=False)

    print('Processing subject ' + subject)

    # Set filename(s)
    raw_folder = join(results_folder, subject, 'raw')

    # Initialize a feature dictionary per subject
    f_dict = {}
    f_dict['subject'] = subject
    f_dict['target'] = label
    f_dict['n_scales'] = n_scales
    f_dict['n_angles'] = n_angles

    for r in tqdm(sphere_radius, desc='Sphere scale'):
        # for r in sphere_radius:
        # Get type of image and sphere params
        raw_file = join(
            raw_folder,
            '%s_%03d_to_%03d_solid_angle_to_sphere.raw' % (img_type, r,
                                                           (r + delta)))

        # Load and do the magic!
        try:
            img = np.fromfile(raw_file, dtype=np.float).reshape([360, 180]).T
        except:
            print('No file found: ' + raw_file)

        # Get a Curvelet decomposition
        f = A.fwd(img)
        print(f.shape)

        # Convert data to dict
        buff = clarray_to_gen_gaussian_dict(A, f, n_scales, n_angles, r)
        f_dict.update(buff)
        del buff, f, img

    # Save subject results
    subject_feats_file = join(output_subfolder, '%s.npz' % subject)
    np.savez_compressed(subject_feats_file, **f_dict)

    # Give permissions
    os.system('chmod 777 ' + subject_feats_file)
    os.system('chmod -R 777 ' + output_subfolder)
Пример #7
0
def test(dim=2,clen=10):

	for i in xrange(clen):
		print "-----------------------------------"
		if dim==2:
			sz = np.arange(256,513)
		elif dim==3:
			sz = np.arange(64,128)

		np.random.shuffle(sz)
		iscplx = [True,False]
		np.random.shuffle(iscplx)
		if iscplx[0]:
			print "Complex input"
			f = np.array(sc.randn(*sz[:dim])+sc.randn(*sz[:dim])*1j)
		else:
			print "Real input"
			f = np.array(sc.randn(*sz[:dim]))
			
		isac = [True,False]
		np.random.shuffle(isac)
		if isac[0]:
			print "All curvelets"
		else:
			print "Wavelets at finest scale"


		print f.shape
		
		if dim==2:
			A = ct.fdct2(f.shape,6,32,isac[0],cpx=iscplx[0])
		elif dim==3:
			A = ct.fdct3(f.shape,4,8,isac[0],cpx=iscplx[0])

		x = A.fwd(f)
		
		if np.allclose(norm(f.flatten(),ord=2),norm(x,ord=2) ):
			print 'Energy check ok!'
		else:
			print 'Problem w energy test'

		fr = A.inv(x)
		if np.allclose(f.flatten(),fr.flatten() ):
			print 'Inverse check ok!'
		else:
			print 'Problem w inverse test'

		print "||f|| = ",norm(f.flatten(),ord=2),f.dtype
		print "||x|| = ",norm(x,ord=2),x.dtype
		print "||fr|| = ",norm(fr.flatten(),ord=2),fr.dtype
Пример #8
0
def stack_pyct_inv(trans,curv_obj=None,nb_sc=4,nb_dir=8,shap = None):
    shap_trans = trans.shape
    if curv_obj is None:
        curv_obj = pyct.fdct2([shap[0],shap[1]],nb_sc,nb_dir,True,norm=True)

    im1 = curv_obj.inv(trans[0,:])
    if shap is None:
        shap = im1.shape
    im_out = zeros((shap[0],shap[1],shap_trans[0]))
    im_out[:,:,0] = im1
    for i in range(1,shap_trans[0]):
        im_out[:,:,i] = curv_obj.inv(trans[i,:])

    return im_out
Пример #9
0
def execute(image, data):
    transformation = fdct2(
            image.shape,
            data["config"]["curvelets"]["scales"],
            data["config"]["curvelets"]["angles"],
            False,
            norm=False,
            vec=False,
            )

    coefficients = transformation.fwd(image)
    coefficient_map = {}

    scale = data["config"]["curvelets"]["use_scale"]
    scale_data = coefficients[scale]
    for angle, angle_data in enumerate(scale_data[:len(scale_data) / 2]):
        angle_data = numpy.fabs(angle_data)
        coefficient_map["{},{}".format(scale, angle)] = angle_data

    return coefficient_map
Пример #10
0
def stack_pyct_fwd(im,curv_obj=None,nb_sc=4,nb_dir=8,corr_en=False):
    shap_im = im.shape
    if curv_obj is None:
        curv_obj = pyct.fdct2([shap_im[0],shap_im[1]],nb_sc,nb_dir,True,norm=True)
    corr_coeff=None # MT = corr_coeff*M^-1
    if corr_en:
        a = numpy.random.randn(shap_im[0],shap_im[1])
        b = curv_obj.fwd(a)
        corr_coeff = (b*numpy.conj(b)).sum()/(a**2).sum()

    curv1 = curv_obj.fwd(im[:,:,0])
    nb_pts = len(curv1)
    curv_out = zeros((shap_im[2],nb_pts))
    curv_out[0,:] = copy(curv1)
    for i in range(1,shap_im[2]):
        curv_out[i,:] = curv_obj.fwd(im[:,:,i])
    if corr_en:
        return curv_out,curv_obj,corr_coeff
    else:
        return curv_out,curv_obj
Пример #11
0
def execute(image, data):
    transformation = fdct2(
        image.shape,
        data["config"]["curvelets"]["scales"],
        data["config"]["curvelets"]["angles"],
        False,
        norm=False,
        vec=False,
    )

    coefficients = transformation.fwd(image)

    c = CurveletCoefficients()
    for scale, scale_data in enumerate(coefficients):
        if len(scale_data) > 1:
            s = CurveletCoefficientScale()
            c.scales.append(s)
            for angle, angle_data in enumerate(scale_data[: len(scale_data) / 2]):
                a = CurveletCoefficientAngle(numpy.fabs(angle_data))
                s.angles.append(a)
    return c
Пример #12
0
def create(filename, channel=0, scales=3, angles=12, output_dir=None, output_pattern="{prefix}_{scale}_{angle}.png"):
    if not output_dir:
        output_dir = datetime.datetime.now().strftime(filename + "_%Y%m%d%H%M%S")

    if os.path.exists(output_dir):
        print("Error: Output directory '{}' already exists.".format(output_dir))
        return

    print("Reading channel {} of file '{}'...".format(channel, filename))
    image = pyplot.imread(filename)[:, :, channel]

    print("Applying curvelet transform on {} scales and {} angles...".format(scales, angles))
    transformation = fdct2(image.shape, scales, angles, True, norm=True)
    cl = transformation.fwd(image)

    file_path, file_tail = os.path.split(filename)
    file_prefix, file_ext = os.path.splitext(file_tail)

    angles = [1, ] + [angles] * (scales - 1)

    print("Writing output to directory '{}'...".format(output_dir))
    os.mkdir(output_dir)
    for scale in range(scales):
        for angle in range(angles[scale]):
            fig = pyplot.figure()
            axes = fig.gca()
            cl_image = cl(scale, angle)

            mappable = axes.imshow(cl_image)
            fig.colorbar(mappable, ax=axes)
            fig.savefig(os.path.join(output_dir, output_pattern.format(
                prefix = file_prefix,
                scale  = scale,
                angle  = angle,
                )),
                format = "png",
                )
Пример #13
0
def inverse_curvelet_transform(coefs, x_shape, num_bands, num_angles, all_curvelets, as_complex):
    # This file requires Curvelab and the PyCurveLab packages be installed on your system.
    try:
        import pyct
    except ImportError:
        raise NotImplementedError("Use of curvelets requires installation of CurveLab and the PyCurveLab package.\nSee: http://curvelet.org/  and  https://www.slim.eos.ubc.ca/SoftwareLicensed/")

    if len(x_shape) == 2:
        ct = pyct.fdct2( n = x_shape,
                          nbs = num_bands,     # Number of bands
                          nba = num_angles,
                          ac = all_curvelets,
                          vec = False,
                          cpx = as_complex)
    else:
        ct = pyct.fdct3( n = x_shape,
                          nbs = num_bands,     # Number of bands
                          nba = num_angles,
                          ac = all_curvelets,
                          vec = False,
                          cpx = as_complex)
    result = ct.inv(coefs)
    del ct
    return result
Пример #14
0
 def load_curvelet_transform(self):
     if hasattr(self, 'image'):
         self.fdct_worker = pyct.fdct2(n=self.image.size,nbs=int(self.nbs.currentText()),\
          nba=int(self.nba.currentText()),ac=self.ac.isChecked(),norm=False,vec=True)
         self.digital_tile.initialize_tiles(int(self.nbs.currentText()),int(self.nba.currentText()),\
          self.ac.isChecked(),self.cursor_selections.currentText(),self.click_functions.currentText())
         if not hasattr(self, 'curvelet_structure'):
             self.digital_tile.WEDGE_REQUESTED.connect(
                 self.click_show_wedge)
             self.digital_tile.WEDGE_ENTER.connect(self.update_wedge_index)
             self.digital_tile.WEDGE_CHOSEN.connect(
                 self.update_chosen_wedges)
         self.curvelet_structure = process.CurveletStructure(
             self.fdct_worker.struct(self.fdct_worker.fwd(self.image)))
         self.curvelet_structure.control_panel.UPDATE_LOG.connect(
             self.update_log)
         self.current_status['curvelet_transform'] = (
             self.nbs.currentText(), self.nba.currentText(),
             self.ac.isChecked())
         self.apply_threshold_button.setEnabled(True)
         self.load_curvelet_button.setEnabled(False)
         self.update_log('[SUCCESS] Curvelet structure created!')
     else:
         self.update_log('[ERROR] Please load an image first!')
Пример #15
0
    def execute(self, item):
        inputs = Bunch.fromDict(item)

        transformation = fdct2(
                inputs.image.shape,
                inputs.parameters.scales,
                inputs.parameters.angles,
                False,
                norm=False,
                vec=False,
                )

        coefficients = transformation.fwd(inputs.image)
        coefficient_map = {}

        for scale, scale_data in enumerate(coefficients):
            if len(scale_data) > 1:
                for angle, angle_data in enumerate(\
                        scale_data[:len(scale_data) / 2]):
                    angle_data = numpy.fabs(angle_data)
                    coefficient_map["{},{}".format(scale, angle)] = angle_data

        item["coefficients"] = coefficient_map
        return item
Пример #16
0
            os.mkdir(dirname(out_folder))
            os.mkdir(out_folder)
        elif not exists(out_folder):
            os.mkdir(out_folder)

        # Load img and split it in half
        if data_file.endswith('.raw'):
            img = np.fromfile(data_file).reshape([180, 90])
        elif data_file.endswith('.npz'):
            img = np.load(data_file)['img']

        # Define a curvelet object
        A = ct.fdct2(
            img.shape,
            nbs=nbs,
            nba=nba,
            ac=True,
            norm=False,
            vec=True,
            cpx=False)

        # Decompose
        feats = pd.Series()
        feats.name = subject_id

        # Start decomposition
        f = A.fwd(img)
        print('[  INFO  ] Curvelet decomposition shape: %s' % str(f.shape))

        # Use generalized Gaussian to fit features
        data_dict = get_sub_bands(A, f)
        for key, val in data_dict.items():
Пример #17
0
        print('[  ERROR  ] Index (%d, %d) out of range' % (scale, angle))


if __name__ == '__main__':
    print('Loading image...')
    filename = os.path.join(root, 'pyCurvelab', 'test', 'gradient_020_to_025_solid_angle_to_sphere.png')
    img = imread(filename, as_grey=True)

    # Parameters
    th = int(sys.argv[1])
    number_of_scales = 7
    number_of_angles = 16

    for s in range(0, number_of_scales):
        # Setup curvelet params
        A = ct.fdct2(img.shape, nbs=number_of_scales, nba=number_of_angles, ac=True, norm=False, vec=True, cpx=False)

        # Apply curvelet to the image
        f = A.fwd(img)

        print('Data coefficients type: ', type(f))
        print('Coefficients array:\n\t', f)
        print('Shape: ', f.shape)
        print('Image shape vect: ', np.prod(img.shape))

        # Print Information
        for scale in range(0, number_of_scales):
            if scale == 0:
                angles = [0]
            elif scale == 1:
                angles = range(0, number_of_angles)
Пример #18
0
                                                 scale=3)
        e_orient[i, :] = s4(coef_scale_angle)
        ### Fine scale s5
        coef_scale = coef_part_scale(coef_full=coef_full,
                                     cuvelet_structure=curvelet2,
                                     scale=4)
        v1 = np.abs(coef_scale)
        v2 = np.log10(v1[v1 > 0])
        f_set_mom[i, :] = s5(x=v2)

    saida = np.hstack((f_set_mom, e_orient, e_distribution))
    return np.mean(saida, axis=0)


#### Export features ###

curvelet = ct.fdct2(vblock, 5, 32, ac=False)  #Curvelet
image = reescalonamento_niveis(rgb2gray(data.imread(vimage)))

features = gen_split_analisys(image, curvelet, block=vblock)
if len(sys.argv) > 3:
    #Train tail
    my_df = pd.DataFrame(features)
    my_df = pd.concat((pd.DataFrame([in_score]), my_df))
    my_df.columns = [in_class]
    my_df.T.to_csv(output_csv, header=False)
else:
    #Test tail
    my_df = pd.DataFrame(features)
    my_df.T.to_csv(output_csv, index=False, header=False)
Пример #19
0
    # Parse configuration
    cfg = ConfigParser()
    cfg.read(join(root, 'config/config.cfg'))
    mapped_dir = cfg.get('dirs', 'sphere_mapping')
    
    # Ge a single image
    img_file = join(mapped_dir, '002_S_0729/gradient_25_to_50_solid_angle_to_sphere.raw')
    img = np.fromfile(img_file).reshape([180,90]).T

    # Decompose in curvelets
    nbs = 4
    nba = 16
    A = ct.fdct2(
        (90,90), 
        nbs=nbs, 
        nba=nba, 
        ac=True, 
        norm=False, 
        vec=True, 
        cpx=False)
    f = A.fwd(img)
    curv_data = get_sub_bands(A, f)
    n = len(curv_data)
    print(n, curv_data.keys())
    ncols = 9
    fig, ax = plt.subplots(ncols=ncols, nrows=np.ceil(n/ncols).astype(int))
    fig.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

    for i, (key, val) in enumerate(curv_data.items()):
        ix = i // ncols
        iy = i % ncols
        # ax[ix, iy].imshow(val)
Пример #20
0
nba = par.int("nba",8) # number of angle at the 2nd coarsest scale
ac = par.bool("ac",1) # curvelets at finest scale
adj = par.bool("adj",False) # adjoint transform

if adj:
    ### inverse transform
    assert input.size(1) == 1,"sffdct with adj=y needs a vector input"
    n1,n2 = input.ints("sizes",2)
    assert n1 and n2,"vector provided is not a curvelet vector (missing sizes header)"

    # load input in memory
    x = np.zeros(input.int("n1"),'f')
    input.read(x)

    # apply transform
    shot = np.float32(ct.fdct2((n2,n1),nbs,nba,ac).inv(x))

    # write result to file
    output.put("n1",n1)
    output.put("n2",n2)
    output.write(shot)
else:
    ### forward transform
    # read in size of input
    n1 = input.int("n1")
    n2 = input.int("n2")
    ni = input.size(2)
    assert ni == 1,"sffdct needs 2D input"
 
    # load input in memory
    shot = np.zeros((n2,n1),'f')
Пример #21
0
nba = par.int("nba", 8)  # number of angle at the 2nd coarsest scale
ac = par.bool("ac", 1)  # curvelets at finest scale
adj = par.bool("adj", False)  # adjoint transform

if adj:
    ### inverse transform
    assert input.size(1) == 1, "sffdct with adj=y needs a vector input"
    n1, n2 = input.ints("sizes", 2)
    assert n1 and n2, "vector provided is not a curvelet vector (missing sizes header)"

    # load input in memory
    x = np.zeros(input.int("n1"), 'f')
    input.read(x)

    # apply transform
    shot = np.float32(ct.fdct2((n2, n1), nbs, nba, ac).inv(x))

    # write result to file
    output.put("n1", n1)
    output.put("n2", n2)
    output.write(shot)
else:
    ### forward transform
    # read in size of input
    n1 = input.int("n1")
    n2 = input.int("n2")
    ni = input.size(2)
    assert ni == 1, "sffdct needs 2D input"

    # load input in memory
    shot = np.zeros((n2, n1), 'f')