示例#1
5
def get_ssim(actual, expected):
    im = Image.fromarray(actual)
    im2 = Image.fromarray(expected)

    if im.size[0] != im2.size[0] or im.size[1] != im2.size[1]:
        raise RuntimeError(
            "Can't calculate SSIM for images of different sizes (one is %dx%d, the other %dx%d)." % (
                im.size[0], im.size[1],
                im2.size[0], im2.size[1],
            )
        )
    return structural_similarity(np.array(im), np.array(im2), multichannel=True)
示例#2
0
def get_ssim(actual, expected):
    im = Image.fromarray(actual)
    im2 = Image.fromarray(expected)

    if im.size[0] != im2.size[0] or im.size[1] != im2.size[1]:
        raise RuntimeError(
            "Can't calculate SSIM for images of different sizes (one is %dx%d, the other %dx%d)." % (
                im.size[0], im.size[1],
                im2.size[0], im2.size[1],
            )
        )
    return structural_similarity(np.array(im), np.array(im2), multichannel=True)
示例#3
0
def ssim(original, result, max_intensity, crop=7):
    #color image in the form 3HW or 4HW
    original = original[0:3, crop:-crop, crop:-crop]
    result = result[0:3, crop:-crop, crop:-crop]
    original = to_gray(original)
    result = to_gray(result)

    original = np.maximum(0, np.minimum(original, max_intensity))
    result = np.maximum(0, np.minimum(result, max_intensity))

    return structural_similarity(original.astype("float32"),
                                 result.astype("float32"),
                                 dynamic_range=max_intensity)
示例#4
0
def ssim(image1, image2):
    """Calculate the Structural Similarity (SSIM) index between two images

    Uses the SSIM implemented in the Scikit-image package with the same parameters used in [1].

    References
    ----------
    .. [1] Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P.
       (2004). Image quality assessment: From error visibility to
       structural similarity. IEEE Transactions on Image Processing,
       13, 600-612.
       https://ece.uwaterloo.ca/~z70wang/publications/ssim.pdf
    """
    return structural_similarity(image1, image2, gaussian_weights=True, sigma=1.5, use_sample_covariance=False)
示例#5
0
def main():

#    image = data.coins() # or any NumPy array!
#    edges = filter.sobel(image)
#    io.imshow(edges)
#    io.show()

    image_file_name_0 = '/local/decarlo/projects/data/microCT/0_180/hdf4/tilt_020_020_0001.hdf'
    image_file_name_180 = '/local/decarlo/projects/data/microCT/0_180/hdf4/tilt_020_020_0002.hdf'
    image_file_name_white = '/local/decarlo/projects/data/microCT/0_180/hdf4/tilt_020_020_0003.hdf'

    image_0 = read_hdf4(image_file_name_0, 'data')
    image_180 = read_hdf4(image_file_name_180, 'data')
    image_white = read_hdf4(image_file_name_white, 'data')

    image_0 = normalize (image_0, image_white)
    image_180 = normalize (image_180, image_white)
    
    plt.imshow(image_0+image_180, cmap=plt.cm.hot)
    plt.colorbar()
    plt.show()

    image_180 = np.fliplr(image_180)

    tform = tf.estimate_transform('similarity', image_0, image_180)

    a, grad = structural_similarity(image_0, image_180, gradient=True)
    print a
    print "grad shape", grad.shape

#    print grad
    plt.imshow(grad, cmap=plt.cm.hot)
    plt.colorbar()
    plt.show()

    result = match_template(image_0, image_180)
    print result.shape
    ij = np.unravel_index(np.argmax(result), result.shape)
    x, y = ij[::-1]
    print x, y

    im2, scale, angle, t = similarity(image_0, image_180)
    print "Scale: ", scale, "Angle: ", angle, "Transforamtion Matrix: ", t

    rot_axis_shift_x = -t[0]/2.0
    rot_axis_tilt = -t[1]/1.0
    
    print "Rotation Axis Shift (x, y):", "(", rot_axis_shift_x, ",", rot_axis_tilt,")"
示例#6
0
def test_denoise_tv_chambolle_weighting():
    # make sure a specified weight gives consistent results regardless of
    # the number of input image dimensions
    rstate = np.random.RandomState(1234)
    img2d = astro_gray.copy()
    img2d += 0.15 * rstate.standard_normal(img2d.shape)
    img2d = np.clip(img2d, 0, 1)

    # generate 4D image by tiling
    img4d = np.tile(img2d[..., None, None], (1, 1, 2, 2))

    w = 0.2
    denoised_2d = restoration.denoise_tv_chambolle(img2d, weight=w)
    denoised_4d = restoration.denoise_tv_chambolle(img4d, weight=w)
    assert measure.structural_similarity(denoised_2d, denoised_4d[:, :, 0,
                                                                  0]) > 0.99
