niqe的python代码
时间: 2023-12-15 09:31:20 浏览: 200
以下是使用Python实现NIQE的代码:
```python
import numpy as np
import cv2
import torch
from torch.autograd import Variable
from scipy import signal
def niqe(img):
# 读取图像并转换为灰度图像
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.float32(img) / 255.0
# 计算局部方差
mu = signal.convolve2d(img, np.ones((8, 8)), mode='same', boundary='symm') / 64.0
mu_sq = mu * mu
sigma = np.sqrt(abs(signal.convolve2d(img * img, np.ones((8, 8)), mode='same', boundary='symm') / 64.0 - mu_sq))
# 计算局部对比度
structdis = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
mu = cv2.morphologyEx(mu, cv2.MORPH_OPEN, structdis)
sigma = cv2.morphologyEx(sigma, cv2.MORPH_OPEN, structdis)
lpc = sigma / mu
# 计算NIQE
niqe_features = []
niqe_features.append(np.mean(lpc))
niqe_features.append(np.std(lpc))
niqe_features.append(np.mean(sigma))
niqe_features.append(np.std(sigma))
niqe_features = np.array(niqe_features)
niqe_features = np.reshape(niqe_features, (1, niqe_features.shape[0]))
# 加载NIQE模型并计算NIQE分数
model = torch.load('niqe/niqe_model.pth')
niqe_features = Variable(torch.FloatTensor(niqe_features))
niqe_score = model(niqe_features)
return niqe_score.item()
```
阅读全文