miércoles, 17 de febrero de 2016

Color Segmentation - First Week

In my first week I downloaded the 1KK application and looked at the code. It is an Open Source Android project developed by Trevor Rife and is hosted on Github. The program works by taking a picture of seeds using a green background with blue circles. The circles are used to estimate the length per pixel of a picture by getting an average diameter of circle in pixel and knowing the real diameter of the circles. For getting the circles it's necessary to do a segmentation of the blue colors and for extracting the seeds from the image there is also a segmentation to remove the green and blue colors.

An example of an image taken with 1KK


The segmentation is done by using range of colors. The problem with this type of segmentation is that the ranges have to be found by experimentation. In the case of RGB colors they may be affected by lighting changes. For example a picture taken with a lot of light can have high values for RGB channels components but  with low illumination  the values would be low. So for the same range and the same objects the picture would give a different segmentation depending on the light. They use HSV color space for segmentation. To test the ranges predefined I used a software I created that lets you try with different color ranges and see the segmentation of the picture in real time (it also works with videos).

The software is free and is called color-range-selector. To run it you need to have Python and OpenCV installed. I used it on Windows 10 and this are the steps I followed to make it run:

How to install OpenCV for Python in Windows

  1. First you need to install Python 2.7 by downloading and executing the msi installer. 
  2. Then install the Python Development Tools for Windows that are available here http://aka.ms/vcpython27 .
  3. After that you will be able to install the Numpy library for Python by running:
    pip install numpy
    in command line.
  4.  Follow the instructions in this tutorial to install OpenCV for Python http://docs.opencv.org/master/d5/de5/tutorial_py_setup_in_windows.html .

How to install and use the color range selector

  1. Download the project from Github by using the command line:
    git clone https://github.com/HenrYxZ/color-range-selector.git
  2. Change the directory to the project:
    cd color-range-selector
  3. Finally run the main program:
    python color_range_selector.py  

Selecting a range of color

Segmentation using HSV range of colors with color-range-selector
The image shows the segmentation for using a range between (40, 0, 0) and (255, 255, 255) in HSV space. The result is capable to extract the green background and the blue circles. But for other image the ranges don't work.

Testing a segmentation using HSV range and not working
But using a BGR color range seems to work better in the second case. The idea is to use a high minimum value for the red channel. That is because both green and blue are far from the red in the BGR color space. This are the results using a range between (0, 0, 60) and (255, 255, 255):

Testing segmentation on BGR and working well

Segmentation on BGR with inferior results than HSV
In conclusion, HSV space may be used for segmentation with low change of hue. For example using seeds or potatoes with brownish hue works well, but the testing using papers of gray hue not. On the other hand, BGR lets use more different colors of seeds but the segmentation is affected by the illumination.

No hay comentarios.:

Publicar un comentario