Skip to main content

Calculate the simlilarity of 2 or more pictures with OpenCV

Project description

Calculate the simlilarity of 2 or more pictures with OpenCV

Tested against Windows 10 / Python 3.11 / Anaconda

pip install a-cv2-calculate_simlilarity

from a_cv2_calculate_simlilarity import add_similarity_to_cv2
add_similarity_to_cv2() #monkeypatch

#if you don't want to use a monkey patch:
#from a_cv2_calculate_simlilarity import calculate_simlilarity


calculate_simlilarity(
    im1: Any,
    im2: Any,
    width=100,
    height=100,
    interpolation=cv2.INTER_LINEAR,
    with_alpha=False,
) -> tuple:
    r"""
    Calculate structural similarity between two images.

    This function computes the structural similarity index (SSIM) between two images,
    which measures the similarity of their structural patterns. The SSIM values range
    from -1 to 1, where a higher value indicates greater similarity.

    Parameters:
        im1: Any
            Image 1, which can be provided as a URL, file path, base64 string, numpy array,
            or PIL image.
        im2: Any
            Image 2, which can be provided as a URL, file path, base64 string, numpy array,
            or PIL image.
        width: int, optional
            Width of the resized images for comparison (default is 100).
        height: int, optional
            Height of the resized images for comparison (default is 100).
        interpolation: int, optional
            Interpolation method for resizing (default is cv2.INTER_LINEAR).
        with_alpha: bool, optional
            Whether to include alpha channel if present (default is False).

    Returns:
        tuple
            A tuple containing four SSIM values in the order (B, G, R, Alpha).

    Example:
        resa = calculate_simlilarity(
            r"https://avatars.githubusercontent.com/u/77182807?v=4",
            r"https://avatars.githubusercontent.com/u/77182807?v=4",
            width=100,
            height=100,
            interpolation=cv2.INTER_LINEAR,
            with_alpha=False,
        )
        print(resa)
        resa2 = calculate_simlilarity(
            r"https://avatars.githubusercontent.com/u/77182807?v=4",
            r"https://avatars.githubusercontent.com/u/1024025?v=4",
            width=100,
            height=100,
            interpolation=cv2.INTER_LINEAR,
            with_alpha=False,
        )
        print(resa2)

        resa2 = calculate_simlilarity(
            r"C:\Users\hansc\Downloads\1633513733_526_Roblox-Royale-High.jpg",
            r"C:\Users\hansc\Downloads\Roblox-Royale-High-Bobbing-For-Apples (1).jpg",
            width=100,
            height=100,
            interpolation=cv2.INTER_LINEAR,
            with_alpha=False,
        )
        print(resa2)

        resa2 = calculate_simlilarity(
            r"C:\Users\hansc\Documents\test1.png",
            r"C:\Users\hansc\Documents\test2.png",
            width=100,
            height=100,
            interpolation=cv2.INTER_LINEAR,
            with_alpha=False,
        )
        print(resa2)
        
        
compare_all_images_with_all_images(
    imagelist,
    width=100,
    height=100,
    interpolation=cv2.INTER_LINEAR,
    with_alpha=False,
    delete_cache=True,
):
    r"""
    Compare a list of images with each other and return a similarity matrix.

    This function compares a list of images with each other using the `calculate_simlilarity`
    function and returns a similarity matrix as a pandas DataFrame. Each element in the matrix
    represents the similarity between two images.

    Parameters:
        imagelist: list
            List of images to compare. Each image can be provided as a URL, file path, base64 string,
            numpy array, or PIL image.
        width: int, optional
            Width of the resized images for comparison (default is 100).
        height: int, optional
            Height of the resized images for comparison (default is 100).
        interpolation: int, optional
            Interpolation method for resizing (default is cv2.INTER_LINEAR).
        with_alpha: bool, optional
            Whether to include alpha channel if present (default is False).
        delete_cache: bool, optional
            Whether to clear the cache of preprocessed images (default is True).

    Returns:
        pandas.DataFrame
            A DataFrame representing the similarity matrix between the images.

    Example:
        add_similarity_to_cv2()
        df = cv2.calculate_simlilarity_of_all_pics(
            [
                r"C:\Users\hansc\Downloads\testcompare\10462.7191107_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7191107_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7213836_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7213836_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7253843_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7253843_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7274426_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7274426_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7286225_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7286225_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7301136_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7301136_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7312635_0.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7312635_1.png",
                r"C:\Users\hansc\Downloads\testcompare\10462.7325586_0.png",
            ],
            width=100,
            height=100,
            interpolation=cv2.INTER_LINEAR,
            with_alpha=False,
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a_cv2_calculate_simlilarity-0.11.tar.gz (58.5 kB view details)

Uploaded Source

Built Distribution

a_cv2_calculate_simlilarity-0.11-py3-none-any.whl (59.7 kB view details)

Uploaded Python 3

File details

Details for the file a_cv2_calculate_simlilarity-0.11.tar.gz.

File metadata

File hashes

Hashes for a_cv2_calculate_simlilarity-0.11.tar.gz
Algorithm Hash digest
SHA256 e48705bf83af2c56d95e31bd2619fcd4ff2e5c4edfb2986daede2d6079d955a8
MD5 0c078afc214cff906da678a74d82552a
BLAKE2b-256 2d7517c6fb49e86306c7b11b9054f49816e8e96fe25c1a98b08ae4974e24b98d

See more details on using hashes here.

File details

Details for the file a_cv2_calculate_simlilarity-0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for a_cv2_calculate_simlilarity-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 b167c099eea38db30dc1c0329fc65bfa14a0829e79507a1e1acb0041b13d1740
MD5 0b72250b321d896af6a7d4cc7e3d839d
BLAKE2b-256 3222bf4b11ac7027894810eb18ab9a357942a322f46ccdc123b0025a4ba047ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page