Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Accurately way to get white pieces instead of duplicated black pieces. U don't whose side of black pieces.

#!/usr/bin/env/python37
#OpenCV 4.4.0, Raspberry pi3b/3B+, 4B, Buster ver 10
#Date 3rd Noivember, 2020

import cv2 
import numpy as np 

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

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_white = np.array([75, 235, 235]) 
upper_white = np.array([255, 255, 255]) 

lower_green = np.array([75, 150, 105]) 
upper_green = np.array([85, 160, 125]) 

mask_green = cv2.inRange(img, lower_green, upper_green)
mask_white = cv2.inRange(img, lower_white, upper_white)

mask = mask_white + mask_green

result = cv2.bitwise_and(img, img, mask = mask) 

cv2.imshow('mask', mask) 
cv2.imshow('result', result)
cv2.waitKey(0) 
cv2.destroyAllWindows()

Output:

image description

Accurately way to get white pieces instead of duplicated black pieces. U don't know whose side of black pieces.

#!/usr/bin/env/python37
#OpenCV 4.4.0, Raspberry pi3b/3B+, 4B, Buster ver 10
#Date 3rd Noivember, 2020

import cv2 
import numpy as np 

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

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_white = np.array([75, 235, 235]) 
upper_white = np.array([255, 255, 255]) 

lower_green = np.array([75, 150, 105]) 
upper_green = np.array([85, 160, 125]) 

mask_green = cv2.inRange(img, lower_green, upper_green)
mask_white = cv2.inRange(img, lower_white, upper_white)

mask = mask_white + mask_green

result = cv2.bitwise_and(img, img, mask = mask) 

cv2.imshow('mask', mask) 
cv2.imshow('result', result)
cv2.waitKey(0) 
cv2.destroyAllWindows()

Output:

image description