筛选法给我的结果不好,识别出4个形状

2024-10-01 00:29:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用公开的简历来识别扑克牌,但我有一些问题。首先我想辨认颜色(红桃,钻石,黑桃或梅花)。我凝视着红色。所以我检测颜色,切割钻石或心脏,并尝试用sift识别-我选择好的匹配和匹配的颜色将有更多(我确定这是愚蠢的,但我不知道怎么做)。我得到的结果如下:

this

这是我的匹配函数的代码:

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

如果你有任何提示,我将不胜感激。


Tags: nameinnoneimgforifis颜色
1条回答
网友
1楼 · 发布于 2024-10-01 00:29:59

你应该有4个模型图像,每个形状一个。在

首先,你应该对颜色进行分类,这样你只需将图像与两个模型(心形/菱形或黑桃/梅花)进行比较。在

现在,因为颜色已经不重要了,你应该对图像进行二值化处理,高斯+大津就足够了,可以这样做:

# 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)

最后,我将使用2个模型图像对th进行特征匹配,得到更多特征匹配的图像(len(good))就是您正在寻找的。在

请注意,模型图像必须二值化,并且大小应该相同。在

希望这有帮助!在

相关问题 更多 >