I built a starbucks logo detector, but I get these strange artifacts when I draw the polylines that should surround the logo.
Here is the correct result:

Here are some examples of artifacts:

I use SIFT to detect key points and draw a rectangle as described in OpenCV tutorials, as shown here :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
sift = cv2.xfeatures2d.SIFT_create()
img1 = cv2.imread('logo.png', 0)
img1.resize(512, 512)
kp1, des1 = sift.detectAndCompute(img1, None)
while (True):
ret, frame = cap.read()
frame = findLogo(frame, kp1=kp1, des1=des1)
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
out.release()
cv2.destroyAllWindows()
def findLogo(frame, kp1, des1):
MIN_MATCH_COUNT = 10
sift = cv2.xfeatures2d.SIFT_create()
img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
kp2, des2 = sift.detectAndCompute(img2, None)
if len(kp2) != 0 and des2 is not None:
FLANN_INDEX_KDTREE = 0
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 = 512
w = 512
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
if M is not None:
dst = cv2.perspectiveTransform(pts, M)
frame = cv2.polylines(frame, [np.int32(dst)], True, (0, 255, 255), 3, cv2.LINE_AA)
else:
print("Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT))
matchesMask = None
return frame
I see that they occur when the program cannot detect the image. This program has lines to prevent this (and it works most of the time, for example, when there is nothing on the screen), but this error still occurs. Changing MIN_MATCH_COUNTto a larger number did not help. As you can see here:

, 37 . , , .
? ?