示例#7
0
def test_denoise_tv_chambolle_weighting():
    # make sure a specified weight gives consistent results regardless of
    # the number of input image dimensions
    rstate = np.random.RandomState(1234)
    img2d = astro_gray.copy()
    img2d += 0.15 * rstate.standard_normal(img2d.shape)
    img2d = np.clip(img2d, 0, 1)

    # generate 4D image by tiling
    img4d = np.tile(img2d[..., None, None], (1, 1, 2, 2))

    w = 0.2
    denoised_2d = restoration.denoise_tv_chambolle(img2d, weight=w)
    denoised_4d = restoration.denoise_tv_chambolle(img4d, weight=w)
    assert measure.structural_similarity(denoised_2d,
                                         denoised_4d[:, :, 0, 0]) > 0.99
示例#8
0
def main():
    file1, file2 = sys.argv[1:1 + 2]
    # read images as 2D arrays (convert to grayscale for simplicity)
    img1 = to_grayscale(imread(file1).astype(float))
    img2 = to_grayscale(imread(file2).astype(float))
    # compare
    n_m, n_0 = compare_images(img1, img2)
    #n_mse = mse(imread(file1),imread(file2))
    n_mse = mse(img1, img2)
    n_psnr = PSNR(n_mse)
    n_ssim = structural_similarity(img1, img2)
    print "Manhattan norm:", n_m, "/ per pixel:", n_m / img1.size
    print "Zero norm:", n_0, "/ per pixel:", n_0 * 1.0 / img1.size
    print "Mean Square Error:", n_mse
    print "Peak Signal to Noise Ratio:", n_psnr
    print "SSIM:", n_ssim
示例#9
0
def ssim(image1, image2):
    """Calculate the Structural Similarity (SSIM) index between two images

    Uses the SSIM implemented in the Scikit-image package with the same parameters used in [1].

    References
    ----------
    .. [1] Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P.
       (2004). Image quality assessment: From error visibility to
       structural similarity. IEEE Transactions on Image Processing,
       13, 600-612.
       https://ece.uwaterloo.ca/~z70wang/publications/ssim.pdf
    """
    return structural_similarity(image1,
                                 image2,
                                 gaussian_weights=True,
                                 sigma=1.5,
                                 use_sample_covariance=False)
示例#10
0
def calculate_ssim(original_filename,test_filename,multichannel=True):
    original_img = cv2.imread(original_filename)
    test_img = cv2.imread(test_filename)
    result = structural_similarity(original_img, test_img, multichannel=multichannel)
    # print(result)
    return result
def interpolate_image(path, print_pics=False):
    orig_img = plt.imread(path)
    print('Original Dimensions : ', orig_img.shape)
    orig_dim = (orig_img.shape[1], orig_img.shape[0])  # (width, height)

    scale_percent = 20  # percent of original size
    width = int(orig_img.shape[1] * scale_percent / 100)
    height = int(orig_img.shape[0] * scale_percent / 100)
    dim = (width, height)
    downsampled_img = cv2.resize(orig_img, dim, interpolation=cv2.INTER_AREA)

    print('Downsampled Dimensions : ', downsampled_img.shape)

    # Upscale image
    resized_NN = cv2.resize(downsampled_img,
                            orig_dim,
                            interpolation=cv2.INTER_NEAREST)
    resized_BL = cv2.resize(downsampled_img,
                            orig_dim,
                            interpolation=cv2.INTER_LINEAR)
    resized_BC = cv2.resize(downsampled_img,
                            orig_dim,
                            interpolation=cv2.INTER_CUBIC)

    print('Resized Dimensions : ', resized_NN.shape)
    if print_pics:
        plt.figure(figsize=(16, 10))

        plt.subplot(2, 3, 1)
        plt.imshow(orig_img)
        # plt.imsave('orig.jpg', orig_img)
        plt.title('Original Image')
        plt.axis('off')

        plt.subplot(2, 3, 2)
        plt.imshow(downsampled_img)
        plt.title('Downsampled Image')
        plt.axis('off')

        plt.subplot(2, 3, 4)
        plt.imshow(resized_NN)
        # plt.imsave('NN.jpg', resized_NN)
        plt.title('Upsampled Image - Nearest Neighbour')
        plt.axis('off')

        plt.subplot(2, 3, 5)
        plt.imshow(resized_BL)
        # plt.imsave('BL.jpg', resized_BL)
        plt.title('Upsampled Image - Bilinear')
        plt.axis('off')

        plt.subplot(2, 3, 6)
        plt.imshow(resized_BC)
        # plt.imsave('BC.jpg', resized_BC)
        plt.title('Upsampled Image - Bicubic')
        plt.axis('off')

        plt.tight_layout()
        plt.savefig('interpolated_' + path[:-4] + '.png')
        plt.show()

    NN_ssim = structural_similarity(orig_img, resized_NN, multichannel=True)
    NN_psnr = peak_signal_noise_ratio(orig_img, resized_NN)

    BL_ssim = structural_similarity(orig_img, resized_BL, multichannel=True)
    BL_psnr = peak_signal_noise_ratio(orig_img, resized_BL)

    BC_ssim = structural_similarity(orig_img, resized_BC, multichannel=True)
    BC_psnr = peak_signal_noise_ratio(orig_img, resized_BC)

    return NN_ssim, NN_psnr, BL_ssim, BL_psnr, BC_ssim, BC_psnr
