gpt4 book ai didi

python - 如何使用 python 比较一张图像和许多其他图像之间的 SSIM?

转载 作者:行者123 更新时间:2023-12-05 07:26:00 31 4
gpt4 key购买 nike

使用这个奇妙的页面:https://www.pyimagesearch.com/2014/09/15/python-compare-two-images/我能够在三张图片之间找到 SSIM

# import the necessary packages
from skimage.measure import structural_similarity as ssim
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv

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 = cv.imread("images/jp_gates_original.png")
contrast = cv.imread("images/jp_gates_contrast.png")
shopped = cv.imread("images/jp_gates_photoshopped.png")

# convert the images to grayscale
original = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
contrast = cv.cvtColor(contrast, cv.COLOR_BGR2GRAY)
shopped = cv.cvtColor(shopped, cv.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")

但是,我不太确定如何将其应用于许多图像。特别是,我如何从包含数百张图像的文件夹中提取一张图像(测试图像)并计算测试图像与所有其他图像之间的 MSE/SSIM?

谢谢!

最佳答案

您只想在不同的目录之间循环。这将与目录进行比较:first_path 和 second_path 以及它们之间的所有文件。

import os
import cv2

results = []
first_dir = os.fsencode(first_path)
second_dir = os.fsencode(second_path)

# Loop through all files in first directory
for first_file in os.listdir(first_dir):
first_filename = os.fsdecodoe(first_file)
first_filepath = os.path.join(os.fsdecode(first_dir), first_filename))
if first_filename.endswith(".your_extension"):

# Compare each file in second directory to each file in first directory
for second_file in os.listdir(second_dir):
second_filename = os.fsdecode(second_file)
second_filepath = os.path.join(os.fsdecode(second_dir), second_filename)
if second_filename.endswith(".your_extension"):
imageA = cv2.imread(first_filepath)
imageB = cv2.imread(second_filepath)
(score, diff) = ssim(imageA, imageB, full=True)
results.append((first_filepath, second_filepath, score))

没有运行,但它应该或多或少地让你得到你需要的东西。如果你只想做一个文件,那么取出第一个循环并将imageA = cv2.imread移到最前面。

关于python - 如何使用 python 比较一张图像和许多其他图像之间的 SSIM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589296/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com