gpt4 book ai didi

python-3.x - 特征匹配+单应性

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:55 26 4
gpt4 key购买 nike

我已经按照 python 中的 opencv 教程进行操作。我正在使用 OpenCV 3.2 和 Python 3.6.1。代码是这样写的:

import cv2
import numpy as np
import matplotlib.pyplot as plt

MIN_MATCH_COUNT = 10

img1 = cv2.imread('test.jpg',0)
img2 = cv2.imread('hanapin_mo.jpg',0)

sift = cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(des1, des2, k = 2)

good = []
for m,n in matches:
if m.distance <0.7*n.distance:
good.append(m)

if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)

M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()

h,w,d = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)

img2 = cv2.polylines(img2,[np.int32(dst)], True, 255, 3, cv2.LINE_AA)

else:
print("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT) )
matchesMask = None

draw_params = dict(matchColor = (0,250,0),
singlePointColor = None,
matchesMask = matchesMask,
flags = 2)

img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, **draw_params)

plt.imshow(img3, 'gray'),plt.show()

当我运行模块时,发生错误:

追溯(最近的调用最后): 文件“C:/Users/Albert Eli Reyes/Desktop/SIFT/SIFT.py”,第 35 行,位于 h,w,d = img1.形状ValueError:没有足够的值来解压(预期 3,得到 2)

    h,w,d = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)

img2 = cv2.polylines(img2,[np.int32(dst)], True, 255, 3, cv2.LINE_AA)

“没有足够的值来解压”是什么意思?我应该怎么做才能让它运行?

最佳答案

Shape 函数仅输出两个值的元组。您试图获取三个值,而该函数仅返回两个值

h,w = img1.shape

上面的方法行得通

关于python-3.x - 特征匹配+单应性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45129646/

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