我正在尝试识别打开cv的扑克牌,但我遇到了一些问题。首先,我想要识别颜色(心,钻石,黑桃或俱乐部)。我正盯着红色。因此,我检测颜色,切割钻石或心脏,并尝试用筛选识别 - 我选择好的匹配和匹配颜色将有更多(我敢肯定它的愚蠢,但我不知道如何做到这一点)。我得到的结果如下:
这是我的匹配功能的代码:
def match(img, models):
goods = []
for name, value in models:
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(name, None)
kp2, des2 = sift.detectAndCompute(img, None)
if des1 is None or des2 is None:
continue
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
if matches is None:
continue
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append([m])
img3 = cv2.drawMatchesKnn(img, kp1, name, kp2, good, None, flags=2)
plt.imshow(img3), plt.show()
goods.append([len(good), value])
maxi = 0
ret = None
for l, v in goods:
if l > maxi:
maxi = l
ret = v
if maxi < 3:
return 0
return ret
如果您有任何提示,我将不胜感激。
答案 0 :(得分:0)
你应该有4个模特图像,每个形状一个。
首先,您应该对颜色进行分类,因此您只需要将图像与2个模型(心形/菱形或铁锹/球杆)进行比较。
现在,由于颜色不再重要,你应该对图像进行二值化,Gauss + Otsu就足够了,可以按照以下方式完成:
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret, th = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
最后,我会将th
的特征匹配与2个模型图像进行匹配,获得更多特征数学(len(good)
)的特征匹配是您正在寻找的。 p>
请注意,模型图像必须二进制化,并且大小必须相同。
希望这有帮助!