圖片 2 (b1.jpg):

這些影像的哈希分數之間的差異是:24
當像素化(50x50 像素)時,它們看起來像這樣:

rs_a1.jpg

rs_b1.jpg
像素影像的哈希差異分數更大!: 26
下面是@ann zen 要求的另外兩個近乎重復的影像對示例:
對 1

對 2

我用來減小影像大小的代碼是這樣的:
from PIL import Image
with Image.open(image_path) as image:
reduced_image = image.resize((50, 50)).convert('RGB').convert("1")
以及比較兩個影像哈希的代碼:
from PIL import Image
import imagehash
with Image.open(image1_path) as img1:
hashing1 = imagehash.phash(img1)
with Image.open(image2_path) as img2:
hashing2 = imagehash.phash(img2)
print('difference : ', hashing1-hashing2)
uj5u.com熱心網友回復:
基于像素的方法過于簡單,因為您談論的是物件相似性,而不是像素相似性。您需要深度學習特征值,而不是像素值。
我收集了一

img1_2.jpg& img2_2.jpg:

img1_3.jpg& img2_3.jpg:

為了證明模糊不會產生真正的誤報,我運行了這個程式:
import cv2
import numpy as np
def process(img):
h, w, _ = img.shape
img = cv2.resize(img, (350, h * w // 350))
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return cv2.GaussianBlur(img_gray, (43, 43), 21)
def confidence(img1, img2):
res = cv2.matchTemplate(process(img1), process(img2), cv2.TM_CCOEFF_NORMED)
return res.max()
img1s = list(map(cv2.imread, ["img1_1.jpg", "img1_2.jpg", "img1_3.jpg"]))
img2s = list(map(cv2.imread, ["img2_1.jpg", "img2_2.jpg", "img2_3.jpg"]))
for i, img1 in enumerate(img1s, 1):
for j, img2 in enumerate(img2s, 1):
conf = confidence(img1, img2)
print(f"img1_{i} img2_{j} Confidence: {round(conf * 100, 2)}%")
輸出:
img1_1 img2_1 Confidence: 84.2% # Corresponding images
img1_1 img2_2 Confidence: -10.86%
img1_1 img2_3 Confidence: 16.11%
img1_2 img2_1 Confidence: -2.5%
img1_2 img2_2 Confidence: 84.61% # Corresponding images
img1_2 img2_3 Confidence: 43.91%
img1_3 img2_1 Confidence: 14.49%
img1_3 img2_2 Confidence: 59.15%
img1_3 img2_3 Confidence: 87.25% # Corresponding images
請注意,只有在將影像與其對應的影像匹配時,程式才會輸出高置信度 (84 %)。
uj5u.com熱心網友回復:
這是一種使用該庫定量確定重復和接近重復影像的方法,該
該資料集有 5 張影像,請注意 cat #1 有重復,而其他則不同。
查找重復影像
Score: 100.000%
.\cat1 copy.jpg
.\cat1.jpg
cat1 和它的副本都是一樣的。
查找近乎重復的影像
Score: 91.116%
.\cat1 copy.jpg
.\cat2.jpg
Score: 91.116%
.\cat1.jpg
.\cat2.jpg
Score: 91.097%
.\bear1.jpg
.\bear2.jpg
Score: 59.086%
.\bear2.jpg
.\cat2.jpg
Score: 56.025%
.\bear1.jpg
.\cat2.jpg
Score: 53.659%
.\bear1.jpg
.\cat1 copy.jpg
Score: 53.659%
.\bear1.jpg
.\cat1.jpg
Score: 53.225%
.\bear2.jpg
.\cat1.jpg
We get more interesting results with the score comparison between each image. Using a threshold of 0.9 or 90%, we can filter out near-duplicate images.
Comparison between just two images
Score: 91.097%
.\bear1.jpg
.\bear2.jpg
Score: 91.116%
.\cat1.jpg
.\cat2.jpg
Score: 93.715%
.\tower1.jpg
.\tower2.jpg
Code
from sentence_transformers import SentenceTransformer, util
from PIL import Image
import glob
import os
# Load the OpenAI CLIP Model
print('Loading CLIP Model...')
model = SentenceTransformer('clip-ViT-B-32')
# Next we compute the embeddings
# To encode an image, you can use the following code:
# from PIL import Image
# encoded_image = model.encode(Image.open(filepath))
image_names = list(glob.glob('./*.jpg'))
print("Images:", len(image_names))
encoded_image = model.encode([Image.open(filepath) for filepath in image_names], batch_size=128, convert_to_tensor=True, show_progress_bar=True)
# Now we run the clustering algorithm. This function compares images aganist
# all other images and returns a list with the pairs that have the highest
# cosine similarity score
processed_images = util.paraphrase_mining_embeddings(encoded_image)
NUM_SIMILAR_IMAGES = 10
# =================
# DUPLICATES
# =================
print('Finding duplicate images...')
# Filter list for duplicates. Results are triplets (score, image_id1, image_id2) and is scorted in decreasing order
# A duplicate image will have a score of 1.00
duplicates = [image for image in processed_images if image[0] >= 1]
# Output the top X duplicate images
for score, image_id1, image_id2 in duplicates[0:NUM_SIMILAR_IMAGES]:
print("\nScore: {:.3f}%".format(score * 100))
print(image_names[image_id1])
print(image_names[image_id2])
# =================
# NEAR DUPLICATES
# =================
print('Finding near duplicate images...')
# Use a threshold parameter to identify two images as similar. By setting the threshold lower,
# you will get larger clusters which have less similar images in it. Threshold 0 - 1.00
# A threshold of 1.00 means the two images are exactly the same. Since we are finding near
# duplicate images, we can set it at 0.99 or any number 0 < X < 1.00.
threshold = 0.99
near_duplicates = [image for image in processed_images if image[0] < threshold]
for score, image_id1, image_id2 in near_duplicates[0:NUM_SIMILAR_IMAGES]:
print("\nScore: {:.3f}%".format(score * 100))
print(image_names[image_id1])
print(image_names[image_id2])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446431.html
標籤:Python 算法 opencv 图像处理 计算机视觉
上一篇:如何計算有效分支因子?
