Exemplo n.º 1
0
def test_ssim(space):
    ground_truth = odl.phantom.white_noise(space)

    # SSIM of true image should be either
    # * 1 with force_lower_is_better == False,
    # * -1 with force_lower_is_better == True and normalized == False,
    # * 0 with force_lower_is_better == True and normalized == True.
    result = fom.ssim(ground_truth, ground_truth)
    assert result == pytest.approx(1)

    result_normalized = fom.ssim(ground_truth, ground_truth, normalized=True)
    assert result_normalized == pytest.approx(1)

    result_flib = fom.ssim(ground_truth,
                           ground_truth,
                           force_lower_is_better=True)
    assert result_flib == pytest.approx(-1)

    result_nf = fom.ssim(ground_truth,
                         ground_truth,
                         normalized=True,
                         force_lower_is_better=True)
    assert result_nf == pytest.approx(0)

    # SSIM with ground truth zero should always give zero if not normalized
    # and 1/2 otherwise.
    data = odl.phantom.white_noise(space)

    result = fom.ssim(data, space.zero())
    assert result == pytest.approx(0)

    result_normalized = fom.ssim(data, space.zero(), normalized=True)
    assert result_normalized == pytest.approx(0.5)

    result_flib = fom.ssim(data, space.zero(), force_lower_is_better=True)
    assert result_flib == pytest.approx(0)

    result_nf = fom.ssim(data,
                         space.zero(),
                         normalized=True,
                         force_lower_is_better=True)
    assert result_nf == pytest.approx(0.5)

    # SSIM should be symmetric if the dynamic range is set explicitly.
    for nor in [True, False]:
        for flib in [True, False]:
            result1 = fom.ssim(data,
                               ground_truth,
                               dynamic_range=1,
                               normalized=nor,
                               force_lower_is_better=flib)
            result2 = fom.ssim(ground_truth,
                               data,
                               dynamic_range=1,
                               normalized=nor,
                               force_lower_is_better=flib)
            assert result1 == pytest.approx(result2)
    def __call__(self, x, **kwargs):

        if len(x) == 2:
            x = x[0]

        k = self.iter_count

        if k in self.iter_save:
            if self.obj_fun is not None:
                self.obj.append(self.obj_fun(x))

        if k in self.iter_plot:
            name = '{}{:04d}'.format(self.prefix, k)

            gtruth = self.gtruth
            if gtruth is not None:
                name += '_psnr{:.1f}db_ssim{:0.4f}'.format(
                    psnr(x, gtruth), ssim(x, gtruth))

            save_result(x, name)
            if self.error and gtruth is not None:
                save_error(x - gtruth, name + '_error')

        self.iter_count += 1
    #                saveto=(save_path + 'cp_iter_{}').format(imax_val))

    fig = plt.figure()
    plt.imshow(np.rot90(x_rec_odl.asarray()), cmap='bone', clim=[0, 2.33])
    plt.xticks([])
    plt.yticks([])
    fig.savefig((save_path + 'cp_iter_{}').format(imax_val),
                transparent=True,
                bbox_inches='tight',
                pad_inches=0)

    fig = plt.figure()
    plt.imshow(np.rot90(x_rec_odl.asarray()), cmap='bone', clim=[0.8, 1.2])
    plt.xticks([])
    plt.yticks([])
    fig.savefig((save_path + 'cp_iter_{}_windowed').format(imax_val),
                transparent=True,
                bbox_inches='tight',
                pad_inches=0)

    mse_val = mean_squared_error(x_rec_odl, x_true_odl)
    ssim_val = ssim(x_rec_odl, x_true_odl)

    print('validation loss={}, data dis={}, regularizer={}'.format(
        loss_result, data_disc_res, reg_res))

    print('mse={}, ssim={}'.format(mse_val, ssim_val))

sys.stdout.log.close()
sys.stdout = sys.stdout.terminal
Exemplo n.º 4
0
odl.solvers.douglas_rachford_pd(x,
                                f,
                                g,
                                L,
                                tau,
                                sigmas,
                                niter=200,
                                callback=callback)

# %% Show results

x_true.show('True image', force_show=True)
y.show('Noisy image', force_show=True)
x.show('Denoised image', force_show=True)
(x_true - x).show('Difference true - denoised', force_show=True)

# %% Compute some image quality metrics

print('Noisy')
print('-----')
print('Mean squared error:', fom.mean_squared_error(y, x_true))
print('PSNR:', fom.psnr(y, x_true))
print('SSIM:', fom.ssim(y, x_true))
print('')

print('Denoised')
print('--------')
print('Mean squared error:', fom.mean_squared_error(x, x_true))
print('PSNR:', fom.psnr(x, x_true))
print('SSIM:', fom.ssim(x, x_true))
Exemplo n.º 5
0
    blur.append(
        fom.blurring(phantom_noisy,
                     phantom,
                     mask,
                     normalized=True,
                     smoothness_factor=30))

    false_struct.append(
        fom.false_structures(phantom_noisy,
                             phantom,
                             mask,
                             normalized=True,
                             smoothness_factor=30))

    ssim.append(fom.ssim(phantom_noisy, phantom))

    psnr.append(fom.psnr(phantom_noisy, phantom, normalized=True))

    haarpsi.append(fom.haarpsi(phantom_noisy, phantom))

fig, ax = plt.subplots()
ax.plot(mse, label='MSE')
ax.plot(mae, label='MAE')
ax.plot(mvd, label='MVD')
ax.plot(std_diff, label='SDD')
ax.plot(range_diff, label='RD')
ax.plot(blur, label='BLUR')
ax.plot(false_struct, label='FS')
ax.plot(ssim, label='SSIM')
ax.plot(haarpsi, label='HaarPSI')
Exemplo n.º 6
0
    blur.append(
        fom.blurring(phantom_noisy,
                     phantom,
                     mask,
                     normalized=True,
                     smoothness_factor=30))

    false_struct.append(
        fom.false_structures(phantom_noisy,
                             phantom,
                             mask,
                             normalized=True,
                             smoothness_factor=30))

    ssim.append(fom.ssim(phantom_noisy, phantom, normalized=True))

plt.figure('Figures of merit')
plt.plot(mse, label='Mean squared error')
plt.plot(mae, label='Mean absolute error')
plt.plot(mvd, label='Mean value difference')
plt.plot(std_diff, label='Standard deviation difference')
plt.plot(range_diff, label='Range difference')
plt.plot(blur, label='Blurring')
plt.plot(false_struct, label='False structures')
plt.plot(ssim, label='Structural similarity')

plt.legend(loc='upper center',
           bbox_to_anchor=(0.5, -0.15),
           fancybox=True,
           shadow=True,