一.视频分镜:基于均值哈希;基于直方图相似度
基于均值哈希相似度:
import os
from PIL import Image
os.chdir("/Users/arco/文稿/2021秋python/1021")
print(os.getcwd())
for i in range(863):
img1=cv2.imread("/Users/arco/文稿/2021秋python/1021/image{}.jpg".format(i))
img2=cv2.imread("/Users/arco/文稿/2021秋python/1021/image{}.jpg".format(i+1))
hash1=aHash(img1)
hash2=aHash(img2)
n=cmpHash(hash1,hash2)
if(n>32):
print("均值哈希算法相似度:",n/64)
cv2.imwrite("/Users/arco/文稿/2021秋python/shot/image{}.jpg".format(i+1),img2)
基于直方图相似度:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
os.chdir(r'/Users/arco/文稿/2021秋python/10.21')
# 通过得到RGB每个通道的直方图来计算相似度
def classify_hist_with_split(image1, image2, size=(256, 256)):
# 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
image1 = cv2.resize(image1, size)
image2 = cv2.resize(image2, size)
plt.imshow(image1)
plt.show()
plt.axis('off')
plt.imshow(image2)
plt.show()
plt.axis('off')
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 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])
plt.plot(hist1, color="r")
plt.plot(hist2, color="g")
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
n = classify_hist_with_split(img1, img2)
print('三直方图算法相似度:', n)
但没有做到通过比较直方图相似度删除相似分镜
因为在if语句中(n>)系数不清楚具体含义
二.在不同参数和视频(2-3个)的实验效果,用FFMPEG截取一段1分钟视频,测试。
cd /Users/arco/文稿/2021秋python
#定位到存在ffmpeg的文件夹
ffmpeg -i C:/Users/arco/文稿/2021秋python/zuoye1.MP4 -ss 00:00:00 -t 10 -vcodec copy -acodec copy -preset superfast output.mp4
#00:00:00是开始截视频的时间,t 10是指截取10s
(在mac上依然无法使用ffmpeg该功能,因此借用windows同学电脑跑出结果)