Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
Tutorial: HSV low/high range tuner tool

Introduction

This tutorial follows Tutorial: Introduction to color segmentation using HSV color scale.

Note that all the material (source code and images) described in this tutorial is part of ViSP source code (in tutorial/segmentation/color folder) and could be found in https://github.com/lagadic/visp/tree/master/tutorial/segmentation/color.

HSV Range tuner tool

In the previous tutorial (see Tutorial: Introduction to color segmentation using HSV color scale), we used the following lines to determine HSV low/high range values in hsv_range vector:

int h = 14, s = 255, v = 209;
int offset = 30;
int h_low = std::max<int>(0, h - offset), h_high = std::min<int>(h + offset, 255);
int s_low = std::max<int>(0, s - offset), s_high = std::min<int>(s + offset, 255);
int v_low = std::max<int>(0, v - offset), v_high = std::min<int>(v + offset, 255);
std::vector<int> hsv_range;
hsv_range.push_back(h_low);
hsv_range.push_back(h_high);
hsv_range.push_back(s_low);
hsv_range.push_back(s_high);
hsv_range.push_back(v_low);
hsv_range.push_back(v_high);

Then this vector was used to determine the pixels that were in the HSV low/high ranges in oder to compute a mask:

vpImage<unsigned char> mask(height, width);
vpImageTools::inRange(reinterpret_cast<unsigned char *>(H.bitmap),
reinterpret_cast<unsigned char *>(S.bitmap),
reinterpret_cast<unsigned char *>(V.bitmap),
hsv_range,
reinterpret_cast<unsigned char *>(mask.bitmap),
mask.getSize());
static int inRange(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value, const vpColVector &hsv_range, unsigned char *mask, unsigned int size)

In tutorial-hsv-range-tuner.cpp we propose a tool that allows to set these HSV low/high values either using a trackbar or by clicking on a pixel in the image before fine tuning the range values with the trackbar.

  • Once build, you can either use this tool on a single image like
    $ cd $VISP_WS/visp-build/tutorial/segmentation/color
    $ ./tutorial-hsv-range-tuner --image ballons.jpg
    
  • or if you have a Realsense camera, proceed with the live stream simply by running
    $ ./tutorial-hsv-range-tuner
    

To use this tool, in the "Current frame" window:

  • Left click allows to set the HSV low/high values in the trackbar
  • Middle click allows to print the RGB and HSV values corresponding to the clicked point
  • Right mouse click allows to save the HSV low/high range values in a yaml file. By default this file is saved in calib/hsv-thresholds.yml. Hereafter we provide a possible content:
    $ cat calib/hsv-thresholds.yml
    # File created 2024/04/03 17:28:10
    rows: 6
    cols: 1
    data:
      - [0]
      - [64]
      - [222]
      - [255]
      - [128]
      - [242]
    

Next tutorial

You are now ready to see how to continue with Tutorial: Live color segmentation using HSV color scale.