TobiasDex
TobiasDex

Reputation: 63

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.

General Information: I am playing at Lichess with all default Setting and Styles. Also my white pawn picture is an transperant white png file (50px x 50px). The threshhold is on 0.6 because I get the best result. 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

Template of White Pawn

I hope you guys can help me. And if you want any extra information please ask me.

Best Regards

Tobias

Upvotes: 1

Views: 1307

Answers (1)

kavko
kavko

Reputation: 2831

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? 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:

enter image description here

My original img:

enter image description here

My template:

enter image description here

Upvotes: 1

Related Questions