简体   繁体   English

如何检查具有不同像素化的两张图像的相似性

[英]How to check similarity of two images that have different pixelization

I am running a python code to check similarity of Quora and Twitter users profiles photos, but i am not getting a positive result when images are the same.我正在运行一个 python 代码来检查 Quora 和 Twitter 用户个人资料照片的相似性,但是当图像相同时我没有得到积极的结果。

This is the code for comparing the two images :这是比较两个图像的代码:

path_photo_quora= "/home/yousuf/Desktop/quora_photo.jpg"
path_photo_twitter="/home/yousuf/Desktop/twitter_photo.jpeg"
if open(path_photo_quora,"rb").read() == open(path_photo_twitter,"rb").read():
     print('photos profile are identical')

despite images are the same, the console is not printing "photos profile are identical", what can i do?尽管图像相同,但控制台未打印“照片配置文件相同”,我该怎么办?

You can use the imagehash library to compare similar images.您可以使用imagehash库来比较相似的图像。

from PIL import Image
import imagehash
hash0 = imagehash.average_hash(Image.open('quora_photo.jpg')) 
hash1 = imagehash.average_hash(Image.open('twitter_photo.jpeg')) 
cutoff = 5  # maximum bits that could be different between the hashes. 

if hash0 - hash1 < cutoff:
  print('images are similar')
else:
  print('images are not similar')

Since the images are not exactly the same, there will be some differences, so therefore we use a cutoff value with an acceptable maximum difference.由于图像不完全相同,会有一些差异,因此我们使用具有可接受的最大差异的截止值。 That difference between the hash objects is the number of bits that are flipped.散列对象之间的差异是翻转的位数。 But imagehash will work even if the images are resized, compressed, different file formats or with adjusted contrast or colors.但是即使图像被调整大小、压缩、不同的文件格式或调整了对比度或颜色,imagehash 也可以工作。

The hash (or fingerprint, really) is derived from a 8x8 monochrome thumbnail of the image.哈希(或指纹,实际上)来自图像的 8x8 单色缩略图。 But even with such a reduced sample, the similarity comparisons give quite accurate results.但即使样本如此减少,相似性比较也会给出相当准确的结果。 Adjust the cutoff to find a balance between false positives and false negatives that is acceptable.调整截止以找到可接受的误报和误报之间的平衡。

With 64 bit hashes, a difference of 0 means the hashes are identical.对于 64 位哈希,0 的差异意味着哈希是相同的。 A difference of 32 means that there's no similarity at all. 32 的差异意味着根本没有相似之处。 A difference of 64 means that one hash is the exact negative of the other. 64 的差异意味着一个散列是另一个散列的完全负数。

The two images are NOT the same - only the thing imaged.这两个图像不一样——只有成像的东西。 The images obviously are different size, as you note yourself.正如您自己所注意到的,这些图像显然是不同的大小。 Thus a comparison must fail.因此,比较必须失败。

You'll need to employ some kind of similarity check.您需要使用某种相似性检查。 The first step is to scale up the smaller image to the one of the larger one.第一步是将较小的图像放大到较大的图像。 Then you need to employ some mean of detecting and defining similarity.然后,您需要采用某种方法来检测和定义相似性。 There are different ways and methods for that, and any combination of them might be valid.有不同的方法和方法,它们的任何组合都可能是有效的。

For example see Checking images for similarity with OpenCV例如,请参阅使用 OpenCV 检查图像的相似性

import cv2

class CompareImage(object):

    def __init__(self, image_1_path, image_2_path):
        self.minimum_commutative_image_diff = 1
        self.image_1_path = image_1_path
        self.image_2_path = image_2_path

    def compare_image(self):
        image_1 = cv2.imread(self.image_1_path, 0)
        image_2 = cv2.imread(self.image_2_path, 0)
        commutative_image_diff = self.get_image_difference(image_1, image_2)

        if commutative_image_diff < self.minimum_commutative_image_diff:
            print "Matched"
            return commutative_image_diff
        return 10000 //random failure value

    @staticmethod
    def get_image_difference(image_1, image_2):
        first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
        second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])

        img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA)
        img_template_probability_match = cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0]
        img_template_diff = 1 - img_template_probability_match

        # taking only 10% of histogram diff, since it's less accurate than template method
        commutative_image_diff = (img_hist_diff / 10) + img_template_diff
        return commutative_image_diff


    if __name__ == '__main__':
        compare_image = CompareImage('image1/path', 'image2/path')
        image_difference = compare_image.compare_image()
        print image_difference

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

相关问题 如何比较两个彩色图像之间的相似性? - how to compare similarity between two color images? 如何使用python检查两个不同excel文件中两个列表之间的相似性? - How to check the similarity between two lists in two different excel files using python? 使用 open cv 检查不同日期拍摄的两幅植物图像的相似性 - Checking the similarity of two plant images captured on different date with open cv 如何衡量两个灰度图像之间的相似度? - How to measure the similarity between two gray level images? 如何在两个不同的 numpy 字符串 arrays 上测试行相似性(但不等价性) - how to test for row similarity (but not equivalence) on two different numpy string arrays 如何检查两个图像是否相同? - How to check if two images are the same? 在python中测量两个rgb图像之间的相似性 - measuring similarity between two rgb images in python 如何处理Matplotlib中重叠曲线的“像素化” - How to deal with the “pixelization” of overlapping curves in Matplotlib 使用带有NLTK的NLTK检查两个单词之间的相似性 - Check the similarity between two words with NLTK with Python 检查两个字符串之间的格式相似性 - Check format similarity between two strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM