Exemplo n.º 1
0
def bandpass_filtering(sphere_data, bpf):
    # apply bandpass filter to all the data sets

    # allocate space for the image data set
    bp_data_complex = np.zeros((num_test, res, res, 2))
    bp_data_intensity = np.zeros((num_test, res, res, 1))

    cnt = 0

    # merge the 2-channel dataset into one channel as complex images
    complex_im = sphere_data[..., 0] + sphere_data[..., 1] * 1j

    # for each image
    for i in range(num_test):

        # apply bandpass filtering
        filtered_im_complex = ms.apply_filter(simRes, simFov,
                                              complex_im[i, ...], bpf)

        # crop the field after bandpass filtering
        cropped_im_complex = ms.crop_field(res, filtered_im_complex)

        # saving them individually
        bp_data_complex[i, :, :, 0] = np.real(cropped_im_complex)
        bp_data_complex[i, :, :, 1] = np.imag(cropped_im_complex)

        bp_data_intensity[i, :, :, 0] = np.abs(cropped_im_complex)**2

        # print progress
        cnt += 1
        sys.stdout.write('\r' + str(cnt / num_test * 100) + ' %')
        sys.stdout.flush()  # important

    return bp_data_complex, bp_data_intensity
def bandpass_filtering(bpf):
    # apply bandpass filter to all the data sets

    # allocate space for the image data set
    bp_data_complex = np.zeros((num_test, res, res, 2))
    bp_data_intensity = np.zeros((num_test, res, res, 1))

    cnt = 0
    # band pass and crop
    for h in range(num):
        sphere_dir = data_dir + '\X_test_%3.3d' % (h) + '.npy'
        sphere_data = np.load(sphere_dir)

        complex_im = sphere_data[..., 0] + sphere_data[..., 1] * 1j

        for i in range(num_test_in_group):
            filtered_im_complex = ms.apply_filter(res, fov, complex_im[i, ...],
                                                  bpf)
            bp_data_complex[h * num_test_in_group + i, :, :,
                            0] = np.real(filtered_im_complex)
            bp_data_complex[h * num_test_in_group + i, :, :,
                            1] = np.imag(filtered_im_complex)

            bp_data_intensity[h * num_test_in_group + i, :, :,
                              0] = np.abs(filtered_im_complex)**2

            # print progress
            cnt += 1
            sys.stdout.write('\r' + str(cnt / num_test * 100) + ' %')
            sys.stdout.flush()  # important

    return bp_data_complex, bp_data_intensity
Exemplo n.º 3
0
    nr = np.linspace(nr_min, nr_max, num)
    ni = np.linspace(ni_min, ni_max, num)

    return a, nr, ni


#%%
# set the size and resolution of both planes
fov = 16  # field of view
res = 128  # resolution
lambDa = 1  # wavelength
k = 2 * math.pi / lambDa  # wavenumber
padding = 3  # padding
# simulation resolution
# in order to do fft and ifft, expand the image use padding
simRes, simFov = ms.pad(res, fov, padding)
ps = [0, 0, 0]  # position of the sphere
k_dir = [0, 0, -1]  # propagation direction of the plane wave
E = [1, 0, 0]  # electric field vector
working_dis = ms.get_working_dis(padding)
# scale factor of the intensity
scale_factor = ms.get_scale_factor(simRes, simFov, working_dis)
# define feature space
num = 20
num_samples = num**3

a_min = 1.0
a_max = 2.0

nr_min = 1.1
nr_max = 2.0
Exemplo n.º 4
0
    noise_pc = np.linspace(noise_pc_min, noise_pc_max, num_bp)

    return a, nr, ni, noise_pc


#%%
# set the size and resolution of both planes
fov = 32  # field of view
res = 256  # resolution

lambDa = 1  # wavelength

k = 2 * math.pi / lambDa  # wavenumber
padding = 2  # padding

simRes, simFov = ms.pad(res, fov, padding)
working_dis = ms.get_working_dis(padding)
scale_factor = ms.get_scale_factor(res, fov, working_dis)

line_size = int(simRes / 2)

ps = [0, 0, 0]  # position of the sphere
k_dir = [0, 0, -1]  # propagation direction of the plane wave
E = [1, 0, 0]  # electric field vector

# define feature space

num = 10
num_bp = 100
num_samples = num**3 * num_bp
Exemplo n.º 5
0
# position of the sphere
ps = [0, 0, 0]
# resolution of the cropped image
res = 128
# in and out numerical aperture of the bandpass optics
NA_in = 0
NA_out = 0
# wave length
lambDa = 1
# wavenumber
k = 2 * np.pi / lambDa
# padding size
padding = 2
# field of view
fov = 16
simRes, simFov = ms.pad(res, fov, padding)
# set ranges of the features of the data set
# refractive index
n_r_min = 1.1
n_r_max = 2.0
# attenuation coefficient
n_i_min = 0.01
n_i_max = 0.05
# sphere radius
a_min = 5
a_max = 10

