python進(jìn)行圖片相似度對(duì)比的兩種實(shí)現(xiàn)方法
Python提供了一些庫(kù)和工具可以用于圖片的相似度比對(duì)。下面介紹兩種常用的方法:
1、感知哈希(Perceptual Hashing)
這種方法通過計(jì)算圖像的哈希值來表示圖像的特征,從而進(jìn)行相似度比對(duì)。
常用庫(kù):imagehash 和 phash
具體代碼如下:
from PIL import Image
import imagehash
# 生成圖像的感知哈希
hash1 = imagehash.average_hash(Image.open('image1.jpg'))
hash2 = imagehash.average_hash(Image.open('image2.jpg'))
# 計(jì)算相似度
similarity = 1 - (hash1 - hash2) / len(hash1.hash) # 范圍為0到1,值越大表示相似度越高
print(similarity)2、結(jié)構(gòu)相似性(Structural Similarity)
這種方法通過比較圖像的結(jié)構(gòu)、紋理和亮度等特征來衡量相似度。
常用庫(kù):scikit-image
具體代碼如下:
from PIL import Image
from skimage import metrics
from skimage.transform import resize
# 打開并調(diào)整圖像大小
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image1 = image1.resize((500, 500)) # 調(diào)整圖像1的大小為500x500
image2 = image2.resize((500, 500)) # 調(diào)整圖像2的大小為500x500
# 將圖像轉(zhuǎn)換為灰度圖像
image1_gray = image1.convert("L")
image2_gray = image2.convert("L")
# 將圖像轉(zhuǎn)換為NumPy數(shù)組
image1_array = np.array(image1_gray)
image2_array = np.array(image2_gray)
# 計(jì)算結(jié)構(gòu)相似性指數(shù)(SSIM)
similarity = metrics.structural_similarity(image1_array, image2_array)
# 將相似性指數(shù)轉(zhuǎn)換為相似度(范圍0到1,值越大表示相似度越高)
similarity = (similarity + 1) / 2
print(similarity)到此這篇關(guān)于python進(jìn)行圖片相似度對(duì)比的兩種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)python 圖片相似度對(duì)比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3簡(jiǎn)單實(shí)例計(jì)算同花的概率代碼
這篇文章主要介紹了Python3簡(jiǎn)單實(shí)例計(jì)算同花的概率代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12
Python常見數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列用法示例
這篇文章主要介紹了Python常見數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列用法,結(jié)合實(shí)例形式簡(jiǎn)單介紹了數(shù)據(jù)結(jié)構(gòu)中棧與隊(duì)列的概念、功能及簡(jiǎn)單使用技巧,需要的朋友可以參考下2019-01-01
使用Python對(duì)零售商品進(jìn)行數(shù)據(jù)分析
Python調(diào)用requests庫(kù)實(shí)現(xiàn)自動(dòng)化發(fā)牌功能
Python 時(shí)間操作例子和時(shí)間格式化參數(shù)小結(jié)
python里的條件語(yǔ)句和循環(huán)語(yǔ)句你了解多少
Python使用Crypto庫(kù)實(shí)現(xiàn)加密解密的示例詳解
python實(shí)現(xiàn)基本進(jìn)制轉(zhuǎn)換的方法

