简体   繁体   English

使用 open cv 检查不同日期拍摄的两幅植物图像的相似性

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

In the garden, capturing the location with the different dates... I need to approve it once the 2 images are from the same garden.. How to find the similarity of the image...在花园里,捕捉不同日期的位置......一旦两张图片来自同一个花园,我需要批准它......如何找到图像的相似度......

I tried subtracting the 2 images through cv2.. but was not satisfied with the process... any guidance?我尝试通过 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()

I tried with imagehash to compare 2 images, it works fine also looking for better solutions.. suggest the same..我尝试使用 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM