#镜头长度
import os
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
#均值哈希
#均值哈希
def aHash(img): # 缩放为8*8
img = cv2.resize(img, (8, 8))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#归一化为灰度图
# s为像素和初值为0,hash_str为hash值初值为''
s = 0
hash_str = '' # 遍历累加求像素和
for i in range(8):
for j in range(8):
s = s + gray[i, j] # 求平均灰度
avg = s / 64 # 灰度大于平均值为1相反为0生成图片的hash值
for i in range(8):
for j in range(8):
if gray[i, j] > avg:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
def cmphash(hash1,hash2):
n=0
if len(hash1)!=len(hash2):
return -1
for i in range(len(hash1)):
if hash1[i]!=hash2[i]:
n=n+1;
n=n/len(hash1)
return n
#差值哈希算法
def dHash(img):
# 缩放8*8
img = cv2.resize(img, (9, 8))
# 转换灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (32, 32))
hash_str = ''
# 每行前一个像素大于后一个像素为1,相反为0,生成哈希
for i in range(8):
for j in range(8):
if gray[i, j] > gray[i, j + 1]:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
#感知哈希
def pHash(img):
if img is None:
print("Image is empty.")
else:
img=cv2.resize(img,(32,32))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dct=cv2.dct(np.float32(gray))
dct_roi=dct[0:8,0:8]
hash=[]
average=np.mean(dct_roi)
for i in range(dct_roi.shape[0]):
for j in range(dct_roi.shape[1]):
if dct_roi[i,j]>average:
hash.append(1)
else:
hash.append(0)
return hash
#RGB直方图相似度
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后,分离为RGB三个通道,再计算每个通道的相似值
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 draw(scene_changes,fps,way):
import matplotlib
ds=[]
for i in range(len(scene_changes) - 1): # 遍历数组,注意只遍历到倒数第二个元素
diff = scene_changes[i+1] - scene_changes[i] # 计算后一个数减去前一个数的差值
ds.append(diff/fps)
matplotlib.rc("font", family='YouYuan')
plt.plot(range(len(ds)), ds)
# 设置 x 轴标签和 y 轴标签以及标题
plt.xlabel("场次序号")
plt.ylabel("时间/s")
plt.title("{}算法阈值0.6镜头长度".format(way))
# 显示图形
plt.show()
def test1(thresholds,ways):
v_path = 'static/巴斯特斯克鲁格斯的歌谣决斗场面.mp4'
cap=cv2.VideoCapture(v_path)
fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps=cap.get(cv2.CAP_PROP_FPS)
scene_changes = []
similarities = []
print(fc)
_, img1 = cap.read()
cv2.imwrite('static/{}/image{}.jpg'.format(ways,0), img1)
print(int(fc))
for i in range(1,int(fc)):
_, img2 = cap.read()
if ways=='ahash':
hash1=aHash(img1)
hash2=aHash(img2)
n=cmphash(hash1,hash2)
similarities.append(1-n)
if (1-n < thresholds):
cv2.imwrite('static/{}/image{}.jpg'.format(ways, i), img2)
scene_changes.append(i)
img1 = img2
if ways == 'phash':
hash1 = pHash(img1)
hash2 = pHash(img2)
n = cmphash(hash1, hash2)
similarities.append(1-n)
if (1-n <thresholds):
cv2.imwrite('static/{}/image{}.jpg'.format(ways, i), img2)
scene_changes.append(i)
img1 = img2
if ways == 'dhash':
hash1 = dHash(img1)
hash2 = dHash(img2)
n = cmphash(hash1, hash2)
similarities.append(1-n)
if (1-n<thresholds):
cv2.imwrite('static/{}/image{}.jpg'.format(ways, i), img2)
scene_changes.append(i)
img1 = img2
if ways == 'hist':
n=classify_hist_with_split(img1, img2)
similarities.append(n)
if (n < thresholds):
cv2.imwrite('static/{}/image{}.jpg'.format(ways, i), img2)
scene_changes.append(i)
img1 = img2
return scene_changes,similarities,fps
os.chdir("C:\\Users\\33361\\PycharmProjects\\F1\\")
way=["phash","ahash","dhash","hist"]
for i in range(len(way)):
scene_changes,similarities,fps=test1(0.6,way[i])
draw(scene_changes,fps,way[i])
效果