From 12cd8ae614ee28a2e1eb55c3e41f701cff364866 Mon Sep 17 00:00:00 2001 From: Fabien Spindler Date: Wed, 3 Apr 2024 19:17:08 +0200 Subject: [PATCH] Clean code --- .../color/tutorial-hsv-range-tuner.cpp | 50 +++++----- .../tutorial-hsv-segmentation-pcl-viewer.cpp | 98 ++++--------------- .../color/tutorial-hsv-segmentation-pcl.cpp | 38 +++---- .../color/tutorial-hsv-segmentation.cpp | 34 ++++--- 4 files changed, 83 insertions(+), 137 deletions(-) diff --git a/tutorial/segmentation/color/tutorial-hsv-range-tuner.cpp b/tutorial/segmentation/color/tutorial-hsv-range-tuner.cpp index c278a170e6..73b21d9466 100644 --- a/tutorial/segmentation/color/tutorial-hsv-range-tuner.cpp +++ b/tutorial/segmentation/color/tutorial-hsv-range-tuner.cpp @@ -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 Ic; + vpImage I; int width, height; #if defined(VISP_HAVE_REALSENSE2) @@ -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); @@ -210,25 +210,25 @@ int main(int argc, char *argv[]) vpImage S(height, width); vpImage V(height, width); vpImage mask(height, width); - vpImage Ic_segmented(height, width); + vpImage 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(Ic.bitmap), + vpImageConvert::RGBaToHSV(reinterpret_cast(I.bitmap), reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), - reinterpret_cast(V.bitmap), Ic.getSize()); + reinterpret_cast(V.bitmap), I.getSize()); vpImageTools::inRange(reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), @@ -237,16 +237,16 @@ int main(int argc, char *argv[]) reinterpret_cast(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; } @@ -256,8 +256,8 @@ int main(int argc, char *argv[]) int h = static_cast(H[i][j]); int s = static_cast(S[i][j]); int v = static_cast(V[i][j]); - std::cout << "RGB[" << i << "][" << j << "]: " << static_cast(Ic[i][j].R) << " " << static_cast(Ic[i][j].G) - << " " << static_cast(Ic[i][j].B) << " -> HSV: " << h << " " << s << " " << v << std::endl; + std::cout << "RGB[" << i << "][" << j << "]: " << static_cast(I[i][j].R) << " " << static_cast(I[i][j].G) + << " " << static_cast(I[i][j].B) << " -> HSV: " << h << " " << s << " " << v << std::endl; } else if (button == vpMouseButton::button1) { unsigned int i = ip.get_i(); @@ -310,14 +310,14 @@ int main(int argc, char *argv[]) std::cout << "Save images in path_img folder..." << std::endl; vpImage 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; diff --git a/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl-viewer.cpp b/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl-viewer.cpp index b84800a23c..63bf896196 100644 --- a/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl-viewer.cpp +++ b/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl-viewer.cpp @@ -16,67 +16,6 @@ #include #include -#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::Ptr pointcloud) - { - pcl::PointCloud::Ptr local_pointcloud(new pcl::PointCloud()); - - 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 lock(mutex); - flush_viewer = m_flush_viewer; - m_flush_viewer = false; - local_pointcloud = pointcloud->makeShared(); - } - - if (flush_viewer) { - if (first_init) { - - viewer->addPointCloud(local_pointcloud, "sample cloud"); - viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud"); - first_init = false; - } - else { - viewer->updatePointCloud(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"; @@ -137,20 +76,21 @@ int main(int argc, char **argv) vpCameraParameters cam_depth = rs.getCameraParameters(RS2_STREAM_DEPTH, vpCameraParameters::perspectiveProjWithoutDistortion); - vpImage Ic(height, width); + vpImage I(height, width); vpImage H(height, width); vpImage S(height, width); vpImage V(height, width); - vpImage Ic_segmented_mask(height, width, 0); + vpImage mask(height, width); vpImage depth_raw(height, width); + vpImage 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; @@ -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(Ic.bitmap), + rs.acquire((unsigned char *)I.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to); + vpImageConvert::RGBaToHSV(reinterpret_cast(I.bitmap), reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), - reinterpret_cast(V.bitmap), Ic.getSize()); + reinterpret_cast(V.bitmap), I.getSize()); vpImageTools::inRange(reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), reinterpret_cast(V.bitmap), hsv_values, - reinterpret_cast(Ic_segmented_mask.bitmap), - Ic_segmented_mask.getSize()); + reinterpret_cast(mask.bitmap), + mask.getSize()); + + vpImageTools::inMask(I, mask, I_segmented); { std::lock_guard 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; diff --git a/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl.cpp b/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl.cpp index e993ab03d1..919a99bc6a 100644 --- a/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl.cpp +++ b/tutorial/segmentation/color/tutorial-hsv-segmentation-pcl.cpp @@ -72,20 +72,21 @@ int main(int argc, char **argv) vpCameraParameters cam_depth = rs.getCameraParameters(RS2_STREAM_DEPTH, vpCameraParameters::perspectiveProjWithoutDistortion); - vpImage Ic(height, width); + vpImage I(height, width); vpImage H(height, width); vpImage S(height, width); vpImage V(height, width); - vpImage Ic_segmented_mask(height, width, 0); + vpImage mask(height, width, 0); vpImage depth_raw(height, width); + vpImage 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; @@ -93,40 +94,41 @@ 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(Ic.bitmap), + rs.acquire((unsigned char *)I.bitmap, (unsigned char *)(depth_raw.bitmap), NULL, NULL, &align_to); + vpImageConvert::RGBaToHSV(reinterpret_cast(I.bitmap), reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), - reinterpret_cast(V.bitmap), Ic.getSize()); + reinterpret_cast(V.bitmap), I.getSize()); vpImageTools::inRange(reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), reinterpret_cast(V.bitmap), hsv_values, - reinterpret_cast(Ic_segmented_mask.bitmap), - Ic_segmented_mask.getSize()); + reinterpret_cast(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; } diff --git a/tutorial/segmentation/color/tutorial-hsv-segmentation.cpp b/tutorial/segmentation/color/tutorial-hsv-segmentation.cpp index 31ad53be7c..b9f4bb2255 100644 --- a/tutorial/segmentation/color/tutorial-hsv-segmentation.cpp +++ b/tutorial/segmentation/color/tutorial-hsv-segmentation.cpp @@ -66,14 +66,15 @@ int main(int argc, char **argv) config.disable_stream(RS2_STREAM_INFRARED, 2); rs.open(config); - vpImage Ic(height, width); + vpImage I(height, width); vpImage H(height, width); vpImage S(height, width); vpImage V(height, width); - vpImage Ic_segmented_mask(height, width, 0); + vpImage mask(height, width); + vpImage 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.; @@ -81,35 +82,36 @@ int main(int argc, char **argv) while (!quit) { double t = vpTime::measureTimeMs(); - rs.acquire(Ic); - vpImageConvert::RGBaToHSV(reinterpret_cast(Ic.bitmap), + rs.acquire(I); + vpImageConvert::RGBaToHSV(reinterpret_cast(I.bitmap), reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), - reinterpret_cast(V.bitmap), Ic.getSize()); + reinterpret_cast(V.bitmap), I.getSize()); vpImageTools::inRange(reinterpret_cast(H.bitmap), reinterpret_cast(S.bitmap), reinterpret_cast(V.bitmap), hsv_values, - reinterpret_cast(Ic_segmented_mask.bitmap), - Ic_segmented_mask.getSize()); + reinterpret_cast(mask.bitmap), + mask.getSize()); - vpDisplay::display(Ic); - vpDisplay::display(Ic_segmented_mask); - vpDisplay::displayText(Ic, 20, 20, "Click to quit...", vpColor::red); + vpImageTools::inMask(I, mask, I_segmented); - if (vpDisplay::getClick(Ic, false)) { + vpDisplay::display(I); + vpDisplay::display(I_segmented); + vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red); + + 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; }