[ Scikit-image ] 利用 SSIM篩選相似度過高的圖像
之前有提到過利用 Hamming distance對兩張照片進行相似度的比對, "利用PhotoHash篩選相似度過高的圖像"。
最近發現另一個相似度的方法,這個方法叫做 SSIM(Structural Similarity Index),有興趣的可以維基一下。
好在 Scikit-image裡已經提供它的算法,參數的部份可以參考這裡。
from skimage.measure import compare_ssim import numpy as np import cv2 import os import glob def filter(member): img_list = glob.glob('./{0}/*.png'.format(member)) img_list.sort(key=lambda num: int(num.split('/')[-1].split('.')[0])) for img in img_list: img_path = img next_img = cv2.imread(img_path,0) if 'img_target' in locals(): s = compare_ssim(img_target, next_img) if s >= 0.8: if not os.path.isdir("./{0}/similar".format(member)): os.system("mkdir ./{0}/similar".format(member)) print("make dir ./{0}/similar".format(member)) os.system("mv {0} ./{1}/similar".format(img_path, member)) print("move {0} to ./{1}/similar".format(img_path, member)) continue img_target = next_img names = ["Nayeon", "Jeongyeon", "Momo", "Sana", "Jihyo", "Mina", "Dahyun", "Chaeyoung", "Tzuyu"] for member in names: filter(member) print('Done!!')辨識的結構跟上一章沒有差太多,一樣是按照順序比較,只有改變辨識的方法。
先利用 OpenCV讀取圖像並轉為 Grayscale,透過 compare_ssim方法計算相似度。
compare_ssim計算出來的值會在 -1~1之間,數字越大相似度越高。






























盡量找了同一個 Frame開始連續十張,順便試了效果比較接近的相似值
其實兩種方式效果好像差不多,不過整體表現就要多嘗試了。
要比較全部的照片的話,只要稍微修改就可以了。
from skimage.measure import compare_ssim import numpy as np import cv2 import os import glob def filter(member): img_list = glob.glob('./{0}/*.png'.format(member)) while True: if img_list==[]: break img_target_path = img_list.pop() img_target = cv2.imread(img_target_path,0) for img_path in img_list: img = cv2.imread(img_path,0) s = compare_ssim(img_target, img) if s >= 0.8: if not os.path.isdir("./{0}/similar".format(member)): os.system("mkdir ./{0}/similar".format(member)) print("make dir ./{0}/similar".format(member)) os.system("mv {0} ./{1}/similar".format(img_path, member)) print("move {0} to ./{1}/similar".format(img_path, member)) img_list.remove(img_path) names = ["Nayeon", "Jeongyeon", "Momo", "Sana", "Jihyo", "Mina", "Dahyun", "Chaeyoung", "Tzuyu"] for member in names: filter(member) print('Done!!')
留言
張貼留言