11.10 pycharm练习

这篇博客介绍了两种视频帧相似度检测方法:哈希算法和直方图比较。首先,通过哈希算法对图像进行处理,定义了哈希值函数aHash和比较哈希值的函数cmpHash,用于从视频中分帧。然后,利用直方图比较方法classify_hist_with_split计算相似度,结合阈值筛选帧。这两种方法分别应用于不同的Python文件,并在Flask web应用中展示结果。
摘要由CSDN通过智能技术生成

一、hash分镜

导入包——哈希算法——定义根目录、分镜——显示在网页里

from flask import Flask,render_template
import os
import cv2

app = Flask(__name__)

def aHash(img): #定义哈希值函数
    img = cv2.resize(img, (8, 8))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    s = 0
    hash_str = ''

    for i in range(8):
        for j in range(8):
            s = s + gray[i, j]

    avg = s / 64
    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
    print(hash1)
    print(hash2)

    if len(hash1) != len(hash2):
        return -1
    # 遍历判断
    for i in range(len(hash1)):
        # 不相等则n计数+1,n最终为相似度
        if hash1[i] != hash2[i]:
            n = n + 1
    return n

def genFrame():  #定义根目录,分镜
    v_path = "static/ghz.mp4"
    image_save = "static/hash"

    if not(os.path.exists(image_save)):
        os.mkdir(image_save)
    cap=cv2.VideoCapture(v_path)
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)

    _,img1=cap.read() #读取第一张图像
    cv2.imwrite("static/hash/image{}.jpg".format(0),img1)
    print(fc)
    for i in range(int(fc)-1):
        _,img2 = cap.read()
        hash1 = aHash(img1)
        hash2 = aHash(img2)
        n = cmpHash(hash1, hash2)
        if (n>35): #数值越大,分的帧数越小
            cv2.imwrite("static/hash/image{}.jpg".format(i), img2)
            img1=img2

@app.route('/hash')
def index():
    genFrame()

    path='static/hash'
    filename = os.listdir(path)
    framecount=len(filename)
    filename.sort(key= lambda x:int(x[5:-4]))
    print(filename)
    return render_template("hash.html", filename=filename, framecount=framecount)

if "__main__" == __name__:
    app.run(port="5008")

注意在打开网页时末尾要加上“/hash"

运行结果

二、直方图比较

from flask import Flask,render_template
import cv2
import os

app = Flask(__name__)

os.chdir(r"C:\Users\lenovo\AppData\Local\Programs\Python\Python37\11.10")
# 通过得到RGB每个通道的直方图来计算相似度
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 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 genFrame():  # 定义根目录,分镜
    v_path = "static/ghz.mp4"
    image_save = "static/hist"

    if not (os.path.exists(image_save)):
        os.mkdir(image_save)

    cap = cv2.VideoCapture(v_path)
    fc = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    print(fc)
    _, img1 = cap.read()  # 读取第一张图像
    cv2.imwrite("static/hist/image{}.jpg".format(0), img1)
    print(int(fc))
    for i in range(248):
        _, img2 = cap.read()
        n = classify_hist_with_split(img1,img2)
        if (n < 0.6):  # 数值越大,分的帧数越小
            cv2.imwrite("static/hist/image{}.jpg".format(i), img2)
            img1 = img2

genFrame()

@app.route('/hist')
def index():
    path='static/hist'
    histfile = os.listdir(path)
    histcount=int(len(histfile))
    histfile.sort(key= lambda x:int(x[5:-4]))   #对List的元素排序
    print(histfile)
    return render_template('hist.html',path=path,histfile=histfile,histcount=histcount)

if "__main__" == __name__:
    app.run(port="5009")

注意:直方图与哈希算法分开来写更清晰,运行时注意选择的是哪个py文件

顺序是:先直方图,再定义定义根目录和分镜,最后呈现在网页里

运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值