尝试使用cv2.DescriptorMatcher_create了点。
例如,在下面的代码我使用pylab,但你可以得到的消息;)
它计算使用GFTT的关键点,然后使用SURF描述符和蛮力匹配。 每个代码部分的输出显示为标题。
%pylab inline
import cv2
import numpy as np
img = cv2.imread('./img/nail.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imshow(gray, cmap=cm.gray)
输出是这样的http://i.stack.imgur.com/8eOTe.png
(在这个例子中,我将欺骗,并使用相同的图像,以获得关键点和描述符)。
img1 = gray
img2 = gray
detector = cv2.FeatureDetector_create("GFTT")
descriptor = cv2.DescriptorExtractor_create("SURF")
matcher = pt1=(int(k1[m.queryIdx].pt[0]),int(k1[m.queryIdx].pt[1]))("FlannBased")
# detect keypoints
kp1 = detector.detect(img1)
kp2 = detector.detect(img2)
print '#keypoints in image1: %d, image2: %d' % (len(kp1), len(kp2))
关键点在图像1:1000,图像2:1000
# descriptors
k1, d1 = descriptor.compute(img1, kp1)
k2, d2 = descriptor.compute(img2, kp2)
print '#Descriptors size in image1: %s, image2: %s' % ((d1.shape), (d2.shape))
描述符尺寸在图像1:(1000,64),图像2:(1000,64)
# match the keypoints
matches = matcher.match(d1,d2)
# visualize the matches
print '#matches:', len(matches)
dist = [m.distance for m in matches]
print 'distance: min: %.3f' % min(dist)
print 'distance: mean: %.3f' % (sum(dist)/len(dist))
print 'distance: max: %.3f' % max(dist)
匹配: 1000
距离:最小:0.000
距离:均值:0.000
距离:最大:0.000
# threshold: half the mean
thres_dist = (sum(dist)/len(dist)) * 0.5 + 0.5
# keep only the reasonable matches
sel_matches = [m for m in matches if m.distance < thres_dist]
print '#selected matches:', len(sel_matches)
选择的比赛:1000
#Plot
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
view = zeros((max(h1, h2), w1 + w2, 3), uint8)
view[:h1, :w1, 0] = img1
view[:h2, w1:, 0] = img2
view[:, :, 1] = view[:, :, 0]
view[:, :, 2] = view[:, :, 0]
for m in sel_matches:
# draw the keypoints
# print m.queryIdx, m.trainIdx, m.distance
color = tuple([random.randint(0, 255) for _ in xrange(3)])
pt1=(int(k1[m.queryIdx].pt[0]),int(k1[m.queryIdx].pt[1]))
pt2=(int(k2[m.queryIdx].pt[0]+w1),int(k2[m.queryIdx].pt[1]))
cv2.line(view,pt1,pt2,color)
输出是这样的http://i.stack.imgur.com/8CqrJ.png
你奶源得到它的工作结束 ? – OddNorg 2015-08-04 14:24:59
我做过了,我甚至在这里发布了答案,但我只注意到它由于某种原因被删除了。奇怪。无论如何,您可以使用[Mahotas](http://luispedro.org/software/mahotas/)来执行此操作,或者查看其他同时发布的其他答案。 – casper 2015-08-04 17:25:16