使用 open cv 检查在不同日期捕获的两个植物图像的相似性

Checking the similarity of two plant images captured on different date with open cv

在花园里,拍摄不同日期的位置...如果2张图片来自同一个花园,我需要批准..如何找到图像的相似度...

我尝试通过 cv2.. 减去 2 个图像,但对过程不满意...有任何指导吗?

import cv2
import numpy as np

original = cv2.imread("initial.jpg")
duplicate = cv2.imread("Day10.jpg")

# 1) Check if 2 images are equals
if original.shape == duplicate.shape:
    print("The images have same size and channels")
    difference = cv2.subtract(original, duplicate)
    b, g, r = cv2.split(difference)


    if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
        print("The images are completely Equal")
        
cv2.imshow("Original", original)
cv2.imshow("Duplicate", duplicate)
cv2.waitKey(0)
cv2.destroyAllWindows()

我尝试使用 imagehash 来比较 2 个图像,它工作正常,也在寻找更好的解决方案.. 建议相同..

import os
from PIL import Image
from PIL import ImageFile
import imagehash

#add missing libraries..

def get_hash_dict(dir):
    hash_dict = {}
    image_quantity = 0
    for _, _, files in os.walk(dir):
        for i, fileName in enumerate(files):
            with open(dir + fileName, 'rb') as fp:
                hash_dict[dir + fileName] = imagehash.average_hash(Image.open(fp))
                image_quantity += 1

    return hash_dict, image_quantity

def compare_image_with_hash(image_file_name_1, image_file_name_2, max_dif=0):

    ImageFile.LOAD_TRUNCATED_IMAGES = True
    hash_1 = None
    hash_2 = None
    with open(image_file_name_1, 'rb') as fp:
        hash_1 = imagehash.average_hash(Image.open(fp))
    with open(image_file_name_2, 'rb') as fp:
        hash_2 = imagehash.average_hash(Image.open(fp))
    dif = hash_1 - hash_2
    if dif < 0:
        dif = -dif
    if dif <= max_dif:
        # return True
        return ("Approved")
    else:
        return("Not Approved")

def compare_image_dir_with_hash(dir_1, dir_2, max_dif=0):

    ImageFile.LOAD_TRUNCATED_IMAGES = True
    hash_dict_1, image_quantity_1 = get_hash_dict(dir_1)
    hash_dict_2, image_quantity_2 = get_hash_dict(dir_2)

    if image_quantity_1 > image_quantity_2:
        tmp = image_quantity_1
        image_quantity_1 = image_quantity_2
        image_quantity_2 = tmp

        tmp = hash_dict_1
        hash_dict_1 = hash_dict_2
        hash_dict_2 = tmp

    result_dict = {}

    for k in hash_dict_1.keys():
        result_dict[k] = None

    for dif_i in range(0, max_dif + 1):
        have_none = False

        for k_1 in result_dict.keys():
            if result_dict.get(k_1) is None:
                have_none = True

        if not have_none:
            return result_dict

        for k_1, v_1 in hash_dict_1.items():
            for k_2, v_2 in hash_dict_2.items():
                sub = (v_1 - v_2)
                if sub < 0:
                    sub = -sub
                if sub == dif_i and result_dict.get(k_1) is None:
                    result_dict[k_1] = k_2
                    break
    return result_dict


image1 = "img1.jpeg"
image2 = "img1.jpeg"
if image1 == image2:
    print("Both the images are same.. Checking with other images")
    break
else:
    relevantResult = (compare_image_with_hash(image1, image2, 5))
    # print(relevantResult)
    r = compare_image_dir_with_hash(image1, image2, 10)
    for k in r.keys():
        print(k, r.get(k))
    if relevantResult == "Approved":
        print("Approved")
        
        break
    else:
        print("Not Approved")