我构建了一个星巴克 Logo 检测器,但是当我绘制应该围绕 Logo 的多段线时,我得到了这些奇怪的人工制品。
这是正确的结果:
这里有一些人工制品的例子:
我正在使用 SIFT 来检测关键点并按照 OpenCV 教程中的教学绘制矩形 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
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# find the keypoints and descriptors with SIFT
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)
# store all the good matches as per Lowe's ratio test.
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 = img1.shape
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
我发现它们是在程序无法检测到图像时发生的。程序中有几行代码可以防止这种情况发生(大多数情况下它确实有效,就像屏幕上没有任何内容一样),但此错误仍在发生。将 MIN_MATCH_COUNT
更改为更大的数字没有帮助。正如您在这里看到的:
即使有 37 个良好匹配,也会出现人工制品。将其设置得太高意味着除非手机完全静止,否则程序将找不到 Logo 。
我怎样才能摆脱那些人工制品?有什么想法吗?
最佳答案
变形 轮廓是由于未正确计算单应性造成的。
当您要查找的对象具有矩形时,这是一种丢弃不良轮廓的方法:
- 找到轮廓的最短和最长边,并计算两边之间的相对间隙:|最长 - 最短 |/最长。如果相对差距太大,则可能意味着轮廓不好。
现在,有四种方法可以处理任何轮廓(不仅仅是矩形轮廓):
- 为轮廓区域设置阈值(可以使用
contourArea
计算)。 轮廓的区域既不能太小也不能太大。 - 检查
findHomography
给出的内点的数量功能。如果这个数字低于某个阈值(例如 10),则丢弃该轮廓。 - 检查轮廓的任何边缘是否相交任何其他边缘。如果是这样,那么你可以确定轮廓是坏的。
- 你也可以试试这个 function读取 homography 矩阵 values 并返回 true 或 false 无论单应性是否“nice”。 (您可能需要根据需要调整函数内的值)。
丢弃所有不良轮廓不是一件容易的事,但是通过使用几种上面解释的方法,您应该摆脱大多数错误的轮廓。
关于python - 我正在尝试检测场景中的星巴克标志。我如何摆脱边界多段线中的这些人工制品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45794473/