Пример #1
0
def poop():
    global sources

    meas_focus = get_measurements(sources=sources,
                                  mode='circular',
                                  psfs=psfs_focus,
                                  real=True)
    meas_mid = get_measurements(sources=sources,
                                mode='circular',
                                psfs=psfs_mid,
                                real=True)
    meas_outer = get_measurements(sources=sources,
                                  mode='circular',
                                  psfs=psfs_outer,
                                  real=True)
    meas_csbs = get_measurements(sources=sources,
                                 mode='circular',
                                 psfs=psfs_csbs,
                                 real=True)

    meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=15)
    meas_mid = add_noise(meas_mid, model='gaussian', dbsnr=15)
    meas_outer = add_noise(meas_outer, model='gaussian', dbsnr=15)
    meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=15)
    # meas_focus = add_noise(meas_focus, model='poisson', max_count=20)
    # meas_mid = add_noise(meas_mid, model='poisson', max_count=20)
    # meas_outer = add_noise(meas_outer, model='poisson', max_count=20)
    # meas_csbs = add_noise(meas_csbs, model='poisson', max_count=20)

    recon_focus = tikhonov(psfs=psfs_focus,
                           measurements=meas_focus,
                           tikhonov_lam=1e-3)
    recon_mid = tikhonov(psfs=psfs_mid,
                         measurements=meas_mid,
                         tikhonov_lam=1e-3)
    recon_outer = tikhonov(psfs=psfs_outer,
                           measurements=meas_outer,
                           tikhonov_lam=1e-3)
    recon_csbs = tikhonov(psfs=psfs_csbs,
                          measurements=meas_csbs,
                          tikhonov_lam=1e-3)

    recon_focus -= np.mean(recon_focus, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_mid -= np.mean(recon_mid, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_outer -= np.mean(recon_outer, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_csbs -= np.mean(recon_csbs, axis=(1, 2))[:, np.newaxis, np.newaxis]
    sources_comp = sources - np.mean(sources, axis=(1, 2))[:, np.newaxis,
                                                           np.newaxis]

    result_focus = np.sum(compare_ssim(sources_comp, recon_focus))
    result_mid = np.sum(compare_ssim(sources_comp, recon_mid))
    result_outer = np.sum(compare_ssim(sources_comp, recon_outer))
    result_csbs = np.sum(compare_ssim(sources_comp, recon_csbs))

    print('focused SSIM sum:', result_focus)
    print('middle SSIM sum:', result_mid)
    print('outer SSIM sum:', result_outer)
    print('csbs SSIM sum:', result_csbs)

    return [result_focus, result_mid, result_outer, result_csbs]
Пример #2
0
        def measure_reconstruct():

            noise = 15  # dB SNR

            meas_focus = get_measurements(sources=sources,
                                          mode='circular',
                                          psfs=psfs_focus,
                                          real=True)
            meas_csbs = get_measurements(sources=sources,
                                         mode='circular',
                                         psfs=psfs_csbs,
                                         real=True)

            meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=noise)
            meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=noise)

            recon_focus = tikhonov(psfs=psfs_focus,
                                   measurements=meas_focus,
                                   tikhonov_lam=1e-3)
            recon_csbs = tikhonov(psfs=psfs_csbs,
                                  measurements=meas_csbs,
                                  tikhonov_lam=1e-3)
            result_focus = np.sum(compare_ssim(sources, recon_focus))
            result_csbs = np.sum(compare_ssim(sources, recon_csbs))

            ratio = result_csbs / result_focus

            return dict(**locals())
Пример #3
0
    def test_tikhonov(self):
        from mas.deconvolution import tikhonov

        recon = tikhonov(measurements=np.fft.fftshift(self.measured_noisy,
                                                      axes=(1, 2)),
                         psfs=self.psfs,
                         tikhonov_lam=5e-2,
                         tikhonov_order=1)
Пример #4
0
def cost(tikhonov_lam_exp, csbs_lam_exp):

    # csbs_grid
    psfs.copies = np.repeat(12, 12)
    # csbs_focus
    # psfs.copies = np.repeat(12, 2)
    csbs(psfs, sse_cost, 12, lam=10**csbs_lam_exp, order=1)
    measured = get_measurements(psfs=psfs, sources=truth, real=True)
    measured_noisy = add_noise(measured, model='poisson', max_count=500)
    recon = tikhonov(
            sources=measured,
            psfs=psfs,
            measurements=measured_noisy,
            tikhonov_lam=10**tikhonov_lam_exp
    )

    plt.imshow(recon[0])
    plt.show()
    plt.pause(.05)
    return compare_ssim(truth[0], recon[0]) + compare_ssim(truth[1], recon[1])
Пример #5
0
def motion_deblur(*, sv, coadded, drift):
    """

    """
    pixel_size_um = sv.pixel_size * 1e6

    # width of the final motion blur kernel with CCD pixel size
    kernel_size = 11
    (x, y) = (pixel_size_um * drift[0], pixel_size_um * drift[1])
    N = int(np.ceil(np.max((abs(x), abs(y)))))

    # set the shape of the initial kernel with 1 um pixels based on the estimated drift
    kernel_um = np.zeros((2 * N + 1, 2 * N + 1))

    # calculate the line representing the motion blur
    rr, cc, val = line_aa(
        N + np.round((y / 2)).astype(int),
        N - np.round((x / 2)).astype(int),
        N - np.round((y / 2)).astype(int),
        N + np.round((x / 2)).astype(int),
    )

    # update the kernel with the calculated line
    kernel_um[rr, cc] = val

    # resize the initial 1 um kernel to the given pixel size
    kernel = resize(size_equalizer(kernel_um,
                                   [int(pixel_size_um) * kernel_size] * 2),
                    [kernel_size] * 2,
                    anti_aliasing=True)
    # compute the analytical photon sieve PSF with the given pixel size
    psfs = copy.deepcopy(sv.psfs)

    # convolve the photon sieve PSF with the motion blur kernel to find the "effective blurring kernel"
    psfs.psfs[0, 0] = convolve2d(psfs.psfs[0, 0], kernel, mode='same')

    # normalize the kernel
    psfs.psfs[0, 0] /= psfs.psfs[0, 0].sum()

    # normalize the coadded image (doesn't change anything but helps choosing regularization parameter consistently)
    coadded /= coadded.max()

    # do a tikhonov regularized deblurring on the coadded image to remove the
    # in-frame blur with the calculated "effective blurring kernel"
    recon_tik = tikhonov(measurements=coadded[np.newaxis, :, :],
                         psfs=psfs,
                         tikhonov_lam=1e1,
                         tikhonov_order=1)
    plt.figure()
    plt.imshow(recon_tik[0], cmap='gist_heat')
    plt.title('Deblurred Tikhonov')
    plt.show()

    # do a Plug and Play with BM3D reconstruction with tikhonov initialization
    recon = admm(measurements=coadded[np.newaxis, :, :],
                 psfs=psfs,
                 regularizer=partial(bm3d_pnp),
                 recon_init=recon_tik,
                 plot=False,
                 iternum=5,
                 periter=1,
                 nu=10**-0.0,
                 lam=[10**-0.5])

    plt.figure()
    plt.imshow(recon[0], cmap='gist_heat')
    plt.title('Deblurred')
    plt.show()

    return recon
Пример #6
0
# measured = get_measurements(
#     sources=sources,
#     mode='circular',
#     meas_size=meas_size,
#     psfs=psfs,
#     blur_sigma=1,
#     drift_amount=5
# )

measured_noisy = add_noise(measured, max_count=100, model='Poisson')

# %% recon ---------------------------------

recon = tikhonov(
    measurements=measured_noisy,
    psfs=psfs,
    tikhonov_lam=2e-2,
    tikhonov_order=1
)
recon = admm(
    sources=sources_ref,
    measurements=measured_noisy,
    psfs=psfs,
    regularizer=partial(
        # patch_based
        # TV
        bm3d_pnp
        # dncnn_pnp, model=model
        ),
    recon_init=recon,
    iternum=20,
    periter=5,
Пример #7
0
noise = 15  # dB SNR

meas_focus = get_measurements(sources=sources,
                              mode='circular',
                              psfs=psfs_focus,
                              real=True)
meas_csbs = get_measurements(sources=sources,
                             mode='circular',
                             psfs=psfs_csbs,
                             real=True)

meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=noise)
meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=noise)

