关于分析视频清晰度

import cv2
import numpy as np
import time
from control_utils import z_downwards, z_stop, z_upwards, setMicroStep, serial_ports, z_downwards_start, z_upwards_start
import serial
import matplotlib.pyplot as plt
    

def computeClarity(img): #这里其实可以使用方差
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    suma=0
    for i in range (1,len(img)-2):
        for j in range(1,len(img[i])-2):
                       suma+=abs(img[i][j]-img[i][j-1])+abs(img[i][j]-img[i][j+1])+abs(img[i][j]-img[i-1][j])+abs(img[i][j]-img[i+1][j])
    return suma
    
    

def readvideo_save(video_path ,skipNo):
    claritylist = []
    VIDEO_PATH = "surviv.io - 2d battle royale game - 个人 - Microsoft Edge 2022-07-06 13-29-06" #读取视频文件
    cap = cv2.VideoCapture(video_path)
    # if not video.isOpened():
    #     print("Could not load video.")
    # else: #成功读入视频
    f_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #获取视频每帧图像的宽
    f_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))#获取视频每帧图像的高
    
    # print("Video loaded!\n")
    print("Frame width: {} px".format(f_width))
    print("Frame height: {} px".format(f_height))
    
    OUTPUT_VIDEO_PATH = "FastVideo.mp4"
    video_out = cv2.VideoWriter(OUTPUT_VIDEO_PATH,
                    cv2.VideoWriter_fourcc('h', '2', '6', '4'),
                    30,
                    (f_width, f_height))#写视频
    frameNo = 0
    one_dir_cnt = 0
    cur_clar = 0
    pre_clar = 0
    pre2_clar = 0
    isDownWard = False
    clar_list = []
    while True: #持续读入每一帧
        ret, frame = cap.read()
        k = cv2.waitKey(100) & 0xff
        if k == 27: #按ESC键退出
            break
        if frameNo % skipNo ==1:
            try:
                cur_clar = computeClarity(frame)
                clar_list.append(cur_clar)
                cv2.putText(frame, str(cur_clar), (100, 110), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
                cv2.imshow('frame', frame)  
                video_out.write(frame)
            except:
                cur_clar = 0
                clar_list.append(cur_clar)                
        frameNo += 1
    video_out.release()
    cv2.destroyAllWindows()
    return clar_list

if __name__ == '__main__':
    #TODO2 修改视频路径
    video_path = 'surviv.io - 2d battle royale game - 个人 - Microsoft Edge 2022-07-06 13-29-06'
    clar_list = readvideo_save(video_path, 10)
    plt.figure()
    plt.plot(range(len(clar_list)), clar_list)
    plt.show()

你可能感兴趣的:(关于分析视频清晰度)