Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
fspindle committed Apr 3, 2024
1 parent 2bb9ae2 commit 12cd8ae
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 137 deletions.
50 changes: 25 additions & 25 deletions tutorial/segmentation/color/tutorial-hsv-range-tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int main(int argc, char *argv[])
hsv_values_trackbar[4] = 0; // Low V
hsv_values_trackbar[5] = max_value; // High V

vpImage<vpRGBa> Ic;
vpImage<vpRGBa> I;
int width, height;

#if defined(VISP_HAVE_REALSENSE2)
Expand All @@ -171,19 +171,19 @@ int main(int argc, char *argv[])
config.disable_stream(RS2_STREAM_INFRARED, 1);
config.disable_stream(RS2_STREAM_INFRARED, 2);
rs.open(config);
rs.acquire(Ic);
rs.acquire(I);
#endif
}
else {
try {
vpImageIo::read(Ic, opt_img_filename);
vpImageIo::read(I, opt_img_filename);
}
catch (const vpException &e) {
std::cout << e.getStringMessage() << std::endl;
return EXIT_FAILURE;
}
width = Ic.getWidth();
height = Ic.getHeight();
width = I.getWidth();
height = I.getHeight();
}

cv::namedWindow(window_detection_name);
Expand All @@ -210,25 +210,25 @@ int main(int argc, char *argv[])
vpImage<unsigned char> S(height, width);
vpImage<unsigned char> V(height, width);
vpImage<unsigned char> mask(height, width);
vpImage<vpRGBa> Ic_segmented(height, width);
vpImage<vpRGBa> I_segmented(height, width);

vpDisplayX d_Ic(Ic, 0, 0, "Current frame");
vpDisplayX d_Ic_segmented(Ic_segmented, Ic.getWidth()+75, 0, "Segmented frame");
vpDisplayX d_I(I, 0, 0, "Current frame");
vpDisplayX d_I_segmented(I_segmented, I.getWidth()+75, 0, "Segmented frame");
bool quit = false;

while (!quit) {
if (use_realsense) {
#if defined(VISP_HAVE_REALSENSE2)
rs.acquire(Ic);
rs.acquire(I);
#endif
}
else {
vpImageIo::read(Ic, opt_img_filename);
vpImageIo::read(I, opt_img_filename);
}
vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(Ic.bitmap),
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), Ic.getSize());
reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());

vpImageTools::inRange(reinterpret_cast<unsigned char *>(H.bitmap),
reinterpret_cast<unsigned char *>(S.bitmap),
Expand All @@ -237,16 +237,16 @@ int main(int argc, char *argv[])
reinterpret_cast<unsigned char *>(mask.bitmap),
mask.getSize());

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

vpDisplay::display(Ic);
vpDisplay::display(Ic_segmented);
vpDisplay::displayText(Ic, 20, 20, "Left click to learn HSV value...", vpColor::red);
vpDisplay::displayText(Ic, 40, 20, "Middle click to get HSV value...", vpColor::red);
vpDisplay::displayText(Ic, 60, 20, "Right click to quit...", vpColor::red);
vpDisplay::display(I);
vpDisplay::display(I_segmented);
vpDisplay::displayText(I, 20, 20, "Left click to learn HSV value...", vpColor::red);
vpDisplay::displayText(I, 40, 20, "Middle click to get HSV value...", vpColor::red);
vpDisplay::displayText(I, 60, 20, "Right click to quit...", vpColor::red);
vpImagePoint ip;
vpMouseButton::vpMouseButtonType button;
if (vpDisplay::getClick(Ic, ip, button, false)) {
if (vpDisplay::getClick(I, ip, button, false)) {
if (button == vpMouseButton::button3) {
quit = true;
}
Expand All @@ -256,8 +256,8 @@ int main(int argc, char *argv[])
int h = static_cast<int>(H[i][j]);
int s = static_cast<int>(S[i][j]);
int v = static_cast<int>(V[i][j]);
std::cout << "RGB[" << i << "][" << j << "]: " << static_cast<int>(Ic[i][j].R) << " " << static_cast<int>(Ic[i][j].G)
<< " " << static_cast<int>(Ic[i][j].B) << " -> HSV: " << h << " " << s << " " << v << std::endl;
std::cout << "RGB[" << i << "][" << j << "]: " << static_cast<int>(I[i][j].R) << " " << static_cast<int>(I[i][j].G)
<< " " << static_cast<int>(I[i][j].B) << " -> HSV: " << h << " " << s << " " << v << std::endl;
}
else if (button == vpMouseButton::button1) {
unsigned int i = ip.get_i();
Expand Down Expand Up @@ -310,14 +310,14 @@ int main(int argc, char *argv[])
std::cout << "Save images in path_img folder..." << std::endl;
vpImage<vpRGBa> I_HSV;
vpImageConvert::merge(&H, &S, &V, nullptr, I_HSV);
vpImageIo::write(Ic, path_img + "/I.png");
vpImageIo::write(I, path_img + "/I.png");
vpImageIo::write(I_HSV, path_img + "/I-HSV.png");
vpImageIo::write(Ic_segmented, path_img + "/I-HSV-segmented.png");
vpImageIo::write(I_segmented, path_img + "/I-HSV-segmented.png");
}
break;
}
vpDisplay::flush(Ic);
vpDisplay::flush(Ic_segmented);
vpDisplay::flush(I);
vpDisplay::flush(I_segmented);
cv::waitKey(10); // To display trackbar
}
return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,6 @@
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>

