1. 首页
  2. Python

图形相似度

 

04

图形相似度


图形相似度算法很多,这里用的是最简单的dhash算法。

def _dHash(self, img_path):
    hash_str = ''
    img = Image.open(img_path)
    width, height = img.size
    for i in range(width-1):
        for j in range(height):
            if img.getpixel((i, j)) > img.getpixel((i+1, j)):
                hash_str += '1'
            else:
                hash_str += '0'
    return hash_str


def _cmpHash(self, hash1, hash2):
    n = 0
    if len(hash1) != len(hash2):
        return -1
        # 遍历判断
    for i in range(len(hash1)):
        # 不相等则n计数+1,n最终为相似度
        if hash1[i] != hash2[i]:
            n = n + 1
    return n

这样遍历就可以预测出来了~~

def _predict_code(self):
    predict_number = ''
    for i in range(4):
        predict_code = []
        hash1 = self._dHash(self.number_path[i])
        compare_path = os.path.join(self.img_dir, str(i+1))
        files = os.listdir(compare_path)
        for index, file in enumerate(files):
            hash2 = self._dHash(os.path.join(compare_path, file))
            cmp_hash = self._cmpHash(hash1, hash2)
            predict_code.append(cmp_hash)
        predict_number += str(predict_code.index(min(predict_code)))
    return predict_number

def run(self):
    self._get_img_from_gif()
    self._merge_multi_pictures()
    self._split_2_four_part()

    print(self._predict_code())

参考:

[华科羽毛球自动抢订代码解析 – Tomorrow’s blog](https://tomorrow505.xyz/%E5%8D%8E%E7%A7%91%E7%BE%BD%E6%AF%9B%E7%90%83%E8%87%AA%E5%8A%A8%E6%8A%A2%E8%AE%A2%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90/)

发表评论

登录后才能评论