Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-hsv-segmentation-basic.cpp
1#include <visp3/core/vpConfig.h>
2#include <visp3/core/vpHSV.h>
3#include <visp3/io/vpImageIo.h>
4#include <visp3/core/vpImageConvert.h>
5#include <visp3/core/vpImageTools.h>
6#include <visp3/gui/vpDisplayFactory.h>
7
8int main()
9{
10#ifdef ENABLE_VISP_NAMESPACE
11 using namespace VISP_NAMESPACE_NAME;
12#endif
13
16 vpImageIo::read(I, "ballons.jpg");
18
20 int h = 14, s = 255, v = 209;
21 int offset = 30;
22 int h_low = std::max<int>(0, h - offset), h_high = std::min<int>(h + offset, 255);
23 int s_low = std::max<int>(0, s - offset), s_high = std::min<int>(s + offset, 255);
24 int v_low = std::max<int>(0, v - offset), v_high = std::min<int>(v + offset, 255);
25 std::vector<int> hsv_range;
26 hsv_range.push_back(h_low);
27 hsv_range.push_back(h_high);
28 hsv_range.push_back(s_low);
29 hsv_range.push_back(s_high);
30 hsv_range.push_back(v_low);
31 hsv_range.push_back(v_high);
33
34#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
39
43 vpImageTools::inRange(Ihsv, hsv_range, maskHSV);
45
47 vpImage<vpRGBa> I_segmented_from_HSV;
48 vpImageTools::inMask(I, maskHSV, I_segmented_from_HSV);
50#endif
51
53 unsigned int width = I.getWidth();
54 unsigned int height = I.getHeight();
55 vpImage<unsigned char> H(height, width);
56 vpImage<unsigned char> S(height, width);
57 vpImage<unsigned char> V(height, width);
58
59 vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(I.bitmap),
60 reinterpret_cast<unsigned char *>(H.bitmap),
61 reinterpret_cast<unsigned char *>(S.bitmap),
62 reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());
63
64 vpImage<unsigned char> mask(height, width);
65 vpImageTools::inRange(reinterpret_cast<unsigned char *>(H.bitmap),
66 reinterpret_cast<unsigned char *>(S.bitmap),
67 reinterpret_cast<unsigned char *>(V.bitmap),
68 hsv_range,
69 reinterpret_cast<unsigned char *>(mask.bitmap),
70 mask.getSize());
71
72 vpImage<vpRGBa> I_segmented(height, width);
73 vpImageTools::inMask(I, mask, I_segmented);
75
76#if defined(VISP_HAVE_DISPLAY)
77#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
78 std::shared_ptr<vpDisplay> d_I = vpDisplayFactory::createDisplay(I);
79 std::shared_ptr<vpDisplay> d_mask = vpDisplayFactory::createDisplay(mask, I.getWidth()+75, 0, "HSV mask");
80 std::shared_ptr<vpDisplay> d_I_segmented = vpDisplayFactory::createDisplay(I_segmented, 2*mask.getWidth()+80, 0, "Segmented frame");
81 std::shared_ptr<vpDisplay> d_mask_hsv = vpDisplayFactory::createDisplay(maskHSV, I.getWidth()+75, mask.getHeight() + 80, "HSV mask using vpHSV");
82 std::shared_ptr<vpDisplay> d_I_segmented_hsv = vpDisplayFactory::createDisplay(I_segmented_from_HSV, 2*mask.getWidth()+80, mask.getHeight() + 80, "Segmented frame using vpHSV");
83#else
84 vpDisplay *d_I = vpDisplayFactory::allocateDisplay(I, 0, 0, "Current frame");
85 vpDisplay *d_mask = vpDisplayFactory::allocateDisplay(mask, I.getWidth()+75, 0, "HSV mask");
86 vpDisplay *d_I_segmented = vpDisplayFactory::allocateDisplay(I_segmented, 2*mask.getWidth()+80, 0, "Segmented frame");
87#endif
88
91 vpDisplay::display(I_segmented);
93 vpDisplay::flush(mask);
94 vpDisplay::flush(I_segmented);
95#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
96 vpDisplay::display(I_segmented_from_HSV);
97 vpDisplay::flush(I_segmented_from_HSV);
98 vpDisplay::display(maskHSV);
99 vpDisplay::flush(maskHSV);
100#endif
102
103#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
104 if (d_I != nullptr) {
105 delete d_I;
106 }
107
108 if (d_mask != nullptr) {
109 delete d_mask;
110 }
111
112 if (d_I_segmented != nullptr) {
113 delete d_I_segmented;
114 }
115#endif
116#endif
117 return EXIT_SUCCESS;
118}
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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 convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static int inMask(const vpImage< vpRGBa > &I, const vpImage< bool > &mask, vpImage< vpRGBa > &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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.