如何使用Python OpenCV将2张图像转换为相同尺寸?

问题描述 投票:0回答:1

cv2.error:OpenCV(4.8.1):-1:错误:(-5:错误参数)在函数“resize”中

重载解析失败:

  • 无法解析“dsize”。期望序列长度2,得到3
  • 无法解析“dsize”。预期序列长度2,得到3 如何将图像转换为相同尺寸?
from skimage.metrics import structural_similarity
import cv2
import numpy as np

first = cv2.imread('1.png')
second = cv2.imread('3.png')

secondasnparray = np.asarray(second, dtype=int) # How to change this code properly???
second =cv2.resize(secondasnparray,first.shape)

将图像转换为灰度

first_gray = cv2.cvtColor(first, cv2.COLOR_BGR2GRAY)
second_gray = cv2.cvtColor(second, cv2.COLOR_BGR2GRAY)

计算两个图像之间的SSIM

score, diff = structural_similarity(first_gray, second_gray, full=True)
print("Similarity Score: {:.3f}%".format(score * 100))
python-3.x image image-processing
1个回答
0
投票

以下代码解决了问题:

从 skimage.metrics 导入结构相似度 导入CV2 将 numpy 导入为 np

首先 = cv2.imread('1.png') 第二个 = cv2.imread('3.png')

firstresized = cv2.resize(second,first.shape[:2]) # 制作相同尺寸的图像 Secondresized = cv2.resize(first,first.shape[:2])

© www.soinside.com 2019 - 2024. All rights reserved.