gpt4 book ai didi

python - SIFT 方法给我不好的结果,识别 4 种形状

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:11 25 4
gpt4 key购买 nike

我正在尝试识别具有开放式简历的扑克牌,但我遇到了一些问题。首先,我想识别颜色(红心、方 block 、黑桃或梅花)。我正盯着红色。所以我检测颜色,切割钻石或心形并尝试用筛选识别 - 我选择好的匹配和匹配的颜色会有更多(我敢肯定它很愚蠢,但我不知道如何做到这一点)。我得到如下结果:

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

如果您有任何建议,我将不胜感激。

最佳答案

您应该有 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)) 就是你寻找。

请注意,模型图像必须经过二值化处理,并且应该具有相同的大小。

希望这对您有所帮助!

关于python - SIFT 方法给我不好的结果,识别 4 种形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com