また、OpenCV Python インターフェイスのみを使用するものを自分で作成しましたが、使用しませんでしscipy
た。 drawMatches
OpenCV 3.0.0 の一部であり、現在使用している OpenCV 2 の一部ではありません。私はパーティーに遅れましたが、これdrawMatches
は私の能力を最大限に模倣した私自身の実装です。
1 つはカメラマンのもので、もう 1 つは同じ画像ですが、反時計回りに 55 度回転した独自の画像を提供しました。
私が書いたものの基本的な前提は、出力画像に両方の画像を配置するために行の量が2つの画像の最大であり、列が単に両方の列の合計である出力RGB画像を割り当てることです一緒。各画像を対応するスポットに配置し、一致したすべてのキーポイントのループを実行します。2 つの画像間で一致するキーポイントを抽出し、それら(x,y)
の座標を抽出します。次に、検出された各位置に円を描き、これらの円を結ぶ線を描きます。
2 番目の画像で検出されたキーポイントは、独自の座標系を基準にしていることに注意してください。これを最終出力画像に配置する場合は、列座標が出力画像の座標系に関連するように、最初の画像から列の量だけ列座標をオフセットする必要があります。 .
難しい話は抜きにして:
import numpy as np
import cv2
def drawMatches(img1, kp1, img2, kp2, matches):
"""
My own implementation of cv2.drawMatches as OpenCV 2.4.9
does not have this function available but it's supported in
OpenCV 3.0.0
This function takes in two images with their associated
keypoints, as well as a list of DMatch data structure (matches)
that contains which keypoints matched in which images.
An image will be produced where a montage is shown with
the first image followed by the second image beside it.
Keypoints are delineated with circles, while lines are connected
between matching keypoints.
img1,img2 - Grayscale images
kp1,kp2 - Detected list of keypoints through any of the OpenCV keypoint
detection algorithms
matches - A list of matches of corresponding keypoints through any
OpenCV keypoint matching algorithm
"""
# Create a new output image that concatenates the two images together
# (a.k.a) a montage
rows1 = img1.shape[0]
cols1 = img1.shape[1]
rows2 = img2.shape[0]
cols2 = img2.shape[1]
out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8')
# Place the first image to the left
out[:rows1,:cols1,:] = np.dstack([img1, img1, img1])
# Place the next image to the right of it
out[:rows2,cols1:cols1+cols2,:] = np.dstack([img2, img2, img2])
# For each pair of points we have between both images
# draw circles, then connect a line between them
for mat in matches:
# Get the matching keypoints for each of the images
img1_idx = mat.queryIdx
img2_idx = mat.trainIdx
# x - columns
# y - rows
(x1,y1) = kp1[img1_idx].pt
(x2,y2) = kp2[img2_idx].pt
# Draw a small circle at both co-ordinates
# radius 4
# colour blue
# thickness = 1
cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0), 1)
cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0), 1)
# Draw a line in between the two points
# thickness = 1
# colour blue
cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), (255, 0, 0), 1)
# Show the image
cv2.imshow('Matched Features', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
これが機能することを示すために、使用した 2 つの画像を次に示します。


OpenCV の ORB 検出器を使用してキーポイントを検出し、正規化されたハミング距離を類似性の距離尺度として使用しました。これはバイナリ記述子であるためです。そのような:
import numpy as np
import cv2
img1 = cv2.imread('cameraman.png') # Original image
img2 = cv2.imread('cameraman_rot55.png') # Rotated image
# Create ORB detector with 1000 keypoints with a scaling pyramid factor
# of 1.2
orb = cv2.ORB(1000, 1.2)
# Detect keypoints of original image
(kp1,des1) = orb.detectAndCompute(img1, None)
# Detect keypoints of rotated image
(kp2,des2) = orb.detectAndCompute(img2, None)
# Create matcher
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Do matching
matches = bf.match(des1,des2)
# Sort the matches based on distance. Least distance
# is better
matches = sorted(matches, key=lambda val: val.distance)
# Show only the top 10 matches
drawMatches(img1, kp1, img2, kp2, matches[:10])
これは私が得るイメージです:
