Opencv学习笔记 均方误差(MSE)、结构相似度指数(SSIM)

        均方误差(mean-square error, MSE)是反映估计量与被估计量之间差异程度的一种度量。设t是根据子样确定的总体参数θ的一个估计量,(θ-t)2的数学期望,称为估计量t的均方误差。它等于σ2+b2,其中σ2与b分别是t的方差与偏倚。

 

公式1:均方误差

        SSIM(Structural SIMilarity),结构相似性,是一种衡量两幅图像相似度的指标。该指标首先由德州大学奥斯丁分校的图像和视频工程实验室(Laboratory for Image and Video Engineering)提出。SSIM使用的两张图像中,一张为未经压缩的无失真图像,另一张为失真后的图像。

公式2:结构相似性指数

        参考代码:

#import skimage.measure
from skimage.metrics import structural_similarity as ssim
# from skimage import measure
import matplotlib.pyplot as plt
import numpy as np
import cv2


def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err

def compare_images(imageA, imageB, title):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(imageA, imageB)
    s = ssim(imageA, imageB)
    # setup the figure
    fig = plt.figure(title)
    plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
    # show first image
    ax = fig.add_subplot(1, 2, 1)
    plt.imshow(imageA, cmap=plt.cm.gray)
    plt.axis("off")
    # show the second image
    ax = fig.add_subplot(1, 2, 2)
    plt.imshow(imageB, cmap=plt.cm.gray)
    plt.axis("off")
    # show the images
    plt.show()

# load the images -- the original, the original + contrast,
# and the original + photoshop
original = cv2.imread("C:/Users/Desktop/111.png")
contrast = cv2.imread("C:/Users/Desktop/222.png")
shopped = cv2.imread("C:/Users/Desktop/333.png")
# convert the images to grayscale
original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
contrast = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)
shopped = cv2.cvtColor(shopped, cv2.COLOR_BGR2GRAY)

# initialize the figure
fig = plt.figure("Images")
images = ("Original", original), ("Contrast", contrast), ("Photoshopped", shopped)
# loop over the images
for (i, (name, image)) in enumerate(images):
	# show the image
	ax = fig.add_subplot(1, 3, i + 1)
	ax.set_title(name)
	plt.imshow(image, cmap = plt.cm.gray)
	plt.axis("off")
# show the figure
plt.show()
# compare the images
compare_images(original, original, "Original vs. Original")
compare_images(original, contrast, "Original vs. Contrast")
compare_images(original, shopped, "Original vs. Photoshopped")

 

Opencv学习笔记 均方误差(MSE)、结构相似度指数(SSIM)_第1张图片

Opencv学习笔记 均方误差(MSE)、结构相似度指数(SSIM)_第2张图片

 

Opencv学习笔记 均方误差(MSE)、结构相似度指数(SSIM)_第3张图片

Opencv学习笔记 均方误差(MSE)、结构相似度指数(SSIM)_第4张图片

        遇到import structural_similarity错误:

        旧:from skimage.measure import structural_similarity as ssim

        新:from skimage.metrics import structural_similarity as ssim

        参考文章:        

        https://www.cnblogs.com/darkchii/p/12679103.html

        https://blog.csdn.net/weixin_33937778/article/details/88667697

        https://blog.csdn.net/chaipp0607/article/details/70160307

你可能感兴趣的:(OpenCv,图像处理,opencv,python)