import matplotlib.pyplot as plt
import cv2
import numpy as np
import os
import pandas as pd
import time
start = time.process_time()
inputs1 = './inputs1'
inputs2 = './inputs2'
path0 = './txt-out'
if not os.path.exists(path0):
os.mkdir(path0)
path1 = './Last-Result'
if not os.path.exists(path1):
os.mkdir(path1)
f3 = open('./txt-out/absdiff.txt','w+')
f4 = open('./txt-out/resolution.txt','w+')
filepath1 = './inputs1'
filepath2 = './inputs2'
pathDir1 = os.listdir(filepath1)
pathDir1.sort(key=lambda x: int(x.split('-')[0]))
pathDir2 = os.listdir(filepath2)
pathDir2.sort(key=lambda x: int(x.split('-')[0]))
for allDir1,allDir2 in zip(pathDir1,pathDir2):
picpath1 = r'./inputs1/' + allDir1
picpath2 = r'./inputs2/' + allDir2
img1 = cv2.imdecode(np.fromfile(picpath1, dtype=np.uint8), 1)
img2 = cv2.imdecode(np.fromfile(picpath2, dtype=np.uint8), 1)
H1 = cv2.calcHist([img1], [0], None, [256],[0,256])
H1 = cv2.normalize(H1, H1, 0, 1, cv2.NORM_MINMAX, -1)
H2 = cv2.calcHist([img2], [0], None, [256],[0,256])
H2 = cv2.normalize(H2, H2, 0, 1, cv2.NORM_MINMAX, -1)
a_gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
b_gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
a_Gauss = cv2.GaussianBlur(a_gray,(3,3),0)
b_Gauss = cv2.GaussianBlur(b_gray,(3,3),0)
diff = cv2.absdiff(a_Gauss,b_Gauss).mean()
resolution = cv2.Laplacian(a_gray, cv2.CV_64F).var()
'''# sobel算子
sobelx = cv2.Sobel(a_gray,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(a_gray,cv2.CV_64F,0,1,ksize=5)
absX = cv2.convertScaleAbs(sobelx) # 转回uint8
absY = cv2.convertScaleAbs(sobely)
'''
print('当前正在计算:'+allDir1+' <———VS———> '+allDir2+'\t'+'请稍等………')
print('清晰度:',resolution)
print('帧间差分:',diff,'\n')
print(allDir1 + "--Vs--"+allDir2+':',diff,file = f3)
print(allDir1 + ':',resolution,file = f4)
f3.close()
f4.close()
print('\n','恭喜,计算完成!')
file3 = open('.\\txt-out\\absdiff.txt', 'r')
df3 = file3.read().split('\n')
df3 = pd.DataFrame(df3,columns=['Compare-Name'])
dt3 = df3['Compare-Name'].str.split(':',expand=True)
df3[['Compare-Name','absdiff']]=dt3 # 设置列名
df3['absdiff'] = df3['absdiff'].astype(np.float)
df3.to_excel('./Last-Result/'+time.strftime('%Y%m%d%H%M%S'+'-',time.localtime(time.time()))+'帧间差分.xls','帧间差分',index = False)
print('帧间差分结果存储完成,请移步----→ Last-Result( 帧间差分.xls )中查看结果。')
file4 = open('.\\txt-out\\resolution.txt', 'r')
df4 = file4.read().split('\n')
df4 = pd.DataFrame(df4,columns=['Compare-Name'])
dt4 = df4['Compare-Name'].str.split(':',expand=True)
df4[['Compare-Name','resolution']]=dt4 # 设置列名
df4['resolution'] = df4['resolution'].astype(np.float)
df4.to_excel('./Last-Result/'+time.strftime('%Y%m%d%H%M%S'+'-',time.localtime(time.time()))+'清晰度.xls','清晰度-Laplacian',index = False)
print('清晰度结果存储完成,请移步----→ Last-Result( 清晰度.xls )中查看结果。')
end = time.process_time()
print('\n','程序运行耗费时间: %s Seconds'%(end-start))
'''
# 两张图对比
a = cv2.imread('./m.jpg')
b = cv2.imread('./n.jpg')
H1 = cv2.calcHist([a], [0], None, [256],[0,256])
H1 = cv2.normalize(H1, H1, 0, 1, cv2.NORM_MINMAX, -1) # 对图片进行归一化处理
H2 = cv2.calcHist([b], [0], None, [256],[0,256])
H2 = cv2.normalize(H2, H2, 0, 1, cv2.NORM_MINMAX, -1)
similarity0 = cv2.compareHist(H1, H2,cv2.HISTCMP_CHISQR) # 卡方检验
similarity1 = cv2.compareHist(H1, H2,cv2.HISTCMP_CORREL) # 相关性
similarity2 = cv2.compareHist(H1, H2,cv2.HISTCMP_BHATTACHARYYA) # 巴氏距离
print('chi:',similarity0)
print('correl:',similarity1)
print('dis:',similarity2)
'''