示例#12
0

proposed = clipCircle2(res[Delta:-Delta, Delta:-Delta])
padded_fbp = clipCircle2(rec_fbp_pad)
proposed_diff = clipCircle2((res - volint)[Delta:-Delta, Delta:-Delta])
padded_fbp_diff = clipCircle2(rec_fbp_pad - volint[Delta:-Delta, Delta:-Delta])


# Print metrics
# --------------

ref1 = clipCircle2(volint[Delta:-Delta, Delta:-Delta])
print("-"*79)
print("Results for %s  sigma = %f  spacing = %f  radius = %d  N2 = %d" % (TEST_CASE, SIGMA, SPACING, OMEGA_RADIUS, N2))
if __has_skimage__:
    ssim1 = structural_similarity(ref1.astype(np.float64), padded_fbp.astype(np.float64))
    ssim2 = structural_similarity(ref1.astype(np.float64), proposed.astype(np.float64))
    print("Padded FBP: \t PSNR = %.3f \t SSIM = %f" % (compare_psnr(ref1, padded_fbp), ssim1))
    print("Proposed: \t PSNR = %.3f \t SSIM = %f" % (compare_psnr(ref1, proposed), ssim2))
else:
    print("Padded FBP: \t PSNR = %.3f" % compare_psnr(ref1, padded_fbp))
    print("Proposed: \t PSNR = %.3f" % compare_psnr(ref1, proposed))
print("-"*79)







L = proposed.shape[0]
def ssim(im1, im2):
    return structural_similarity(im1, im2, dynamic_range=im1.max() - im1.min())
示例#14
0
def test_old_name_deprecated():
    from skimage.measure import structural_similarity
    with expected_warnings('deprecated'):
        ssim_result = structural_similarity(cam, cam_noisy, win_size=31)
def test_old_name_deprecated():
    from skimage.measure import structural_similarity
    with expected_warnings('deprecated'):
        ssim_result = structural_similarity(cam, cam_noisy, win_size=31)
def test_old_name_deprecated():
    from skimage.measure import structural_similarity
    with expected_warnings('Call to deprecated function '
                           '``structural_similarity``. Use '
                           '``compare_ssim`` instead.'):
        ssim_result = structural_similarity(cam, cam_noisy, win_size=31)
示例#17
0
mmap  = Xpost[:,idx[0]].reshape(250,250)
fig = pl.figure()
pl.subplot(121)
pl.imshow(mtrue, cmap="PuBu")
pl.axis("off")
pl.title("true reservoir")
pl.subplot(122)
pl.imshow(mmap, cmap="PuBu")
pl.axis("off")
pl.title("MAP estimate")
pl.show(); fig.savefig("MAP.pdf", bbox_inches="tight")

#-----------------------------------------------------------

try:
    import pandas as pd
    from skimage.measure import structural_similarity

    logger.info("Computing structural similarity statistics...")

    ssim = np.empty(25)
    for name, X in [("prior",Xprior),("posterior",Xpost)]:
        for i in xrange(25):
            ssim[i] = structural_similarity(mtrue, X[:,idx[i]].reshape(250,250), win_size=7)
        ssim = pd.Series(ssim)
        print "==> " + name + " SSIM statistics:", ssim.describe().to_string()
        print "interquartile range:", ssim.quantile(0.75) - ssim.quantile(0.25)
    print "SSIM index for MAP estimate:", structural_similarity(mtrue, mmap, win_size=7)
except ImportError:
    print "Consider installing scikit-image and pandas for SSIM statistics."
示例#18
0
# Load in best model
model_path = 'models/models_at_epoch{:04d}'.format(best_epoch) + '/gen'
with open(model_path + '/architecture.json') as json_file:
    json_config = json_file.read()
best_model = tf.keras.models.model_from_json(json_config)
best_model.load_weights(model_path + '/weights.h5')
print('Successfully loaded best model.')

# Compute SSIM and PSNR on test dataset
PSNR, SSIM = 0, 0
for lr_img, hr_img in zip(lr_test_data, hr_test_data):
    lr_img = np.expand_dims(lr_img, axis=0)
    sr_img = best_model(lr_img, training=False)
    sr_img = np.asarray(sr_img)

    SSIM += structural_similarity(hr_img, sr_img[0], multichannel=True)
    PSNR += peak_signal_noise_ratio(hr_img, sr_img[0])
SSIM /= len(hr_test_data)
PSNR /= len(hr_test_data)

print('Test set SSIM: {:.2f}'.format(SSIM))
print('Test set PSNR: {:.2f}'.format(PSNR))

# Save 10 sample images
if not os.path.exists('test_samples'):
    os.mkdir('test_samples')

for i in range(10):
    hr_img = random.choice(hr_test_data)
    lr_img = downSampleAll([hr_img], factor)
    sr_img = best_model(lr_img, training=False)
示例#19
0
def train_adversarial(train_data, val_data, batch_size, start_epoch, n_epochs,
                      factor):
    '''
    Train generator and discriminator for n_epochs with batch_size
    '''
    # Set aside sample image to track progress
    sample_image = random.choice(val_data)

    # create models directory if doesn't exist (to save models here)
    dirName = 'models'
    if not os.path.exists(dirName):
        os.mkdir(dirName)
        print("Directory ", dirName, " Created ")
    else:
        print("Directory ", dirName, " already exists")

    # create valdiation history dataframe
    validation_hist = pd.DataFrame(columns=['epoch', 'SSIM', 'PSNR'])

    # Train for n_epochs
    for epoch in range(start_epoch, n_epochs):
        losses_per_epoch = np.zeros(4)
        start = time()
        print('Starting epoch {}'.format(epoch + 1))

        n = 0
        for hr_images in train_data:
            lr_images = downSampleAll(hr_images.numpy(), factor)
            losses_per_epoch += train_step_adversarial(lr_images, hr_images)
            n += 1

        print('Epoch {} time: {:.2f}'.format(epoch + 1, time() - start))
        losses_per_epoch /= n

        if epoch == start_epoch:
            losses = np.asarray([losses_per_epoch])
        else:
            losses = np.vstack([losses, losses_per_epoch])

        # update loss history plot
        show_loss_history(losses,
                          ["gen_adv", "gen_content", "disc_real", "disc_fake"],
                          title='Training History')

        # Show progress update with sample image every 5 epochs
        if (epoch + 1) % 5 == 0:
            progress_update(gen, sample_image, epoch, factor)

        # Save models every 10 epochs and compute validation scores
        if (epoch + 1) % 10 == 0:
            # validation metrics
            PSNR, SSIM = 0, 0
            for hr_test_img in val_data:
                lr_test_img = downSampleAll([hr_test_img], factor)
                sr_img = gen(lr_test_img, training=False)

                sr_img = np.asarray(sr_img)

                SSIM += structural_similarity(hr_test_img,
                                              sr_img[0],
                                              multichannel=True)
                PSNR += peak_signal_noise_ratio(hr_test_img, sr_img[0])
            SSIM /= len(val_data)
            PSNR /= len(val_data)

            #validation_hist = validation_hist.append({'epoch':epoch+1, 'SSIM':SSIM, 'PSNR':PSNR}, ignore_index=True)
            validation_hist = validation_hist.append(
                {
                    'epoch': epoch + 1,
                    'SSIM': SSIM,
                    'PSNR': PSNR
                },
                ignore_index=True)

            # Save models
            dirName = 'models/models_at_epoch{:04d}'.format(epoch + 1)
            if not os.path.exists(dirName):
                os.mkdir(dirName)
            # save generator
            dirName = 'models/models_at_epoch{:04d}/gen'.format(epoch + 1)
            if not os.path.exists(dirName):
                os.mkdir(dirName)
            # Save JSON config to disk
            json_config = gen.to_json()
            with open(dirName + '/architecture.json', 'w') as json_file:
                json_file.write(json_config)
            # Save weights to disk
            gen.save_weights(dirName + '/weights.h5')
            print('Saved generator.')

            # save descriminator
            dirName = 'models/models_at_epoch{:04d}/disc'.format(epoch + 1)
            if not os.path.exists(dirName):
                os.mkdir(dirName)
            # Save JSON config to disk
            json_config = disc.to_json()
            with open(dirName + '/architecture.json', 'w') as json_file:
                json_file.write(json_config)
            # Save weights to disk
            disc.save_weights(dirName + '/weights.h5')
            print('Saved discriminator.')

    # Write validation history to csv
    validation_hist.to_csv('adv_validation_history.csv')
def test_old_name_deprecated():
    from skimage.measure import structural_similarity
    with expected_warnings('Call to deprecated function '
                           '``structural_similarity``. Use '
                           '``compare_ssim`` instead.'):
        ssim_result = structural_similarity(cam, cam_noisy, win_size=31)