Quick technique for comparing images better than MSE in Python
Asked Answered
V

1

7

I have been using Structural Similarity Index (through tensorflow) for comparing images, however it takes too long. I was wondering if there is an alternative technique that doesn't take so much time. It is also okay if someone could point out a more efficient implementation of SSIM than tensorflow in Python.

My intention for using SSIM, is that given a reference image (A) and a set of images (B), I need to understand which image in B is the most similar to the reference image A.

Vraisemblance answered 24/12, 2020 at 15:29 Comment(7)
Have you tried ssim from skimage? IDK, but is there any difference with tf's implementation?Allomerism
Have you tried this? https://mcmap.net/q/889481/-fast-and-efficient-way-to-detect-if-two-images-are-visually-identical-in-pythonAllomerism
Thank you but I am not looking to check if they are exactly identical. I'd like to compare them using a certain metric that outputs a score.Vraisemblance
we don't know how you want the images compared. what are you going to use this score matrix for?Certified
@Certified Thank you for the comment. I have made the changes. Please let me know if you need more information.Vraisemblance
Have you looked at aHash, which is one of the hashing algorithms in ImageHash? I can compare 100 images in approx. 0.29 of a second. When I tried the same test using skimage it's about 1.2 seconds.Rhetorician
I have not looked into that, but I will soon.Vraisemblance
R
6

UPDATE 01-02-2021

I decided to explore some other Python modules that could be used for Image comparison. I also wanted to use concurrent.futures, which I hadn't used before.

I created two GitGub Gists with the code that I wrote.

skimage ssim image comparison

ImageHash aHash image comparison

The ImageHash module was able to compare 100 images in 0.29 of a second and the skimage module took 1.2 seconds for the same task.


ORIGINAL POST

I haven't tested the speed of the code in this answer, because I have only used the code in some image testing that I posted to GitHub:

facial similarities

facial prediction

The code below will produce a similarity score between reference image (A) and set of images (B).

The complete code is located in my GitHub repository

import os
from os import walk
import numpy as np
from PIL import Image
from math import *


def get_image_files(directory_of_images):
    """
     This function is designed to traverse a directory tree and extract all
     the image names contained in the directory.
    :param directory_of_images: the name of the target directory containing
           the images to be trained on.
    :return: list of images to be processed.
    """
    images_to_process = []
    for (dirpath, dirnames, filenames) in walk(directory_of_images):
        for filename in filenames:
            accepted_extensions = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.svg', '.tiff')
            if filename.endswith(accepted_extensions):
                images_to_process.append(os.path.join(dirpath, filename))
        return images_to_process


def pre_process_images(image_one, image_two, additional_resize=False, max_image_size=1000):
    """
     This function is designed to resize the images using the Pillow module.
    :param image_one: primary image to evaluate against a secondary image
    :param image_two: secondary image to evaluate against the primary image
    :param additional_resize:
    :param max_image_size: maximum allowable image size in pixels
    :return: resized images
    """
    lower_boundary_size = (min(image_one.size[0], image_two.size[0]), min(image_one.size[1], image_two.size[1]))
    # reference: https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize
    # reference: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.LANCZOS
    image_one = image_one.resize(lower_boundary_size, resample=Image.LANCZOS)
    image_two = image_two.resize(lower_boundary_size, resample=Image.LANCZOS)
    if max(image_one.size) > max_image_size and additional_resize:
        resize_factor = max_image_size / max(image_one.size)
        image_one = image_one.resize((int(lower_boundary_size[0] * resize_factor),
                                      int(lower_boundary_size[1] * resize_factor)), resample=Image.LANCZOS)

        image_two = image_two.resize((int(lower_boundary_size[0] * resize_factor),
                                      int(lower_boundary_size[1] * resize_factor)), resample=Image.LANCZOS)
    return image_one, image_two


def get_ssim_similarity(image_one_name, image_two_name, window_size=7, dynamic_range=255):
    """
    The Structural Similarity Index (SSIM) is a method for measuring the similarity between two images.
    The SSIM index can be viewed as a quality measure of one of the images being compared, provided the
    other image is regarded as of perfect quality.
    :param image_one_name: primary image to evaluate against a secondary image
    :param image_two_name: secondary image to evaluate against the primary image
    :param window_size: The side-length of the sliding window used in comparison. Must be an odd value.
    :param dynamic_range: Dynamic range of the input image, specified as a positive scalar.
    The default dynamic range is 255 for images of data type uint8.
    :return: computational score and image names
    """
    image_one = Image.open(image_one_name)
    image_two = Image.open(image_two_name)

    if min(list(image_one.size) + list(image_two.size)) < 7:
        raise Exception("One of the images was too small to process using the SSIM approach")
    image_one, image_two = pre_process_images(image_one, image_two, True)
    image_one, image_two = image_one.convert('I'), image_two.convert('I')
    c1 = (dynamic_range * 0.01) ** 2
    c2 = (dynamic_range * 0.03) ** 2
    pixel_length = window_size ** 2
    ssim = 0.0
    adjusted_width = image_one.size[0] // window_size * window_size
    adjusted_height = image_one.size[1] // window_size * window_size
    for i in range(0, adjusted_width, window_size):
        for j in range(0, adjusted_height, window_size):
            crop_box = (i, j, i + window_size, j + window_size)
            crop_box_one = image_one.crop(crop_box)
            crop_box_two = image_two.crop(crop_box)
            np_array_one, np_array_two = np.array(crop_box_one).flatten(), np.array(crop_box_two).flatten()
            np_variable_one, np_variable_two = np.var(np_array_one), np.var(np_array_two)
            np_average_one, np_average_two = np.average(np_array_one), np.average(np_array_two)
            cov = (np.sum(np_array_one * np_array_two) - (np.sum(np_array_one) *
                                                          np.sum(crop_box_two) / pixel_length)) / pixel_length
            ssim += ((2.0 * np_average_one * np_average_two + c1) * (2.0 * cov + c2)) / \
                    ((np_average_one ** 2 + np_average_two ** 2 + c1) * (np_variable_one + np_variable_two + c2))
    similarity_percent = (ssim * pixel_length / (adjusted_height * adjusted_width)) * 100
    return round(similarity_percent, 2)


target_image = 'a.jpg'
image_directory = 'b_images'

images = get_image_files(image_directory)

for image in images:
    ssim_result = get_ssim_similarity(target_image, image)

I would also recommend looking at the Python module ImageHash. I have multiple code examples and test cases published here.

Rhetorician answered 28/12, 2020 at 21:28 Comment(4)
Thank you. Could you please post a working example of this. I tried running the code but I am encountering errors -Vraisemblance
import numpy as np; import tensorflow as tf; (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data( path='mnist.npz' ); a,b = pre_process_images(x_train[1], x_train[2] , additional_resize=False, max_image_size=1000); TypeError: 'int' object is not subscriptableVraisemblance
I added all the code for this example. All my code is in script numpy_math_module_image_comparison in the repository that I mentioned.Rhetorician
@Vraisemblance my code does not use tensorflow, so the error "TypeError: 'int' object is not subscriptable" is not from my code example.Rhetorician

© 2022 - 2024 — McMap. All rights reserved.