1

I have an image:

enter image description here

I would like to create a binary mask for the chess piece and set the background to black. I know I have to use inRange() function then using Bitwise operation, but I don't know how to get the color range for the chess piece in this image.

Edited

These more samples :

sample 1 sample 2 sample 3 sample 4

Miki
  • 40,887
  • 13
  • 123
  • 202
suhayb
  • 35
  • 4
  • compute/draw [HSV histogram](http://stackoverflow.com/a/29286584/2521214) that should enlight you about the range of colors... also sometimes is better to treshold the background colors instead of the object (use what has more stable colors/results ...) – Spektre Nov 30 '15 at 08:16
  • 1
    If you crop out the largest area possible of pure background (chessboard) and get the average HSV value, and then crop out the largest possible area of the pawn (soldier) without any chessboard and again get the average HSV for that area, you will see that the two are readily distinguishable by using the Hue component whereas they are hard to differentiate in RGB colourspace. – Mark Setchell Dec 01 '15 at 10:23
  • Can you post a couple more images? I'd like to test my proposal on more images before posting an answer. – Miki Dec 01 '15 at 12:54
  • @Miki I did , in the last editing – suhayb Dec 02 '15 at 12:16
  • Ok thanks. Well, your images are _very_ different: 1) background color is not a single chessboard cell, 2) pieces have different color, illumination 3) pieces may have some pixel with same background color. Bottomline, you won't be able to segment the piece with a simple `inRange`. – Miki Dec 02 '15 at 12:43
  • @Miki I am very thankful for your attention brother , I solved my problem as you say in two `inRange` by using detecting the range manually as it explained in the tutorial below in the answer :) – suhayb Dec 02 '15 at 13:14

1 Answers1

0

There are some applications that can give the hsv value for a specific colour such as http://www.colorpicker.com/. You can use this as a starting point then proceed with the old fashion trial and error way, to get the perfect filter. The tutorial below combines the inrage function with trackbars. This allows the changing of the inrage filters dynamically, making it easier to find the desired treshold.

http://opencv-srf.blogspot.co.uk/2010/09/object-detection-using-color-seperation.html

Eni
  • 115
  • 8