#if 1
class vpPointCloudViewer
{
public:
explicit vpPointCloudViewer() : m_stop(false), m_flush_viewer(false) { }

void flush()
{
m_flush_viewer = true;
}

void run(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZ>());

bool flush_viewer = false;
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->initCameraParameters();
viewer->setPosition(640 + 80, 480 + 80);
viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
viewer->setSize(640, 480);

bool first_init = true;
while (!m_stop) {
{
std::lock_guard<std::mutex> lock(mutex);
flush_viewer = m_flush_viewer;
m_flush_viewer = false;
local_pointcloud = pointcloud->makeShared();
}

if (flush_viewer) {
if (first_init) {

viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
first_init = false;
}
else {
viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
}
}

viewer->spinOnce(10);
}

std::cout << "End of point cloud display thread" << std::endl;
}

void stop()
{
m_stop = true;
}

private:
bool m_stop;
bool m_flush_viewer;
};
#endif

int main(int argc, char **argv)
{
std::string opt_hsv_filename = "calib/hsv-thresholds.yml";
Expand Down Expand Up @@ -137,20 +76,21 @@ int main(int argc, char **argv)
vpCameraParameters cam_depth = rs.getCameraParameters(RS2_STREAM_DEPTH,
vpCameraParameters::perspectiveProjWithoutDistortion);

vpImage<vpRGBa> Ic(height, width);
vpImage<vpRGBa> I(height, width);
vpImage<unsigned char> H(height, width);
vpImage<unsigned char> S(height, width);
vpImage<unsigned char> V(height, width);
vpImage<unsigned char> Ic_segmented_mask(height, width, 0);
vpImage<unsigned char> mask(height, width);
vpImage<uint16_t> depth_raw(height, width);
vpImage<vpRGBa> I_segmented(height, width);

vpDisplayX d_Ic(Ic, 0, 0, "Current frame");
vpDisplayX d_Ic_segmented_mask(Ic_segmented_mask, Ic.getWidth()+75, 0, "HSV segmented frame");
vpDisplayX d_I(I, 0, 0, "Current frame");
vpDisplayX d_I_segmented(I_segmented, I.getWidth()+75, 0, "HSV segmented frame");

bool quit = false;
double loop_time = 0., total_loop_time = 0.;
long nb_iter = 0;
float Z_min = 0.2;
float Z_min = 0.1;
float Z_max = 2.5;
int pcl_size = 0;

Expand All @@ -162,37 +102,39 @@ int main(int argc, char **argv)

while (!quit) {
double t = vpTime::measureTimeMs();
rs.acquire((unsigned char *)Ic.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to);
vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(Ic.bitmap),
rs.acquire((unsigned char *)I.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to);
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), Ic.getSize());
reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());

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

vpImageTools::inMask(I, mask, I_segmented);

