Я пытаюсь рассчитать SSIM между соответствующими изображениями. Например, изображение с именем 106.tif в основном каталоге соответствует «поддельному» сгенерированному изображению 106.jpg в поддельном каталоге.
Абсолютный путь к каталогу истинной истины: /home/pr/pm/zh_pix2pix/datasets/mousebrain/test/B
Абсолютный путь поддельного каталога: /home/pr/pm/zh_pix2pix/output/fake_B
Изображения внутри соответствуют друг другу, вот так: увидеть изображение
Есть тысячи этих изображений, которые я хочу сравнить один к одному. Я не хочу сравнить SSIM одного изображения со многими другими. И соответствующие наземные изображения, и поддельные изображения имеют одинаковое имя файла, но разные расширения (например, 106.tif и 106.jpg), и я хочу только сравнить их друг с другом.
Я изо всех сил пытаюсь отредактировать доступные сценарии для сравнения SSIM таким образом. Я хочу использовать этот: https://github.com/mostafaGwely/Structural-Similarity-Index-SSIM-/blob/master/ssim.py, но другие предложения приветствуются. Код также показан ниже:
# Usage:
#
# python3 script.py --input original.png --output modified.png
# Based on: https://github.com/mostafaGwely/Structural-Similarity-Index-SSIM-
# 1. Import the necessary packages
#from skimage.measure import compare_ssim
from skimage.metrics import structural_similarity as ssim
import argparse
import imutils
import cv2
# 2. Construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True, help = "Directory of the image that will be compared")
ap.add_argument("-s", "--second", required=True, help = "Directory of the image that will be used to compare")
args = vars(ap.parse_args())
# 3. Load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
# 4. Convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
# 5. Compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
#(score, diff) = compare_ssim(grayA, grayB, full=True)
(score, diff) = ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
# 6. You can print only the score if you want
print("SSIM: {}".format(score))
Использование argparse в настоящее время ограничивает меня только одним изображением за раз, но в идеале я хотел бы сравнить их, используя цикл по реальным и поддельным каталогам. Любой совет будет принят во внимание.
Вот рабочий пример для сравнения одного изображения с другим. Вы можете расширить его, чтобы сравнить сразу несколько. Два тестовых входных изображения с небольшими отличиями:
Полученные результаты
Выделенные различия
Оценка сходства
Image similarity 0.9639027981846681
Разностные маски
Код
from skimage.metrics import structural_similarity
import cv2
import numpy as np
before = cv2.imread('5.jpg')
after = cv2.imread('6.jpg')
# Convert images to grayscale
before_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)
# Compute SSIM between two images
(score, diff) = structural_similarity(before_gray, after_gray, full=True)
print("Image similarity", score)
# The diff image contains the actual image differences between the two images
# and is represented as a floating point data type in the range [0,1]
# so we must convert the array to 8-bit unsigned integers in the range
# [0,255] before we can use it with OpenCV
diff = (diff * 255).astype("uint8")
# Threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
mask = np.zeros(before.shape, dtype='uint8')
filled_after = after.copy()
for c in contours:
area = cv2.contourArea(c)
if area > 40:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(before, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.rectangle(after, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.drawContours(mask, [c], 0, (0,255,0), -1)
cv2.drawContours(filled_after, [c], 0, (0,255,0), -1)
cv2.imshow('before', before)
cv2.imshow('after', after)
cv2.imshow('diff',diff)
cv2.imshow('mask',mask)
cv2.imshow('filled after',filled_after)
cv2.waitKey(0)
Это сработало отлично! Сим был всем, что мне было нужно, но остальное тоже было очень полезно. Изменено, чтобы включить цикл for, и он работал для нескольких изображений, которые у меня есть.