recon_focus = tikhonov(psfs=psfs_focus,
                       measurements=meas_focus,
                       tikhonov_lam=1e-3)
recon_csbs = tikhonov(psfs=psfs_csbs,
                      measurements=meas_csbs,
                      tikhonov_lam=1e-3)
result_focus = np.sum(compare_ssim(sources, recon_focus))
result_csbs = np.sum(compare_ssim(sources, recon_csbs))

print(result_csbs / result_focus, result_focus)

# print('focused SSIM:', result_focus)
# print('csbs SSIM:', result_csbs)

# recon_focus_mean = recon_focus - np.mean(recon_focus, axis=(1, 2))[:, np.newaxis, np.newaxis]
# recon_csbs_mean = recon_csbs - np.mean(recon_csbs, axis=(1, 2))[:, np.newaxis, np.newaxis]
# sources_mean = sources - np.mean(sources, axis=(1, 2))[:, np.newaxis, np.newaxis]
Пример #8
0
    source_wavelengths=source_wavelengths,
    psf_generator=circ_incoherent_psf,
    # image_width=psf_width,
    num_copies=1)

measured = get_measurements(sources=sources,
                            mode='valid',
                            meas_size=meas_size,
                            psfs=psfs)
measured_noisy = add_noise(measured, max_count=10, model='Poisson')

