Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-hsv-segmentation-basic.cpp
1 #include <visp3/core/vpConfig.h>
2 #include <visp3/io/vpImageIo.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/core/vpImageTools.h>
5 #include <visp3/gui/vpDisplayX.h>
6 
7 int main()
8 {
9 #ifdef ENABLE_VISP_NAMESPACE
10  using namespace VISP_NAMESPACE_NAME;
11 #endif
12 
14  vpImageIo::read(I, "ballons.jpg");
15 
16  unsigned int width = I.getWidth();
17  unsigned int height = I.getHeight();
18 
19  vpImage<unsigned char> H(height, width);
20  vpImage<unsigned char> S(height, width);
21  vpImage<unsigned char> V(height, width);
22 
23  vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(I.bitmap),
24  reinterpret_cast<unsigned char *>(H.bitmap),
25  reinterpret_cast<unsigned char *>(S.bitmap),
26  reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());
27 
29  int h = 14, s = 255, v = 209;
30  int offset = 30;
31  int h_low = std::max<int>(0, h - offset), h_high = std::min<int>(h + offset, 255);
32  int s_low = std::max<int>(0, s - offset), s_high = std::min<int>(s + offset, 255);
33  int v_low = std::max<int>(0, v - offset), v_high = std::min<int>(v + offset, 255);
34  std::vector<int> hsv_range;
35  hsv_range.push_back(h_low);
36  hsv_range.push_back(h_high);
37  hsv_range.push_back(s_low);
38  hsv_range.push_back(s_high);
39  hsv_range.push_back(v_low);
40  hsv_range.push_back(v_high);
42 
44  vpImage<unsigned char> mask(height, width);
45  vpImageTools::inRange(reinterpret_cast<unsigned char *>(H.bitmap),
46  reinterpret_cast<unsigned char *>(S.bitmap),
47  reinterpret_cast<unsigned char *>(V.bitmap),
48  hsv_range,
49  reinterpret_cast<unsigned char *>(mask.bitmap),
50  mask.getSize());
52 
53  vpImage<vpRGBa> I_segmented(height, width);
54  vpImageTools::inMask(I, mask, I_segmented);
55 
56 #if defined(VISP_HAVE_X11)
57  vpDisplayX d_I(I, 0, 0, "Current frame");
58  vpDisplayX d_mask(mask, I.getWidth()+75, 0, "HSV mask");
59  vpDisplayX d_I_segmented(I_segmented, 2*mask.getWidth()+80, 0, "Segmented frame");
60 
62  vpDisplay::display(mask);
63  vpDisplay::display(I_segmented);
65  vpDisplay::flush(mask);
66  vpDisplay::flush(I_segmented);
68 #endif
69 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static int inMask(const vpImage< unsigned char > &I, const vpImage< unsigned char > &mask, vpImage< unsigned char > &I_mask)
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)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
Type * bitmap
points toward the bitmap
Definition: vpImage.h:135
unsigned int getHeight() const
Definition: vpImage.h:181