使用nrss方法计算视频的清晰度代码:
#encoding=utf-8
import cv2
import os
import pdb
import numpy as np
import time
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
def gauseBlur(img):
img_Guassian = cv2.GaussianBlur(img,(7,7),0)
return img_Guassian
def sobel(img):
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
absX = cv2.convertScaleAbs(x) # 转回uint8
absY = cv2.convertScaleAbs(y)
dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
return dst
def getBlock(G,Gr):
(h, w) = G.shape
G_blk_list = []
Gr_blk_list = []
sp = 6
for i in range(sp):
for j in range(sp):
G_blk = G[int((i / sp) * h):int(((i + 1) / sp) * h), int((j / sp) * w):int(((j + 1) / sp) * w)]
Gr_blk = Gr[int((i / sp) * h):int(((i + 1) / sp) * h), int((j / sp) * w):int(((j + 1) / sp) * w)]
G_blk_list.append(G_blk)
Gr_blk_list.append(Gr_blk)
sum = 0
for i in range(sp*sp):
mssim = compare_ssim(G_blk_list[i], Gr_blk_list[i])
sum = mssim + sum
nrss = 1-sum/(sp*sp*1.0)
return nrss
def NRSS(image):
Ir = gauseBlur(image)
G = sobel(image)
Gr = sobel(Ir)
val = getBlock(G, Gr)
return val
if __name__ == "__main__":
t0 = time.time()
cap = cv2.VideoCapture(r'in.ts')
wid = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
hei = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
sm_wid = wid // 2
sm_hei = hei // 2
i = 0
sum = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# img = cv2.resize(frame, (sm_wid, sm_hei), interpolation=cv2.INTER_NEAREST) #每帧4K图像用时约1.6s,缩小到1080用时约0.4s
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
nrss = NRSS(gray)
sum = sum + nrss
else:
break
i = i + 1
avg = sum / i
print("The nrss value is: %f" % avg)
cap.release()
t1 = time.time()
print("The cost is : %f second." %(t1 - t0))