python计算图像结构相似度(SSIM)实例_图像相似度计算_图像质量评价

scient

scient一个用python实现科学计算相关算法的包,包括自然语言、图像、神经网络、优化算法、机器学习、图计算等模块。

scient源码和编译安装包可以在Python package index获取。

The source code and binary installers for the latest released version are available at the [Python package index].

https://pypi.org/project/scient

可以用pip安装scient

You can install scient like this:

pip install scient

也可以用setup.py安装。

Or in the scient directory, execute:

python setup.py install

scient.image

图像相关算法模块,包括边缘检测、图像相似度计算、图像质量评价、图像特征提取等。

scient.image.friqa

全参考图像质量评价模块,包括峰值信噪比(PSNR),结构相似度(SSIM),直方图相似度(HistSim)。

scient.image.friqa.ssim(image1,image2,k1=0.01,k2=0.03,block_size=(8, 8),max_pix=255)

Parameters

image1 : numpy.array 2D

image2 : numpy.array 2D

k1 : float, optional,k1<<1,避免分母为0造成不稳定. The default is 0.01.

k2 : float, optional,k2<<1,避免分母为0造成不稳定. The default is 0.03.

block_size : tuple, optional,将图像分成多个block,采用gaussian加权计算所有block的均值、方差、协方差,进而计算所有block的ssim,最后的ssim取所有block的平均值. The default is (8,8).

max_pix : int, optional default is 255, 像素值的最大值,默认值是255.

Returns

float

Algorithms

SSIM(Structural Similarity),结构相似度,用于衡量两个图像相似程度,或检测图像的失真程度。
SSIM基于样本之间的亮度(luminance,像素平均值)、对比度(contrast,像素标准差)和结构(structure,像素减均值除以标准差)计算。

S S I M ( x , y ) = f ( l ( x , y ) , c ( x , y ) , s ( x , y ) ) SSIM(x,y)=f(l(x,y),c(x,y),s(x,y)) SSIM(x,y)=f(l(x,y),c(x,y),s(x,y))

l ( x , y ) l(x,y) l(x,y)为亮度对比函数,是关于图像的平均灰度 μ x , μ y μ_x,μ_y μx,μy的函数;

l ( x , y ) = 2 μ x μ y + C 1 μ x 2 μ y 2 + C 1 μ x = 1 N ∑ i = 1 N x i C 1 = ( K 1 L ) 2 l(x,y)=\frac { 2μ_x μ_y + C1 } { μ_x^2 μ_y^2 + C1 } \\ μ_x=\frac { 1 } { N } \sum^{N}_{i=1}{x_i} \\ C1=(K_1 L)^2 l(x,y)=μx2μy2+C12μxμy+C1μx=N1i=1NxiC1=(K1L)2

像素值的最大值,默认值是255. K1<<1。

c ( x , y ) c(x,y) c(x,y)为对比度对比函数,是关于图像的标准差 σ x , σ y σ_x,σ_y σx,σy的函数;

c ( x , y ) = 2 σ x σ y + C 2 σ x 2 σ y 2 + C 2 σ x = ( 1 N − 1 ∑ i = 1 N ( x i − μ x ) 2 ) 1 2 C 2 = ( K 2 L ) 2 c(x,y)=\frac { 2σ_x σ_y + C2 } { σ_x^2 σ_y^2 + C2 } \\ σ_x=(\frac { 1 } { N-1 } \sum^{N}_{i=1}{(x_i-μ_x)^2})^{\frac { 1 } { 2 }} \\ C2=(K_2 L)^2 c(x,y)=σx2σy2+C22σxσy+C2σx=(N11i=1N(xiμx)2)21C2=(K2L)2

K2<<1

s ( x , y ) s(x,y) s(x,y)为结构对比函数,是关于图像的标准化 x − μ x σ x , y − μ y σ y \frac { x-μ_x } { σ_x },\frac { y-μ_y } { σ_y } σxxμx,σyyμy的函数;

s ( x , y ) = σ x y + C 3 σ x σ y + C 3 σ x y = 1 N − 1 ( ∑ i = 1 N ( x i − μ x ) ( y i − μ y ) ) s(x,y)=\frac { σ_{xy} + C3 } { σ_x σ_y + C3 } \\ σ_{xy}=\frac { 1 } { N-1 } (\sum^{N}_{i=1}{(x_i-μ_x)(y_i-μ_y)}) \\ s(x,y)=σxσy+C3σxy+C3σxy=N11(i=1N(xiμx)(yiμy))

S S I M ( x , y ) = [ l ( x , y ) ] α [ c ( x , y ) ] β [ s ( x , y ) ] γ SSIM(x,y)=[l(x,y)]^α[c(x,y)]^β[s(x,y)]^γ SSIM(x,y)=[l(x,y)]α[c(x,y)]β[s(x,y)]γ

α,β,γ取1,令 C 3 = C 2 2 C_3=\frac { C_2 } { 2 } C3=2C2,可将SSIM简化为:

S S I M ( x , y ) = ( 2 μ x μ y + C 1 ) ( 2 σ x y + C 2 ) ( μ x 2 μ y 2 + C 1 ) ( σ x 2 σ y 2 + C 2 ) SSIM(x,y)=\frac { (2μ_x μ_y + C1)(2σ_{xy} + C2) } { (μ_x^2 μ_y^2 + C1)(σ_x^2 σ_y^2 + C2) } SSIM(x,y)=(μx2μy2+C1)(σx2σy2+C2)(2μxμy+C1)(2σxy+C2)

SSIM取值范围为[0,1],值越大表示图像质量越好。
SSIM具有:对称性,ssim(x,y)=ssim(y,x);
有界性,ssim(x,y)<=1;
最大值唯一性,当且仅当x==y时,ssim(x,y)==1。
SSIM缺点:对于图像出现位移、缩放、旋转(皆属于非结构性的失真)的情况无法有效的判断。

Examples

import os
from scient.image import friqa
import numpy
from PIL import Image

ref_image='test/data/I10.BMP'
images=['test/data/I10.BMP','test/data/i10_23_3.bmp','test/data/i10_23_4.bmp','test/data/i10_23_5.bmp','test/data/i10_24_5.bmp']

#读取图像文件
ref_image=Image.open(os.path.join(os.path.dirname(friqa.__file__),'..',ref_image))
images=[Image.open(os.path.join(os.path.dirname(friqa.__file__),'..',i)) for i in images]

#计算ssim
for i in images:
    print(friqa.ssim(numpy.array(ref_image.convert("L")),numpy.array(i.convert("L"))))

运行结果

1.0
0.8568124416229375
0.6810351495300123
0.5575398637742431
0.5072153083460104
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值