这是我的代码,我得到了一个AttributeError:"tuple"对象没有属性"sort".我正在try 进行图像对齐,并在一篇文章中找到了这个标准的图像对齐代码.我正在学习openCV和python,这也是我真正的新手,我现在能够用openCV做基本的事情,我正在try 学习图像对齐,我被困在这一部分.

Traceback (most recent call last):
  File "test9.py", line 31, in <module>
    matches.sort(key = lambda x: x.distance)
AttributeError: 'tuple' object has no attribute 'sort'


------------------
(program exited with code: 1)
Press return to continue


import cv2
import numpy as np

# Open the image files.
img1_color = cv2.imread("/home/pi/Desktop/Project AOI/testboard1/image_2.jpg") # Image to be aligned.
img2_color = cv2.imread("/home/pi/Desktop/Project AOI/testboard1/image_0.jpg") # Reference image.

# Convert to grayscale.
img1 = cv2.cvtColor(img1_color, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY)
height, width = img2.shape

# Create ORB detector with 5000 features.
orb_detector = cv2.ORB_create(5000)

# Find keypoints and descriptors.
# The first arg is the image, second arg is the mask
# (which is not required in this case).
kp1, d1 = orb_detector.detectAndCompute(img1, None)
kp2, d2 = orb_detector.detectAndCompute(img2, None)

# Match features between the two images.
# We create a Brute Force matcher with
# Hamming distance as measurement mode.
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Match the two sets of descriptors.
matches = matcher.match(d1, d2)

# Sort matches on the basis of their Hamming distance.
matches.sort(key = lambda x: x.distance)

# Take the top 90 % matches forward.
matches = matches[:int(len(matches)*0.9)]
no_of_matches = len(matches)

# Define empty matrices of shape no_of_matches * 2.
p1 = np.zeros((no_of_matches, 2))
p2 = np.zeros((no_of_matches, 2))

for i in range(len(matches)):
    p1[i, :] = kp1[matches[i].queryIdx].pt
    p2[i, :] = kp2[matches[i].trainIdx].pt

# Find the homography matrix.
homography, mask = cv2.findHomography(p1, p2, cv2.RANSAC)

# Use this matrix to transform the
# colored image wrt the reference image.
transformed_img = cv2.warpPerspective(img1_color,
                    homography, (width, height))

# Save the output.
cv2.imwrite('output.jpg', transformed_img)

推荐答案

你得到的是tuple分,不是list分.你不能只有matches.sort(...)分.

自v4.5.4以来,OpenCV在其Python绑定生成中表现出这种行为.

您必须使用此选项:

matches = sorted(matches, ...)

这将创建一个新列表,其中包含原始元组的排序元素.

相关问题:

Python相关问答推荐

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

在Python Attrs包中,如何在field_Transformer函数中添加字段?

图像 pyramid .难以创建所需的合成图像

PMMLPipeline._ fit()需要2到3个位置参数,但给出了4个位置参数

如何在python polars中停止otherate(),当使用when()表达式时?

我们可以为Flask模型中的id字段主键设置默认uuid吗

多指标不同顺序串联大Pandas 模型

Polars asof在下一个可用日期加入

python中csv. Dictreader. fieldname的类型是什么?'

为什么'if x is None:pass'比'x is None'单独使用更快?

在我融化极点数据帧之后,我如何在不添加索引的情况下将其旋转回其原始形式?

来自Airflow Connection的额外参数

为什么在Python中00是一个有效的整数?

Groupby并在组内比较单独行上的两个时间戳

如何删除剪裁圆的对角线的外部部分

将时间序列附加到数据帧

如何定义一个将类型与接收该类型的参数的可调用进行映射的字典?