# the z position of the visiulization plane
z_max = a_max
# the maximal order
l = ms.get_order(a_max, lambDa)
Exemplo n.º 6
0
#%%
# set parameters
fov = 32                    # field of view
res = 256                   # resolution
a = 2                       # radius of the spere
lambDa = 1                  # wavelength
n = 1.4 + 1j * 0.02            # refractive index
k = 2 * math.pi / lambDa    # wavenumber
padding = 0                 # padding
working_dis = 10000 * (2 * padding + 1)           # working distance
scale_factor = working_dis * 2 * math.pi * res/fov            # scale factor of the intensity
NA_in = 0.0
NA_out = 1

simRes, simFov = ms.pad(res, fov, padding)

ps = [0, 0, 0]              # position of the sphere
k_dir = [0, 0, -1]          # propagation direction of the plane wave
E = [1, 0, 0]               # electric field vector
E0 = 1

#%%
# get 1-D far field
E_far_line = ms.far_field(simRes, simFov, working_dis, a, n, 
                          lambDa, k_dir, scale_factor, dimension=1)
# get 1-D near line without bandpass filtering
E_near_line, E_near_x = ms.idhf(simRes, simFov, E_far_line)
# get 1-D bandpass filter
bpf_line = ms.bandpass_filter(simRes, simFov, NA_in, NA_out, dimension=1)
# get 1-D near field and its sample index
Exemplo n.º 7
0
import numpy as np
import math
import matplotlib.pyplot as plt
import chis.MieScattering as ms

#%%
# set parameters
fov = 16                    # field of view
res = 128                   # resolution
a = 1                       # radius of the spere
lambDa = 1                  # wavelength
n = 1.5 + 1j*0.01            # refractive index
k = 2 * math.pi / lambDa    # wavenumber
padding = 1                 # padding

simRes, simFov = ms.pad(res, fov, padding)
working_dis = 10000 * (2 * padding + 1)           # working distance
scale_factor = working_dis * 2 * math.pi * res/fov            # scale factor of the intensity
NA_in = 0.3
NA_out = 0.6

ps = [0, 0, 0]              # position of the sphere
k_dir = [0, 0, -1]          # propagation direction of the plane wave
E = [1, 0, 0]               # electric field vector
E0 = 1
#%%
# get far field
E_far = ms.far_field(simRes, simFov, working_dis, a, n, lambDa, scale_factor)

# get near field
E_near = ms.far2near(E_far) + E0
# for the meanings of these parameters
# by keep the cursor in the function and hit Ctrl+I
k = [0, 0, -1]
res = 256
numSample = 1000
NA_in = 0
NA_out = 0
numFrames = 70
option = 'Horizontal'
n0 = 1.2
fov = 8
padding = 0
a = 1
pp = 0
ps = [0, 0, 0]
simRes, simFov = ms.pad(res, fov, padding)

#%%
# calculate the horizontal simulation
Et_h, B0, Emask0, rVecs = traditional_mie.getTotalField(
    k, k, n0, res, a, ps, pp, padding, fov, numSample, NA_in, NA_out, option)
# calculate the vertical simulation
Et_v, B1, Emask1, rVecs_v = traditional_mie.getTotalField(
    k, k, n0, res, a, ps, pp, padding, fov, numSample, NA_in, NA_out,
    'Vertical')

# propagate the horizontal simulation
# the field can be propagated along the k direction for d distance
d = 1
Et_0_p = ms.propagate_2D(simRes, simFov, Et_h, d)
        raise ValueError('Invalid Value for dimension')

    return E_crop


#%%
# specify parameters
fov = 32  # field of view
res = 256  # resolution
a = 1  # radius of the spere
lambDa = 1  # wavelength
n = 1.5 + 1j * 0.01  # refractive index
k = 2 * np.pi / lambDa  # wavenumber
padding = 1  # padding

simRes, simFov = ms.pad(res, fov, padding)
working_dis = ms.get_working_dis(padding)
scale_factor = ms.get_scale_factor(
    res, fov, working_dis)  # scale factor of the intensity
NA_in = 0.0
NA_out = 1.0

ps = [0, 0, 0]  # position of the sphere
k_dir = [0, 0, -1]  # propagation direction of the plane wave
E = [1, 0, 0]  # electric field vector
E0 = 1

shift1 = 0
shift2 = 99
#%%
# get 1-D far field
Exemplo n.º 10
0
#%%
# load data set

raw_im = np.load(
    r'D:\irimages\irholography\CNN\data_v9_far_field\raw_data\im_data010.npy')
raw_im = np.reshape(raw_im, (640, 640, 2, 400))

raw_im = np.swapaxes(raw_im, 0, -1)
raw_im = np.swapaxes(raw_im, -2, -1)

data_dir = r'D:\irimages\irholography\CNN\data_v9_far_field'

#%%
num_test = 400
lambDa = 1
NA_in = 0
NA_out = 0.25
res = 128
fov = 16
padding = 2
simRes, simFov = ms.pad(res, fov, padding)

# create a bandpass filter
bpf = ms.bandpass_filter(simRes, simFov, NA_in, NA_out, dimension=2)
# apply bandpass filtering to all the images
im_bp_comp, im_bp_inten = bandpass_filtering(raw_im, bpf)

