Ask Your Question
0

Color threshholding only outputs edge for green color.

asked 2020-11-02 16:25:18 -0600

updated 2020-11-02 22:24:21 -0600

supra56 gravatar image

So basically I tried to threshhold the white and green color. While the white color outputs ideal results, the green threshold only outputs the outline of the color.

import cv2 
import numpy as np 

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

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

lower_white = np.array([205, 235, 235]) 
upper_white = np.array([220, 240, 240]) 

lower_green = np.array([80, 140, 110]) 
upper_green = np.array([90, 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()

C:\fakepath\board.png

C:\fakepath\green_mask_doesnt_work.JPG

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2020-11-03 04:20:26 -0600

kbarni gravatar image

This is normal. The color of the green squares is (BGR) 79,155,108, which is outside the lower_green - upper green range.

The green squares have a border with 86,160,116 color, the inRange segments this part.

Try to change your range to: lower_green = np.array([75, 150, 105]) and upper_green = np.array([90, 165, 120]).

edit flag offensive delete link more

Comments

Thanks, this is it.

jjreddish gravatar imagejjreddish ( 2020-11-03 07:41:36 -0600 )edit
0

answered 2020-11-03 06:51:47 -0600

supra56 gravatar image

updated 2020-11-03 07:40:40 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-11-02 16:25:18 -0600

Seen: 3,337 times

Last updated: Nov 03 '20