Preprocess the image to select all pixels within specified range of color.
OpenCV provides InRange function that can be used to pick out pixels based on their values.
We provide upper and lower threshold values to this function,
it then generates a mask;
a binary image where foreground pixels (white) are within the specified range.
We can specify range manually(like I have used constant values) or you might also use one of the OpenCV machine learning algorithms to pick the ranges automatically.