|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# @Time : 2018/3/13 08:30 |
| 3 | +# @Author : play4fun |
| 4 | +# @File : compare_photos.py |
| 5 | +# @Software: PyCharm |
| 6 | + |
| 7 | +""" |
| 8 | +compare_photos.py: |
| 9 | +""" |
| 10 | + |
| 11 | +import cv2, pickle |
| 12 | + |
| 13 | +with open('photo_mat', 'rb') as f: |
| 14 | + mat = pickle.load(f) |
| 15 | + |
| 16 | +pairs = [] # 配对好的 |
| 17 | +lenX = 9 # 行 |
| 18 | +lenY = 8 # 列 |
| 19 | + |
| 20 | + |
| 21 | +def get_image_difference(image_1, image_2):#这个函数不行 |
| 22 | + first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256]) |
| 23 | + second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256]) |
| 24 | + |
| 25 | + img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA) |
| 26 | + img_template_probability_match = cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0] |
| 27 | + img_template_diff = 1 - img_template_probability_match |
| 28 | + |
| 29 | + # taking only 10% of histogram diff, since it's less accurate than template method |
| 30 | + commutative_image_diff = (img_hist_diff / 10) + img_template_diff |
| 31 | + return commutative_image_diff |
| 32 | + |
| 33 | + |
| 34 | +def compare(i, j, img): |
| 35 | + for x in range(lenX): |
| 36 | + if x < i: |
| 37 | + continue |
| 38 | + for y in range(lenY): |
| 39 | + if y < j: |
| 40 | + continue |
| 41 | + z = mat[x][y] |
| 42 | + # 图片相似度 |
| 43 | + y1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 44 | + z1 = cv2.cvtColor(z, cv2.COLOR_BGR2GRAY) |
| 45 | + image_difference = get_image_difference(y1, z1) |
| 46 | + print(i, j, x, y, image_difference) |
| 47 | + # if abs(image_difference-1)>0.5: |
| 48 | + if image_difference < 0.1: |
| 49 | + pairs.append((i, j, x, y, image_difference)) |
| 50 | + print('--------') |
| 51 | + |
| 52 | + |
| 53 | +for i, x in enumerate(mat): |
| 54 | + for j, y in enumerate(x): |
| 55 | + compare(i, j, y) |
| 56 | + |
| 57 | +print('--------') |
| 58 | +# print(pairs) |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +# 1, 0, 1, 5 |
| 63 | +a = mat[1][0] |
| 64 | +b = mat[1][5] |
| 65 | +y1 = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY) |
| 66 | +z1 = cv2.cvtColor(b, cv2.COLOR_BGR2GRAY) |
| 67 | +image_difference = get_image_difference(y1, z1) |
| 68 | +print(1, 0, 1, 5, image_difference) |
0 commit comments