Skip to content

Commit

Permalink
Rename tuner tutorial and introduce a new tutorial to explain the HSV…
Browse files Browse the repository at this point in the history
… segmentation basics
  • Loading branch information
fspindle committed Apr 3, 2024
1 parent 9951438 commit 3555dbc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
10 changes: 9 additions & 1 deletion tutorial/segmentation/color/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ find_package(VISP REQUIRED visp_core visp_sensor visp_io visp_gui)

# set the list of source files
set(tutorial_cpp
tutorial-hsv-range-tuner.cpp
tutorial-hsv-segmentation.cpp
tutorial-hsv-segmentation-basic.cpp
tutorial-hsv-segmentation-pcl.cpp
tutorial-hsv-segmentation-pcl-viewer.cpp
tutorial-hsv-tuner.cpp
)

file(GLOB tutorial_data "*.jpg")

foreach(cpp ${tutorial_cpp})
visp_add_target(${cpp})
if(COMMAND visp_add_dependency)
visp_add_dependency(${cpp} "tutorials")
endif()
endforeach()

# Copy the data files to the same location than the target
foreach(data ${tutorial_data})
visp_copy_data(tutorial-hsv-segmentation-basic.cpp ${data})
endforeach()
Binary file added tutorial/segmentation/color/ballons.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! \example tutorial-hsv-tuner.cpp
//! \example tutorial-hsv-range-tuner.cpp

#include <iostream>

Expand Down Expand Up @@ -158,10 +158,11 @@ int main(int argc, char *argv[])
vpImage<unsigned char> H(height, width);
vpImage<unsigned char> S(height, width);
vpImage<unsigned char> V(height, width);
vpImage<unsigned char> Ic_segmented(height, width, 0);
vpImage<unsigned char> mask(height, width);
vpImage<vpRGBa> Ic_segmented(height, width);

vpDisplayX d_Ic(Ic, 0, 0, "Current frame");
vpDisplayX d_Ic_segmented(Ic_segmented, Ic.getWidth()+75, 0, "HSV segmented frame");
vpDisplayX d_Ic_segmented(Ic_segmented, Ic.getWidth()+75, 0, "Segmented frame");
bool quit = false;

while (!quit) {
Expand All @@ -172,11 +173,13 @@ int main(int argc, char *argv[])
reinterpret_cast<unsigned char *>(V.bitmap), Ic.getSize());

vpImageTools::inRange(reinterpret_cast<unsigned char *>(H.bitmap),
reinterpret_cast<unsigned char *>(S.bitmap),
reinterpret_cast<unsigned char *>(V.bitmap),
hsv_values_trackbar,
reinterpret_cast<unsigned char *>(Ic_segmented.bitmap),
Ic_segmented.getSize());
reinterpret_cast<unsigned char *>(S.bitmap),
reinterpret_cast<unsigned char *>(V.bitmap),
hsv_values_trackbar,
reinterpret_cast<unsigned char *>(mask.bitmap),
mask.getSize());

vpImageTools::inMask(Ic, mask, Ic_segmented);

vpDisplay::display(Ic);
vpDisplay::display(Ic_segmented);
Expand Down
52 changes: 52 additions & 0 deletions tutorial/segmentation/color/tutorial-hsv-segmentation-basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <visp3/io/vpImageIo.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpImageTools.h>
#include <visp3/gui/vpDisplayX.h>

int main()
{
vpImage<vpRGBa> I;
vpImageIo::read(I, "ballons.jpg");

unsigned int width = I.getWidth();
unsigned int height = I.getHeight();

vpImage<unsigned char> H(height, width);
vpImage<unsigned char> S(height, width);
vpImage<unsigned char> V(height, width);

vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(I.bitmap),
reinterpret_cast<unsigned char *>(H.bitmap),
reinterpret_cast<unsigned char *>(S.bitmap),
reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());

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({ h_low, h_high, s_low, s_high, v_low, v_high });

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());

vpImage<vpRGBa> I_segmented(height, width);
vpImageTools::inMask(I, mask, I_segmented);

vpDisplayX d_I(I, 0, 0, "Current frame");
vpDisplayX d_mask(mask, I.getWidth()+75, 0, "HSV mask");
vpDisplayX d_I_segmented(I_segmented, 2*mask.getWidth()+80, 0, "Segmented frame");

vpDisplay::display(I);
vpDisplay::display(mask);
vpDisplay::display(I_segmented);
vpDisplay::flush(I);
vpDisplay::flush(mask);
vpDisplay::flush(I_segmented);
vpDisplay::getClick(I);
}

0 comments on commit 3555dbc

Please sign in to comment.