#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import Image,ImageFilter
def make_regalur_image(img, size = (64, 64)):
return img.resize(size).convert('RGBA')
# return img.resize(size).convert('RGB')
def split_image(img, part_size = (64, 64)):
w, h = img.size
pw, ph = part_size
assert w % pw == h % ph == 0
return [img.crop((i, j, i+pw, j+ph)).copy() \
for i in xrange(0, w, pw) \
for j in xrange(0, h, ph)]
def hist_similar(lh, rh):
assert len(lh) == len(rh)
print lh
# for l, r in zip(lh, rh):
# print l,r
return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh)
def compare(value1,value2):
# if value1>0 and value2>0:
# return 1
# elif value1==0 and value2==0:
# return 1
# else:
# return 0
temp=0
if value1==value2:
temp=0
else:
temp=float(abs(value1-value2))/max(value1,value2)
return 1-temp
def compareRGB(color1,color2):
temp=0;
temp+=compare(color1[3],color2[3])
return temp
# for i in xrange(0,3):
# temp+=compare(color1[i],color2[i])
# return temp*1.0/3
def calc_similar(li, ri):
pixL=li.load()
pixR=ri.load()
w,h=li.size
temp=0
number=0
for i in xrange(0,h):
for j in xrange(0,w):
color1=pixL[j,i]
color2=pixR[j,i]
if color1[3]>0 or color2[3]>0:
number+=1
if color1[3]>0 and color2[3]>0:
temp+=1
return temp*1.0/number
def calc_similar_by_path(lf, rf):
li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
im_contour = ri.filter(ImageFilter.CONTOUR)
# return calc_similar(li, ri)
return calc_similar(li, ri)
def getSimilarValue(imagepath1,imagepath2):
return calc_similar_by_path(imagepath1,imagepath2)*100
def getSimilarValueX(pix_data1,pix_data2):
pixL=pix_data1
pixR=pix_data2
w,h=64,64
temp=0
number=0
for i in xrange(0,h):
for j in xrange(0,w):
color1=pixL[j,i]
color2=pixR[j,i]
if color1[3]>0 or color2[3]>0:
number+=1
if color1[3]>0 and color2[3]>0:
temp+=1
return (temp*1.0/number)*100
def getPixelAccesData(imagepath):
img=make_regalur_image(Image.open(imagepath))
return img.load()
def getA_Number(imagepath):
img=make_regalur_image(Image.open(imagepath))
count=0
pix=img.load()
w,h=img.size
for i in xrange(0,h):
for j in xrange(0,w):
r,g,b,a=pix[j,i]
if a>0:
count+=1
return count
# 输入灰度图,返回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 changeImage(res):
h,w,channels=res.shape
for i in xrange(0,h):
for j in xrange(0,w):
color=res[j,i].tolist()
if color[3]>0:
color[0]=255
color[1]=255
color[2]=255
res[j,i]=np.array(color)
else:
color[0]=0
color[1]=0
color[2]=0
res[j,i]=np.array(color)
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
# ret, thresh = cv2.threshold(gray,127, 255, cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(res,contours,-1,(0,255,0,255),4)
# cv2.imshow("res",res)
# print contours
return gray
def getImageCountour(res):
h,w,channels=res.shape
for i in xrange(0,h):
for j in xrange(0,w):
color=res[j,i].tolist()
if color[3]>0:
color[0]=255
color[1]=255
color[2]=255
res[j,i]=np.array(color)
else:
color[0]=0
color[1]=0
color[2]=0
res[j,i]=np.array(color)
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(res,contours,-1,(0,255,0,255),4)
# cv2.imshow("res",res)
# print contours
return contours
# 计算汉明距离
def Hamming_distance(hash1,hash2):
num = 0
for index in range(len(hash1)):
if hash1[index] != hash2[index]:
num += 1
return num
# 平均哈希算法计算
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 get_aHash(imagepath):
img=cv2.imread(imagepath,-1)
res=cv2.resize(img,(8,8),interpolation=cv2.INTER_CUBIC)
gray=changeImage(res)
ahash=getHash(gray)
return ahash
def compareCountour(imagepath1,imagepath2):
img1=cv2.imread(imagepath1,-1)
res1=cv2.resize(img1,(8,8),interpolation=cv2.INTER_CUBIC)
contours1=getImageCountour(res1)
img2=cv2.imread(imagepath2,-1)
res2=cv2.resize(img2,(8,8),interpolation=cv2.INTER_CUBIC)
contours2=getImageCountour(res2)
temp=0
# mlist=list(contours1)
# for i in xrange(0,len(mlist)):
# print list(mlist[i])
# print list(contours1)
# print "____________"
# print contours2
# print len(contours1)
# print contours1
# print len(contours2)
# print contours2
# minValue=min(len(contours1),len(contours2))
# for i in xrange(0,minValue):
# mValue=cv2.matchShapes(contours1[i],contours2[i],cv2.cv.CV_CONTOURS_MATCH_I1,0.0)
# print mValue
# temp+=mValue
# print temp
# print contours1[0],contours2[0]
# print cv2.matchShapes(contours1,contours2,cv2.cv.CV_CONTOURS_MATCH_I1,0.0)
def compare2(imagepath1,imagepath2):
img1=cv2.imread(imagepath1,-1)
res1=cv2.resize(img1,(8,8),interpolation=cv2.INTER_LINEAR)
gray1=changeImage(res1)
img2=cv2.imread(imagepath2,-1)
res2=cv2.resize(img2,(8,8),interpolation=cv2.INTER_LINEAR)
gray2=changeImage(res2)
hash1=getHash(gray1)
hash2=getHash(gray2)
return(1.0-Hamming_distance(hash1,hash2)/64.0)*100
def getSimilarDegress(hash1,hash2):
return(1.0-Hamming_distance(hash1,hash2)/64.0)*100
def getSimilarValue2(imagepath,pixR):
img=make_regalur_image(Image.open(imagepath))
pixL=img.load()
w,h=img.size
temp=0
for i in xrange(0,h):
for j in xrange(0,w):
temp+=compareRGB(pixL[j,i],pixR[j,i])
return (temp/(w*h))*100
def test():
# print getSimilarValue("0048_1024.png","1024.png")
# print compare2("0048_1024.png","1024.png")
compareCountour("111.png","222.png")
# cv2.waitKey(0)
test()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import Image,ImageFilter
def make_regalur_image(img, size = (64, 64)):
return img.resize(size).convert('RGBA')
# return img.resize(size).convert('RGB')
def split_image(img, part_size = (64, 64)):
w, h = img.size
pw, ph = part_size
assert w % pw == h % ph == 0
return [img.crop((i, j, i+pw, j+ph)).copy() \
for i in xrange(0, w, pw) \
for j in xrange(0, h, ph)]
def hist_similar(lh, rh):
assert len(lh) == len(rh)
print lh
# for l, r in zip(lh, rh):
# print l,r
return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh)
def compare(value1,value2):
# if value1>0 and value2>0:
# return 1
# elif value1==0 and value2==0:
# return 1
# else:
# return 0
temp=0
if value1==value2:
temp=0
else:
temp=float(abs(value1-value2))/max(value1,value2)
return 1-temp
def compareRGB(color1,color2):
temp=0;
temp+=compare(color1[3],color2[3])
return temp
# for i in xrange(0,3):
# temp+=compare(color1[i],color2[i])
# return temp*1.0/3
def calc_similar(li, ri):
pixL=li.load()
pixR=ri.load()
w,h=li.size
temp=0
number=0
for i in xrange(0,h):
for j in xrange(0,w):
color1=pixL[j,i]
color2=pixR[j,i]
if color1[3]>0 or color2[3]>0:
number+=1
if color1[3]>0 and color2[3]>0:
temp+=1
return temp*1.0/number
def calc_similar_by_path(lf, rf):
li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
im_contour = ri.filter(ImageFilter.CONTOUR)
# return calc_similar(li, ri)
return calc_similar(li, ri)
def getSimilarValue(imagepath1,imagepath2):
return calc_similar_by_path(imagepath1,imagepath2)*100
def getSimilarValueX(pix_data1,pix_data2):
pixL=pix_data1
pixR=pix_data2
w,h=64,64
temp=0
number=0
for i in xrange(0,h):
for j in xrange(0,w):
color1=pixL[j,i]
color2=pixR[j,i]
if color1[3]>0 or color2[3]>0:
number+=1
if color1[3]>0 and color2[3]>0:
temp+=1
return (temp*1.0/number)*100
def getPixelAccesData(imagepath):
img=make_regalur_image(Image.open(imagepath))
return img.load()
def getA_Number(imagepath):
img=make_regalur_image(Image.open(imagepath))
count=0
pix=img.load()
w,h=img.size
for i in xrange(0,h):
for j in xrange(0,w):
r,g,b,a=pix[j,i]
if a>0:
count+=1
return count
# 输入灰度图,返回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 changeImage(res):
h,w,channels=res.shape
for i in xrange(0,h):
for j in xrange(0,w):
color=res[j,i].tolist()
if color[3]>0:
color[0]=255
color[1]=255
color[2]=255
res[j,i]=np.array(color)
else:
color[0]=0
color[1]=0
color[2]=0
res[j,i]=np.array(color)
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
# ret, thresh = cv2.threshold(gray,127, 255, cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(res,contours,-1,(0,255,0,255),4)
# cv2.imshow("res",res)
# print contours
return gray
def getImageCountour(res):
h,w,channels=res.shape
for i in xrange(0,h):
for j in xrange(0,w):
color=res[j,i].tolist()
if color[3]>0:
color[0]=255
color[1]=255
color[2]=255
res[j,i]=np.array(color)
else:
color[0]=0
color[1]=0
color[2]=0
res[j,i]=np.array(color)
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(res,contours,-1,(0,255,0,255),4)
# cv2.imshow("res",res)
# print contours
return contours
# 计算汉明距离
def Hamming_distance(hash1,hash2):
num = 0
for index in range(len(hash1)):
if hash1[index] != hash2[index]:
num += 1
return num
# 平均哈希算法计算
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 get_aHash(imagepath):
img=cv2.imread(imagepath,-1)
res=cv2.resize(img,(8,8),interpolation=cv2.INTER_CUBIC)
gray=changeImage(res)
ahash=getHash(gray)
return ahash
def compareCountour(imagepath1,imagepath2):
img1=cv2.imread(imagepath1,-1)
res1=cv2.resize(img1,(8,8),interpolation=cv2.INTER_CUBIC)
contours1=getImageCountour(res1)
img2=cv2.imread(imagepath2,-1)
res2=cv2.resize(img2,(8,8),interpolation=cv2.INTER_CUBIC)
contours2=getImageCountour(res2)
temp=0
# mlist=list(contours1)
# for i in xrange(0,len(mlist)):
# print list(mlist[i])
# print list(contours1)
# print "____________"
# print contours2
# print len(contours1)
# print contours1
# print len(contours2)
# print contours2
# minValue=min(len(contours1),len(contours2))
# for i in xrange(0,minValue):
# mValue=cv2.matchShapes(contours1[i],contours2[i],cv2.cv.CV_CONTOURS_MATCH_I1,0.0)
# print mValue
# temp+=mValue
# print temp
# print contours1[0],contours2[0]
# print cv2.matchShapes(contours1,contours2,cv2.cv.CV_CONTOURS_MATCH_I1,0.0)
def compare2(imagepath1,imagepath2):
img1=cv2.imread(imagepath1,-1)
res1=cv2.resize(img1,(8,8),interpolation=cv2.INTER_LINEAR)
gray1=changeImage(res1)
img2=cv2.imread(imagepath2,-1)
res2=cv2.resize(img2,(8,8),interpolation=cv2.INTER_LINEAR)
gray2=changeImage(res2)
hash1=getHash(gray1)
hash2=getHash(gray2)
return(1.0-Hamming_distance(hash1,hash2)/64.0)*100
def getSimilarDegress(hash1,hash2):
return(1.0-Hamming_distance(hash1,hash2)/64.0)*100
def getSimilarValue2(imagepath,pixR):
img=make_regalur_image(Image.open(imagepath))
pixL=img.load()
w,h=img.size
temp=0
for i in xrange(0,h):
for j in xrange(0,w):
temp+=compareRGB(pixL[j,i],pixR[j,i])
return (temp/(w*h))*100
def test():
# print getSimilarValue("0048_1024.png","1024.png")
# print compare2("0048_1024.png","1024.png")
compareCountour("111.png","222.png")
# cv2.waitKey(0)
test()