{
std::lock_guard<std::mutex> lock(pcl_viewer_mutex);
vpImageConvert::depthToPointCloud(depth_raw, depth_scale, cam_depth, pointcloud, &Ic_segmented_mask, Z_min, Z_max);
vpImageConvert::depthToPointCloud(depth_raw, depth_scale, cam_depth, pointcloud, &mask, Z_min, Z_max);
pcl_size = pointcloud->size();
}

std::cout << "Segmented point cloud size: " << pcl_size << std::endl;

vpDisplay::display(Ic);
vpDisplay::display(Ic_segmented_mask);
vpDisplay::displayText(Ic, 20, 20, "Click to quit...", vpColor::red);
vpDisplay::display(I);
vpDisplay::display(I_segmented);
vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);

if (vpDisplay::getClick(Ic, false)) {
if (vpDisplay::getClick(I, false)) {
quit = true;
}

vpDisplay::flush(Ic);
vpDisplay::flush(Ic_segmented_mask);
vpDisplay::flush(I);
vpDisplay::flush(I_segmented);
nb_iter++;
loop_time = vpTime::measureTimeMs() - t;
total_loop_time += loop_time;
Expand Down
38 changes: 20 additions & 18 deletions tutorial/segmentation/color/tutorial-hsv-segmentation-pcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,61 +72,63 @@ int main(int argc, char **argv)
vpCameraParameters cam_depth = rs.getCameraParameters(RS2_STREAM_DEPTH,
vpCameraParameters::perspectiveProjWithoutDistortion);

vpImage<vpRGBa> Ic(height, width);
vpImage<vpRGBa> I(height, width);
vpImage<unsigned char> H(height, width);
vpImage<unsigned char> S(height, width);
vpImage<unsigned char> V(height, width);
vpImage<unsigned char> Ic_segmented_mask(height, width, 0);
vpImage<unsigned char> mask(height, width, 0);
vpImage<uint16_t> depth_raw(height, width);
vpImage<vpRGBa> I_segmented(height, width);

vpDisplayX d_Ic(Ic, 0, 0, "Current frame");
vpDisplayX d_Ic_segmented_mask(Ic_segmented_mask, Ic.getWidth()+75, 0, "HSV segmented frame");
vpDisplayX d_I(I, 0, 0, "Current frame");
vpDisplayX d_I_segmented(I_segmented, I.getWidth()+75, 0, "HSV segmented frame");

bool quit = false;
double loop_time = 0., total_loop_time = 0.;
long nb_iter = 0;
float Z_min = 0.2;
float Z_min = 0.1;
float Z_max = 2.5;
int pcl_size = 0;

pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);

while (!quit) {
double t = vpTime::measureTimeMs();
rs.acquire((unsigned char *)Ic.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to);
vpImageConvert::RGBaToHSV(reinterpret_cast<unsigned char *>(Ic.bitmap),
rs.acquire((unsigned char *)I.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to);
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), Ic.getSize());
reinterpret_cast<unsigned char *>(V.bitmap), I.getSize());

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

vpImageConvert::depthToPointCloud(depth_raw, depth_scale, cam_depth, pointcloud, &Ic_segmented_mask, Z_min, Z_max);
vpImageTools::inMask(I, mask, I_segmented);

vpImageConvert::depthToPointCloud(depth_raw, depth_scale, cam_depth, pointcloud, &mask, Z_min, Z_max);
pcl_size = pointcloud->size();

std::cout << "Segmented point cloud size: " << pcl_size << std::endl;

vpDisplay::display(Ic);
vpDisplay::display(Ic_segmented_mask);
vpDisplay::displayText(Ic, 20, 20, "Click to quit...", vpColor::red);
vpDisplay::display(I);
vpDisplay::display(I_segmented);
vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);

if (vpDisplay::getClick(Ic, false)) {
if (vpDisplay::getClick(I, false)) {
quit = true;
}

vpDisplay::flush(Ic);
vpDisplay::flush(Ic_segmented_mask);
vpDisplay::flush(I);
vpDisplay::flush(I_segmented);
nb_iter++;
loop_time = vpTime::measureTimeMs() - t;
total_loop_time += loop_time;
}


std::cout << "Mean loop time: " << total_loop_time / nb_iter << std::endl;
return EXIT_SUCCESS;
}
Expand Down
Loading

0 comments on commit 12cd8ae

Please sign in to comment.