-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDiff.py
More file actions
129 lines (119 loc) · 4.6 KB
/
findDiff.py
File metadata and controls
129 lines (119 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
#feimengjuan
# ÀûÓÃpythonʵÏÖ¶àÖÖ·½·¨À´ÊµÏÖͼÏñʶ±ð
import cv2
import numpy as np
#from matplotlib import pyplot as plt
# ×î¼òµ¥µÄÒÔ»Ò¶ÈÖ±·½Í¼×÷ΪÏàËÆ±È½ÏµÄʵÏÖ
def classify_gray_hist(image1,image2,size = (256,256)):
# ÏȼÆËãÖ±·½Í¼
# ¼¸¸ö²ÎÊý±ØÐëÓ÷½À¨ºÅÀ¨ÆðÀ´
# ÕâÀïÖ±½ÓÓûҶÈͼ¼ÆËãÖ±·½Í¼£¬ËùÒÔÊÇʹÓõÚÒ»¸öͨµÀ£¬
# Ò²¿ÉÒÔ½øÐÐͨµÀ·ÖÀëºó£¬µÃµ½¶à¸öͨµÀµÄÖ±·½Í¼
# bins ȡΪ16
image1 = cv2.resize(image1,size)
image2 = cv2.resize(image2,size)
hist1 = cv2.calcHist([image1],[0],None,[256],[0.0,255.0])
hist2 = cv2.calcHist([image2],[0],None,[256],[0.0,255.0])
# ¿ÉÒԱȽÏÏÂÖ±·½Í¼
plt.plot(range(256),hist1,'r')
plt.plot(range(256),hist2,'b')
plt.show()
# ¼ÆËãÖ±·½Í¼µÄÖØºÏ¶È
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + (1 - abs(hist1[i]-hist2[i])/max(hist1[i],hist2[i]))
else:
degree = degree + 1
degree = degree/len(hist1)
return degree
# ¼ÆË㵥ͨµÀµÄÖ±·½Í¼µÄÏàËÆÖµ
def calculate(image1,image2):
hist1 = cv2.calcHist([image1],[0],None,[256],[0.0,255.0])
hist2 = cv2.calcHist([image2],[0],None,[256],[0.0,255.0])
# ¼ÆËãÖ±·½Í¼µÄÖØºÏ¶È
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + (1 - abs(hist1[i]-hist2[i])/max(hist1[i],hist2[i]))
else:
degree = degree + 1
degree = degree/len(hist1)
return degree
# ͨ¹ýµÃµ½Ã¿¸öͨµÀµÄÖ±·½Í¼À´¼ÆËãÏàËÆ¶È
def classify_hist_with_split(image1,image2,size = (256,256)):
# ½«Í¼Ïñresizeºó£¬·ÖÀëΪÈý¸öͨµÀ£¬ÔÙ¼ÆËãÿ¸öͨµÀµÄÏàËÆÖµ
image1 = cv2.resize(image1,size)
image2 = cv2.resize(image2,size)
sub_image1 = cv2.split(image1)
sub_image2 = cv2.split(image2)
sub_data = 0
for im1,im2 in zip(sub_image1,sub_image2):
sub_data += calculate(im1,im2)
sub_data = sub_data/3
return sub_data
# ƽ¾ù¹þÏ£Ëã·¨¼ÆËã
def classify_aHash(image1,image2):
image1 = cv2.resize(image1,(8,8))
image2 = cv2.resize(image2,(8,8))
gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
hash1 = getHash(gray1)
hash2 = getHash(gray2)
return Hamming_distance(hash1,hash2)
def classify_pHash(image1,image2):
image1 = cv2.resize(image1,(32,32))
image2 = cv2.resize(image2,(32,32))
gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
# ½«»Ò¶ÈͼתΪ¸¡µãÐÍ£¬ÔÙ½øÐÐdct±ä»»
dct1 = cv2.dct(np.float32(gray1))
dct2 = cv2.dct(np.float32(gray2))
# È¡×óÉϽǵÄ8*8£¬ÕâЩ´ú±íͼƬµÄ×îµÍƵÂÊ
# Õâ¸ö²Ù×÷µÈ¼ÛÓÚc++ÖÐÀûÓÃopencvʵÏÖµÄÑÚÂë²Ù×÷
# ÔÚpythonÖнøÐÐÑÚÂë²Ù×÷£¬¿ÉÒÔÖ±½ÓÕâÑùÈ¡³öͼÏñ¾ØÕóµÄijһ²¿·Ö
dct1_roi = dct1[0:8,0:8]
dct2_roi = dct2[0:8,0:8]
hash1 = getHash(dct1_roi)
hash2 = getHash(dct2_roi)
return Hamming_distance(hash1,hash2)
# ÊäÈë»Ò¶Èͼ£¬·µ»Øhash
def getHash(image):
avreage = np.mean(image)
hash = []
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if image[i,j] > avreage:
hash.append(1)
else:
hash.append(0)
return hash
# ¼ÆË㺺Ã÷¾àÀë
def Hamming_distance(hash1,hash2):
num = 0
for index in range(len(hash1)):
if hash1[index] != hash2[index]:
num += 1
return num
def isDiff(img1,img2):
#degree = classify_gray_hist(img1,img2)
#degree = classify_hist_with_split(img1,img2)
degree = classify_aHash(img1,img2)
#degree = classify_pHash(img1,img2)
#cv2.waitKey(0)
if degree>=7:
return False;
else:
return True;
if __name__ == '__main__':
img1 = cv2.imread('image22.jpg')
#cv2.imshow('img1',img1)
img2 = cv2.imread('image21.jpg')
#cv2.imshow('img2',img2)
#degree = classify_gray_hist(img1,img2)
#degree = classify_hist_with_split(img1,img2)
degree = classify_aHash(img1,img2)
#degree = classify_pHash(img1,img2)
#print degree
cv2.waitKey(0)