简体   繁体   English

OpenCV Pawn Chess棋子没有检测到?

[英]OpenCV Pawn Chess piece is not detecting?

I having a trouble to detect the pawn white chess piece. 我在检测棋子白色棋子时遇到麻烦。 It is a bit odd because it detects every second white pawn piece. 这有点奇怪,因为它每隔两秒就检测一次白色典当。 If I play the undetected pawn piece, it get detected. 如果我播放未检测到的棋子,它将被检测到。

Thats the reason why I am asking in Stackoverflow because I can't find any help or can't explain to myself. 这就是为什么我在Stackoverflow中询问的原因,因为我找不到任何帮助或无法向自己解释。

General Information: I am playing at Lichess with all default Setting and Styles. 一般信息:我在Lichess玩所有默认设置和样式。 Also my white pawn picture is an transperant white png file (50px x 50px). 我的白色典当图片也是透明的白色png文件(50像素x 50像素)。 The threshhold is on 0.6 because I get the best result. 阈值是0.6,因为我得到了最好的结果。 If I decrease the number it get less detected white pawn and if I increase the number it detect black pawn pieces. 如果我减少数字,则得到较少的白色典当;如果我增加数字,则将发现黑色的典当。

# Standard settings
img_rgb = cv2.cvtColor(original_img,cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)

# White Pawn Template
pawn_white_template = cv2.imread("chess_pieces_template/pawn_white.png",0)

w_pawn_white, h_pawn_white = pawn_white_template.shape[::-1]

res_pawn_white = cv2.matchTemplate(img_gray,pawn_white_template,cv2.TM_CCOEFF_NORMED)

threshhold = 0.6
loc = np.where(res_pawn_white >= threshhold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb,pt,(pt[0]+w_pawn_white, pt[1]+h_pawn_white),(0,255,255),1)

cv2.imshow('detected',img_rgb)

Picture of the result detected Window 检测到的结果的图片窗口

白色典当模板

I hope you guys can help me. 我希望你们能帮助我。 And if you want any extra information please ask me. 如果您需要任何其他信息,请询问我。

Best Regards 最好的祝福

Tobias 托比亚斯

I have downloaded your picture and edited it to remove the yellow squares and made my own template both done with paint (please don't judge me). 我已经下载了您的图片并对其进行了编辑以删除黄色方块,并用油漆完成了自己的模板(请不要判断我)。 Then I tried your code and it worked well. 然后,我尝试了您的代码,效果很好。 It is hard to tell why it is not working in your case but if I guess I would have to say that it has something to do with your template. 很难说出为什么它在您的情况下不起作用,但是如果我想我不得不说它与您的模板有关。 When you say you have a transparent png picture does it mean it is completly transparent (even inside the black borders) or just the surroundings? 当您说自己有透明的png图片时,是否表示它完全透明(甚至在黑色边框内)还是只是周围? If the first case is true try to make a template that has the middle filled with white pixels as in the original image. 如果第一种情况是正确的,请尝试制作一个模板,该模板的中间与原始图像一样用白色像素填充。

Your code: 您的代码:

import cv2
import numpy as np

img = cv2.imread('pawn.png')

# Standard settings
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)

# White Pawn Template
pawn_white_template = cv2.imread("pawn_white2.png",0)

w_pawn_white, h_pawn_white = pawn_white_template.shape[::-1]

res_pawn_white = cv2.matchTemplate(img_gray,pawn_white_template,cv2.TM_CCOEFF_NORMED)

threshhold = 0.6
loc = np.where(res_pawn_white >= threshhold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb,pt,(pt[0]+w_pawn_white, pt[1]+h_pawn_white),(0,255,255),1)

cv2.imshow('detected',img_rgb)

Result: 结果:

在此处输入图片说明

My original img: 我的原始img:

在此处输入图片说明

My template: 我的模板:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM