Package: a-cv2-calculate-simlilarity
Calculate the simlilarity of 2 or more pictures with OpenCVTested against Windows 10 / Python 3.11 / Anacondapip install a-cv2-calculate_simlilarityfroma_cv2_calculate_simlilarityimportadd_similarity_to_cv2add_similarity_to_cv2()#monkeypatch#if you don't want to use a monkey patch:#from a_cv2_calculate_simlilarity import calculate_simlilaritycalculate_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 rangefrom -1 to 1, where a higher value indicates greater similarity.Parameters:im1: AnyImage 1, which can be provided as a URL, file path, base64 string, numpy array,or PIL image.im2: AnyImage 2, which can be provided as a URL, file path, base64 string, numpy array,or PIL image.width: int, optionalWidth of the resized images for comparison (default is 100).height: int, optionalHeight of the resized images for comparison (default is 100).interpolation: int, optionalInterpolation method for resizing (default is cv2.INTER_LINEAR).with_alpha: bool, optionalWhether to include alpha channel if present (default is False).Returns:tupleA 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"""Comparealistofimageswitheachotherandreturnasimilaritymatrix.Thisfunctioncomparesalistofimageswitheachotherusingthe`calculate_simlilarity`functionandreturnsasimilaritymatrixasapandasDataFrame.Eachelementinthematrixrepresentsthesimilaritybetweentwoimages.Parameters:imagelist:listListofimagestocompare.EachimagecanbeprovidedasaURL,filepath,base64string,numpyarray,orPILimage.width:int,optionalWidthoftheresizedimagesforcomparison(defaultis100).height:int,optionalHeightoftheresizedimagesforcomparison(defaultis100).interpolation:int,optionalInterpolationmethodforresizing(defaultiscv2.INTER_LINEAR).with_alpha:bool,optionalWhethertoincludealphachannelifpresent(defaultisFalse).delete_cache:bool,optionalWhethertoclearthecacheofpreprocessedimages(defaultisTrue).Returns:pandas.DataFrameADataFramerepresentingthesimilaritymatrixbetweentheimages.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,)