#%%
# create an animation
animation.anime(im_bp_inten, 20, data_dir, 'intensityBP0.25', 'Real')
    
    # crop the image
    E_crop = E[y_lower: y_upper, x_left: x_right]
    
    return E_crop
#%%
# specify parameters
fov = 16                    # field of view
res = 256                   # resolution
a = 1                       # radius of the spere
lambDa = 1                  # wavelength
n = 1.5 + 1j*0.01            # refractive index
k = 2 * np.pi / lambDa    # wavenumber
padding = 1                 # padding

simRes, simFov = ms.pad(res, fov, padding)
working_dis = ms.get_working_dis(padding)
scale_factor = ms.get_scale_factor(res, fov, working_dis)            # scale factor of the intensity
NA_in = 0.0
NA_out = 1.0

ps = [0, 0, 0]              # position of the sphere
k_dir = [0, 0, -1]          # propagation direction of the plane wave
E = [1, 0, 0]               # electric field vector
E0 = 1

shift1 = 0
shift2 = int(simRes/8)
#%%
# get far field
E_far = ms.far_field(simRes, simFov, working_dis, a, n, lambDa, k_dir, scale_factor)
Exemplo n.º 12
0
n_i_min = 0.01
n_i_max = 0.05

a_min = 5
a_max = 10

nb_nr = 25
nb_ni = 25
nb_a = 25

nr = np.linspace(n_r_min, n_r_max, nb_nr)
ni = np.linspace(n_i_min, n_i_max, nb_ni)
a = np.linspace(a_min, a_max, nb_a)

l = ms.get_order(a_max)

im_data = np.zeros((res, res, 2, nb_nr, nb_ni, nb_a))
sphere_data = np.zeros((3, nb_nr, nb_ni, nb_a))
B_data_real = np.zeros((len(l), nb_nr, nb_ni, nb_a))
B_data_imag = np.zeros((len(l), nb_nr, nb_ni, nb_a))

cnt = 0
for i in range(nb_nr):
    for j in range(nb_ni):
        for h in range(nb_a):

            n0 = nr[i] + ni[j] * 1j
            a0 = a[h]
            #position of the visualization plane, along z axis
            pp = a0
intensity_CNN = load_model(
    r'D:\irimages\irholography\CNN\CNN_v12_far_field_a_n\intensity\intensity_model30.h5'
)

# parent directory of the data set
data_dir = r'D:\irimages\irholography\CNN\data_v10_far_field\split_data\test'

# allocate space for complex and intensity accuracy
complex_error = np.zeros((nb_NA, 3), dtype=np.float64)
intensity_error = np.zeros((nb_NA, 3), dtype=np.float64)

# for each NA to be tested
for NA_idx in range(nb_NA):

    # calculate the band pass filter
    bpf = ms.bandpass_filter(simRes, simFov, NA_in, NA_list[NA_idx])

    # print some info about the idx of NA
    print('Banbpassing the ' + str(NA_idx + 1) + 'th filter \n')
    im_data_complex, im_data_intensity = bandpass_filtering(bpf)

    print('Evaluating complex model \n')
    # handle complex model first
    complex_error[NA_idx, :] = calculate_error(im_data_complex,
                                               option='complex')

    print('Evaluating intensity model \n')
    # handle intensity model second
    intensity_error[NA_idx, :] = calculate_error(im_data_intensity,
                                                 option='intensity')
Exemplo n.º 14
0
    S_noise = S + eta_r + 1j * eta_i

    return S_noise


# set the size and resolution of both planes
fov = 16  # field of view
res = 128  # resolution

lambDa = 1  # wavelength

k = 2 * math.pi / lambDa  # wavenumber
padding = 2  # padding

simRes, simFov = ms.pad(res, fov, padding)
working_dis = ms.get_working_dis(padding)
scale_factor = ms.get_scale_factor(res, fov, working_dis)

line_size = int(simRes / 2)

ps = [0, 0, 0]  # position of the sphere
k_dir = [0, 0, -1]  # propagation direction of the plane wave
E = [1, 0, 0]  # electric field vector

# define feature space

num = 20
num_samples = num**4

a_min = 1.0
# allocate space for data set
sphere_data = np.zeros((3, num, num, num, num_bp))
im_data = np.zeros((line_size, 2, num, num, num, num_bp))
im_dir = r'D:\irimages\irholography\CNN\data_v12_far_line\more_bandpass_HD\im_data'

# get all the bandpass filters
bpfs = []
for b in range(num_bp):
                
    bp0 = bp[b]

    bpf = get_bpf(simFov, simRes, 0, bp0)
    bpf_line = bpf[0, :int(simRes/2)+1]
    
    bpf_line0 = ms.bandpass_filter(simRes, simFov, 0, bp0, dimention=1)
    
    bpfs.append(bpf_line)

cnt = 0
for h in range(num):
    for i in range(num):
        for j in range(num):
            
            a0 = a[h]
            n0 = nr[i] + 1j*ni[j]
            
            B = coeff_b(l, k, n0, a0)

            # integrate through all the orders to get the farfield in the Fourier Domain
            E_scatter_fft = np.sum(scatter_matrix * B, axis = -1) * scale_factor