a-cv2-calculate-simlilarity


Namea-cv2-calculate-simlilarity JSON
Version 0.11 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/a_cv2_calculate_simlilarity
SummaryCalculate the simlilarity of 2 or more pictures with OpenCV
upload_time2023-09-20 06:00:13
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords opencv simlilarity cv2
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Calculate the simlilarity of 2 or more pictures with OpenCV

## Tested against Windows 10 / Python 3.11 / Anaconda

### pip install a-cv2-calculate_simlilarity


```python

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,
        )
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/a_cv2_calculate_simlilarity",
    "name": "a-cv2-calculate-simlilarity",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "opencv,simlilarity,cv2",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/2d/75/17c6fb49e86306c7b11b9054f49816e8e96fe25c1a98b08ae4974e24b98d/a_cv2_calculate_simlilarity-0.11.tar.gz",
    "platform": null,
    "description": "\r\n# Calculate the simlilarity of 2 or more pictures with OpenCV\r\n\r\n## Tested against Windows 10 / Python 3.11 / Anaconda\r\n\r\n### pip install a-cv2-calculate_simlilarity\r\n\r\n\r\n```python\r\n\r\nfrom a_cv2_calculate_simlilarity import add_similarity_to_cv2\r\nadd_similarity_to_cv2() #monkeypatch\r\n\r\n#if you don't want to use a monkey patch:\r\n#from a_cv2_calculate_simlilarity import calculate_simlilarity\r\n\r\n\r\ncalculate_simlilarity(\r\n    im1: Any,\r\n    im2: Any,\r\n    width=100,\r\n    height=100,\r\n    interpolation=cv2.INTER_LINEAR,\r\n    with_alpha=False,\r\n) -> tuple:\r\n    r\"\"\"\r\n    Calculate structural similarity between two images.\r\n\r\n    This function computes the structural similarity index (SSIM) between two images,\r\n    which measures the similarity of their structural patterns. The SSIM values range\r\n    from -1 to 1, where a higher value indicates greater similarity.\r\n\r\n    Parameters:\r\n        im1: Any\r\n            Image 1, which can be provided as a URL, file path, base64 string, numpy array,\r\n            or PIL image.\r\n        im2: Any\r\n            Image 2, which can be provided as a URL, file path, base64 string, numpy array,\r\n            or PIL image.\r\n        width: int, optional\r\n            Width of the resized images for comparison (default is 100).\r\n        height: int, optional\r\n            Height of the resized images for comparison (default is 100).\r\n        interpolation: int, optional\r\n            Interpolation method for resizing (default is cv2.INTER_LINEAR).\r\n        with_alpha: bool, optional\r\n            Whether to include alpha channel if present (default is False).\r\n\r\n    Returns:\r\n        tuple\r\n            A tuple containing four SSIM values in the order (B, G, R, Alpha).\r\n\r\n    Example:\r\n        resa = calculate_simlilarity(\r\n            r\"https://avatars.githubusercontent.com/u/77182807?v=4\",\r\n            r\"https://avatars.githubusercontent.com/u/77182807?v=4\",\r\n            width=100,\r\n            height=100,\r\n            interpolation=cv2.INTER_LINEAR,\r\n            with_alpha=False,\r\n        )\r\n        print(resa)\r\n        resa2 = calculate_simlilarity(\r\n            r\"https://avatars.githubusercontent.com/u/77182807?v=4\",\r\n            r\"https://avatars.githubusercontent.com/u/1024025?v=4\",\r\n            width=100,\r\n            height=100,\r\n            interpolation=cv2.INTER_LINEAR,\r\n            with_alpha=False,\r\n        )\r\n        print(resa2)\r\n\r\n        resa2 = calculate_simlilarity(\r\n            r\"C:\\Users\\hansc\\Downloads\\1633513733_526_Roblox-Royale-High.jpg\",\r\n            r\"C:\\Users\\hansc\\Downloads\\Roblox-Royale-High-Bobbing-For-Apples (1).jpg\",\r\n            width=100,\r\n            height=100,\r\n            interpolation=cv2.INTER_LINEAR,\r\n            with_alpha=False,\r\n        )\r\n        print(resa2)\r\n\r\n        resa2 = calculate_simlilarity(\r\n            r\"C:\\Users\\hansc\\Documents\\test1.png\",\r\n            r\"C:\\Users\\hansc\\Documents\\test2.png\",\r\n            width=100,\r\n            height=100,\r\n            interpolation=cv2.INTER_LINEAR,\r\n            with_alpha=False,\r\n        )\r\n        print(resa2)\r\n        \r\n        \r\ncompare_all_images_with_all_images(\r\n    imagelist,\r\n    width=100,\r\n    height=100,\r\n    interpolation=cv2.INTER_LINEAR,\r\n    with_alpha=False,\r\n    delete_cache=True,\r\n):\r\n    r\"\"\"\r\n    Compare a list of images with each other and return a similarity matrix.\r\n\r\n    This function compares a list of images with each other using the `calculate_simlilarity`\r\n    function and returns a similarity matrix as a pandas DataFrame. Each element in the matrix\r\n    represents the similarity between two images.\r\n\r\n    Parameters:\r\n        imagelist: list\r\n            List of images to compare. Each image can be provided as a URL, file path, base64 string,\r\n            numpy array, or PIL image.\r\n        width: int, optional\r\n            Width of the resized images for comparison (default is 100).\r\n        height: int, optional\r\n            Height of the resized images for comparison (default is 100).\r\n        interpolation: int, optional\r\n            Interpolation method for resizing (default is cv2.INTER_LINEAR).\r\n        with_alpha: bool, optional\r\n            Whether to include alpha channel if present (default is False).\r\n        delete_cache: bool, optional\r\n            Whether to clear the cache of preprocessed images (default is True).\r\n\r\n    Returns:\r\n        pandas.DataFrame\r\n            A DataFrame representing the similarity matrix between the images.\r\n\r\n    Example:\r\n        add_similarity_to_cv2()\r\n        df = cv2.calculate_simlilarity_of_all_pics(\r\n            [\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7191107_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7191107_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7213836_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7213836_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7253843_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7253843_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7274426_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7274426_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7286225_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7286225_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7301136_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7301136_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7312635_0.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7312635_1.png\",\r\n                r\"C:\\Users\\hansc\\Downloads\\testcompare\\10462.7325586_0.png\",\r\n            ],\r\n            width=100,\r\n            height=100,\r\n            interpolation=cv2.INTER_LINEAR,\r\n            with_alpha=False,\r\n        )\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Calculate the simlilarity of 2 or more pictures with OpenCV",
    "version": "0.11",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/a_cv2_calculate_simlilarity"
    },
    "split_keywords": [
        "opencv",
        "simlilarity",
        "cv2"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3222bf4b11ac7027894810eb18ab9a357942a322f46ccdc123b0025a4ba047ed",
                "md5": "0b72250b321d896af6a7d4cc7e3d839d",
                "sha256": "b167c099eea38db30dc1c0329fc65bfa14a0829e79507a1e1acb0041b13d1740"
            },
            "downloads": -1,
            "filename": "a_cv2_calculate_simlilarity-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0b72250b321d896af6a7d4cc7e3d839d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 59719,
            "upload_time": "2023-09-20T06:00:11",
            "upload_time_iso_8601": "2023-09-20T06:00:11.386973Z",
            "url": "https://files.pythonhosted.org/packages/32/22/bf4b11ac7027894810eb18ab9a357942a322f46ccdc123b0025a4ba047ed/a_cv2_calculate_simlilarity-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2d7517c6fb49e86306c7b11b9054f49816e8e96fe25c1a98b08ae4974e24b98d",
                "md5": "0c078afc214cff906da678a74d82552a",
                "sha256": "e48705bf83af2c56d95e31bd2619fcd4ff2e5c4edfb2986daede2d6079d955a8"
            },
            "downloads": -1,
            "filename": "a_cv2_calculate_simlilarity-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "0c078afc214cff906da678a74d82552a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 58458,
            "upload_time": "2023-09-20T06:00:13",
            "upload_time_iso_8601": "2023-09-20T06:00:13.559500Z",
            "url": "https://files.pythonhosted.org/packages/2d/75/17c6fb49e86306c7b11b9054f49816e8e96fe25c1a98b08ae4974e24b98d/a_cv2_calculate_simlilarity-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-20 06:00:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "a_cv2_calculate_simlilarity",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "a-cv2-calculate-simlilarity"
}
        
Elapsed time: 0.11405s