Search code examples
pythonopencvextractfeature-extractionfeature-detection

How to transfer a blob of an image to a white background?


I am attempting to extract the color features of tennis ball in an image. To make it easier, I thought that transferring the tennis ball to a white canvas and then extracting the features would be better. I am extracting the color features via a histogram.

I have used a mean shift image segmentation algorithm on a frame from a video where the tennis ball is falling (https://i.sstatic.net/etG2n.jpg). Originally, I converted an the frame to gray scale and made an histogram from the updated frame, but I realized that it was ineffective because I am trying to extract the color features. Therefore, I am now trying to transfer the the tennis ball to a white canvas and so its easier to extract the color features from the tennis ball into a histogram.


Solution

  • From your segmented image:

    We can notice that the color difference is very obvious. In such cases, we can simply convert to a color space that can separate these colors better for us, such as HSV. In HSV, the hue channel contains the color variation data. In this image, this is the hue channel:

    From this, all you need is to analyze the image histogram and determine a simple threshold value. By applying the threshold, you reach this:

    Here's a tutorial from the OpenCV documentation on how you can do this in Python. If you can't select this threshold manually, you can use Otsu's binarization method (OpenCV tutorial) on the H channel (splitting from the HSV image).

    Then you can then use this binary image as a mask to extract more features. You don't need to transfer the image to a white/black background, but, if you want to, you can just apply this mask. See OpenCV - Apply mask to a color image.