目录
cannot import name ‘compare_ssim‘
multichannel警告:
代码:
from skimage.measure import compare_ssim
报错:
Traceback (most recent call last):
File "D:/project/detect/face/Yolov5_DeepSort_Pytorch/nahua.py", line 7, in
from skimage.measure import compare_ssim
ImportError: cannot import name 'compare_ssim' from 'skimage.measure' (D:\ProgramData\Miniconda3\lib\site-packages\skimage\measure\__init__.py)
解决方法:
from skimage.metrics import structural_similarity as ssim
ssim_v = ssim(img2, template, multichannel=True)
FutureWarning: `multichannel` is a deprecated argument name for `structural_similarity`. It will be removed in version 1.0. Please use `channel_axis` instead.
ssim = compare_ssim(a, b, multichannel=True)
解决方法:
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
import math
import sys
from skimage import io, color, filters
import os
import math
def rmetrics(a, b):
# pnsr
mse = np.mean((a - b) ** 2)
psnr = 10 * math.log10(1 / mse)
# ssim
ssim = compare_ssim(a, b, channel_axis=2)
# ssim = compare_ssim(a, b, full=True)
return psnr, ssim
def nmetrics(a):
rgb = a
lab = color.rgb2lab(a)
gray = color.rgb2gray(a)
# UCIQE
c1 = 0.4680
c2 = 0.2745
c3 = 0.2576
l = lab[:, :, 0]
# 1st term
chroma = (lab[:, :, 1] ** 2 + lab[:, :, 2] ** 2) ** 0.5
uc = np.mean(chroma)
sc = (np.mean((chroma - uc) ** 2)) ** 0.5
# 2nd term
top = np.int32(np.round(0.01 * l.shape[0] * l.shape[1]))
sl = np.sort(l, axis=None)
isl = sl[::-1]
conl = np.mean(isl[:top]) - np.mean(sl[:top])
# 3rd term
satur = []
chroma1 = chroma.flatten()
l1 = l.flatten()
for i in range(len(l1)):
if chroma1[i] == 0:
satur.append(0)
elif l1[i] == 0:
satur.append(0)
else:
satur.append(chroma1[i] / l1[i])
us = np.mean(satur)
uciqe = c1 * sc + c2 * conl + c3 * us
# UIQM
p1 = 0.0282
p2 = 0.2953
p3 = 3.5753
# 1st term UICM
rg = rgb[:, :, 0] - rgb[:, :, 1]
yb = (rgb[:, :, 0] + rgb[:, :, 1]) / 2 - rgb[:, :, 2]
rgl = np.sort(rg, axis=None)
ybl = np.sort(yb, axis=None)
al1 = 0.1
al2 = 0.1
T1 = np.int32(al1 * len(rgl))
T2 = np.int32(al2 * len(rgl))
rgl_tr = rgl[T1:-T2]
ybl_tr = ybl[T1:-T2]
urg = np.mean(rgl_tr)
s2rg = np.mean((rgl_tr - urg) ** 2)
uyb = np.mean(ybl_tr)
s2yb = np.mean((ybl_tr - uyb) ** 2)
uicm = -0.0268 * np.sqrt(urg ** 2 + uyb ** 2) + 0.1586 * np.sqrt(s2rg + s2yb)
# 2nd term UISM (k1k2=8x8)
Rsobel = rgb[:, :, 0] * filters.sobel(rgb[:, :, 0])
Gsobel = rgb[:, :, 1] * filters.sobel(rgb[:, :, 1])
Bsobel = rgb[:, :, 2] * filters.sobel(rgb[:, :, 2])
Rsobel = np.round(Rsobel).astype(np.uint8)
Gsobel = np.round(Gsobel).astype(np.uint8)
Bsobel = np.round(Bsobel).astype(np.uint8)
Reme = eme(Rsobel)
Geme = eme(Gsobel)
Beme = eme(Bsobel)
uism = 0.299 * Reme + 0.587 * Geme + 0.114 * Beme
# 3rd term UIConM
uiconm = logamee(gray)
uiqm = p1 * uicm + p2 * uism + p3 * uiconm
return uiqm, uciqe
def eme(ch, blocksize=8):
num_x = math.ceil(ch.shape[0] / blocksize)
num_y = math.ceil(ch.shape[1] / blocksize)
eme = 0
w = 2. / (num_x * num_y)
for i in range(num_x):
xlb = i * blocksize
if i < num_x - 1:
xrb = (i + 1) * blocksize
else:
xrb = ch.shape[0]
for j in range(num_y):
ylb = j * blocksize
if j < num_y - 1:
yrb = (j + 1) * blocksize
else:
yrb = ch.shape[1]
block = ch[xlb:xrb, ylb:yrb]
blockmin = np.float32(np.min(block))
blockmax = np.float32(np.max(block))
# # old version
# if blockmin == 0.0: eme += 0
# elif blockmax == 0.0: eme += 0
# else: eme += w * math.log(blockmax / blockmin)
# new version
if blockmin == 0: blockmin += 1
if blockmax == 0: blockmax += 1
eme += w * math.log(blockmax / blockmin)
return eme
def plipsum(i, j, gamma=1026):
return i + j - i * j / gamma
def plipsub(i, j, k=1026):
return k * (i - j) / (k - j)
def plipmult(c, j, gamma=1026):
return gamma - gamma * (1 - j / gamma) ** c
def logamee(ch, blocksize=8):
num_x = math.ceil(ch.shape[0] / blocksize)
num_y = math.ceil(ch.shape[1] / blocksize)
s = 0
w = 1. / (num_x * num_y)
for i in range(num_x):
xlb = i * blocksize
if i < num_x - 1:
xrb = (i + 1) * blocksize
else:
xrb = ch.shape[0]
for j in range(num_y):
ylb = j * blocksize
if j < num_y - 1:
yrb = (j + 1) * blocksize
else:
yrb = ch.shape[1]
block = ch[xlb:xrb, ylb:yrb]
blockmin = np.float32(np.min(block))
blockmax = np.float32(np.max(block))
top = plipsub(blockmax, blockmin)
bottom = plipsum(blockmax, blockmin)
m = top / bottom
if m == 0.:
s += 0
else:
s += (m) * np.log(m)
return plipmult(w, s)
if __name__ == '__main__':
img_1 =r'F:\project\wendu\zengqiang_s\test\input\16_img_.jpg' #读取图片
img_2 = r'F:\project\wendu\zengqiang_s\test\target\16_img_.jpg'
corrected = io.imread(img_1)
reference = io.imread(img_2)
psnr, ssim = rmetrics(corrected, reference)
uiqm, uciqe = nmetrics(corrected)
print('Average: psnr={} ssim={} uiqm={} uciqe={}\n'.format(psnr, ssim, uiqm, uciqe))