Wie zu überprüfen, ob ein Punkt innerhalb eines Satzes von Konturen ist
Hallo an alle. Das obige Bild ist eine Summe von zwei Bildern, in denen ich eine Übereinstimmung gefunden habe und alle passenden Punkte gezeichnet habe. Ich fand auch die Konturen der PCB-Teile im ersten Bild (halb links Bild-3 Konturen). Die Frage ist, wie könnte ich nur die übereinstimmenden Punkte zeichnen, die innerhalb dieser Konturen im ersten Bild statt dieser blauen Unordnung sind? Ich benutze Python 2.7 und opencv 2.4.12.
Ich schrieb dazu führen, eine Funktion zur Auslosung Spielen in opencv 2.4.12 es kein implementiertes Verfahren dafür. Wenn ich etwas nicht aufgenommen habe, sag es mir bitte. Vielen Dank im Voraus!
import numpy as np
import cv2
def drawMatches(img1, kp1, img2, kp2, matches):
# Create a new output image that concatenates the two images
# (a.k.a) a montage
rows1 = img1.shape[0]
cols1 = img1.shape[1]
rows2 = img2.shape[0]
cols2 = img2.shape[1]
# Create the output image
# The rows of the output are the largest between the two images
# and the columns are simply the sum of the two together
# The intent is to make this a colour image, so make this 3 channels
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:] = 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.imwrite("shift_points.png", out)
cv2.waitKey(0)
cv2.destroyWindow('Matched Features')
# Also return the image if you'd like a copy
return out
img1 = cv2.imread('pic3.png', 0) # Original image - ensure grayscale
img2 = cv2.imread('pic1.png', 0) # Rotated image - ensure grayscale
sift = cv2.SIFT()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# Create matcher
bf = cv2.BFMatcher()
# Perform KNN matching
matches = bf.knnMatch(des1, des2, k=2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
# Add first matched keypoint to list
# if ratio test passes
good.append(m)
# Show only the top 10 matches - also save a copy for use later
out = drawMatches(img1, kp1, img2, kp2, good)
Ich habe eine große Menge von Punkten gegeben von Sift und Feature-Matching und ich möchte nur diejenigen innerhalb der Konturen, die ich gefunden habe, zu zeichnen. Ich habe irgendwo die Methode gesehen, die Sie vorgeschlagen haben, aber ich konnte nicht verstehen, wie ich sie umsetzen soll. Ich werde den angehängten Artikel überprüfen und versuchen, es zu erstellen. Wenn Sie die Zeit haben und wissen, wie es geht, legen Sie einen Beispielcode, der mir vielleicht helfen könnte. Wie auch immer, danke! –
Übrigens sind meine Konturen Rechtecke –
Ich habe meine Antwort mit mehr Informationen bearbeitet – Draco