# %% recon ------------------------------------

recon = tikhonov(sources=sources_ref,
                 measurements=measured_noisy,
                 psfs=psfs,
                 tikhonov_lam=2e-1,
                 tikhonov_order=1)
if deconvolver == admm:
    recon = admm(
        sources=sources_ref,
        measurements=measured_noisy,
        psfs=psfs,
        regularizer=partial(
            # patch_based
            # TV
            bm3d_pnp
            # dncnn_pnp, model=model
        ),
        recon_init=recon,
        iternum=20,
Пример #9
0
    num_copies=6)

psfs_csbs = csbs(psfs_csbs, sse_cost, 12, lam=csbs_lam, order=order)

# %% measure
psnr_focus, psnr_csbs, ssim_focus, ssim_csbs = [], [], [], []
measured_focus = get_measurements(sources=sources, psfs=psfs_focus)
measured_csbs = get_measurements(sources=sources, psfs=psfs_csbs)
for i in range(50):
    measured_noisy_focus = add_noise(measured_focus,
                                     dbsnr=15,
                                     model='Gaussian')
    measured_noisy_csbs = add_noise(measured_csbs, dbsnr=15, model='Gaussian')

    recon_focus = tikhonov(psfs=psfs_focus,
                           measurements=measured_noisy_focus,
                           tikhonov_lam=1e-3,
                           tikhonov_order=order)
    recon_csbs = tikhonov(psfs=psfs_csbs,
                          measurements=measured_noisy_csbs,
                          tikhonov_lam=1e-3,
                          tikhonov_order=order)

    psnr_focus.append(compare_psnr(sources, recon_focus))
    psnr_csbs.append(compare_psnr(sources, recon_csbs))
    ssim_focus.append(np.sum(compare_ssim(sources, recon_focus)))
    ssim_csbs.append(np.sum(compare_ssim(sources, recon_csbs)))

print('psnr_focus:{} \npsnr_csbs:{} \nssim_focus:{} \nssim_csbs:{}'.format(
    np.array(psnr_focus).mean(),
    np.array(psnr_csbs).mean(),
    np.array(ssim_focus).mean(),