|
| 1 | +/** |
| 2 | + * @ingroup vision_examples |
| 3 | + * @defgroup exampleMeshFromLiveRGBD exampleMeshFromLiveRGBD |
| 4 | + * @brief Transform RGBD frame to cloud/mesh. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <yarp/conf/version.h> |
| 8 | + |
| 9 | +#include <yarp/os/LogStream.h> |
| 10 | +#include <yarp/os/Network.h> |
| 11 | +#include <yarp/os/Property.h> |
| 12 | +#include <yarp/os/SystemClock.h> |
| 13 | +#include <yarp/os/Value.h> |
| 14 | + |
| 15 | +#include <yarp/dev/IRGBDSensor.h> |
| 16 | +#include <yarp/dev/PolyDriver.h> |
| 17 | + |
| 18 | +#include <yarp/sig/Image.h> |
| 19 | +#include <yarp/sig/IntrinsicParams.h> |
| 20 | +#include <yarp/sig/PointCloud.h> |
| 21 | +#include <yarp/sig/PointCloudUtils.h> |
| 22 | + |
| 23 | +#include <YarpCloudUtils.hpp> |
| 24 | + |
| 25 | +constexpr const char * DEFAULT_REMOTE = "/realsense2"; |
| 26 | +constexpr const char * DEFAULT_PREFIX = "/exampleMeshFromLiveRGBD"; |
| 27 | +constexpr const char * DEFAULT_COLLECTION = "meshPipeline"; |
| 28 | + |
| 29 | +int main(int argc, char * argv[]) |
| 30 | +{ |
| 31 | + yarp::os::Network yarp; |
| 32 | + |
| 33 | + if (!yarp::os::Network::checkNetwork()) |
| 34 | + { |
| 35 | + yError() << "no YARP server available"; |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + yarp::os::Property options; |
| 40 | + options.fromCommand(argc, argv); |
| 41 | + |
| 42 | + if (options.check("help")) |
| 43 | + { |
| 44 | + yInfo() << argv[0] << "commands:"; |
| 45 | + yInfo() << "\t--remote" << "\tremote port prefix to connect to, defaults to:" << DEFAULT_REMOTE; |
| 46 | + yInfo() << "\t--prefix" << "\tlocal port prefix, defaults to:" << DEFAULT_PREFIX; |
| 47 | + yInfo() << "\t--cloud " << "\tpath to file with .ply extension to export the point cloud to"; |
| 48 | + yInfo() << "\t--mesh " << "\tpath to file with .ply extension to export the surface mesh to"; |
| 49 | + yInfo() << "\t--steps " << "\tsection collection defining the meshing pipeline, defaults to:" << DEFAULT_COLLECTION; |
| 50 | + yInfo() << "\t--binary" << "\texport data in binary format, defaults to: true"; |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + auto remote = options.check("remote", yarp::os::Value(DEFAULT_REMOTE)).asString(); |
| 55 | + auto prefix = options.check("prefix", yarp::os::Value(DEFAULT_PREFIX)).asString(); |
| 56 | + auto fileCloud = options.check("cloud", yarp::os::Value("")).asString(); |
| 57 | + auto fileMesh = options.check("mesh", yarp::os::Value("")).asString(); |
| 58 | + auto collection = options.check("steps", yarp::os::Value(DEFAULT_COLLECTION)).asString(); |
| 59 | + auto binary = options.check("binary", yarp::os::Value(true)).asBool(); |
| 60 | + |
| 61 | + yarp::sig::FlexImage colorImage; |
| 62 | + yarp::sig::ImageOf<yarp::sig::PixelFloat> depthImage; |
| 63 | + yarp::sig::IntrinsicParams colorParams; |
| 64 | + |
| 65 | + { |
| 66 | + yarp::os::Property sensorOptions { |
| 67 | + {"device", yarp::os::Value("RGBDSensorClient")}, |
| 68 | + {"localImagePort", yarp::os::Value(prefix + "/client/rgbImage:i")}, |
| 69 | + {"localDepthPort", yarp::os::Value(prefix + "/client/depthImage:i")}, |
| 70 | + {"localRpcPort", yarp::os::Value(prefix + "/client/rpc:o")}, |
| 71 | + {"remoteImagePort", yarp::os::Value(remote + "/rgbImage:o")}, |
| 72 | + {"remoteDepthPort", yarp::os::Value(remote + "/depthImage:o")}, |
| 73 | + {"remoteRpcPort", yarp::os::Value(remote + "/rpc:i")} |
| 74 | + }; |
| 75 | + |
| 76 | + yarp::dev::PolyDriver sensorDevice; |
| 77 | + |
| 78 | + if (!sensorDevice.open(sensorOptions)) |
| 79 | + { |
| 80 | + yError() << "unable to establish connection with remote RGBD sensor"; |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + yarp::dev::IRGBDSensor * iRGBDSensor; |
| 85 | + |
| 86 | + if (!sensorDevice.view(iRGBDSensor)) |
| 87 | + { |
| 88 | + yError() << "unable to view IRGBDSensor interface"; |
| 89 | + return 1; |
| 90 | + } |
| 91 | + |
| 92 | +#if YARP_VERSION_MINOR < 5 |
| 93 | + // Wait for the first few frames to arrive. We kept receiving invalid pixel codes |
| 94 | + // from the depthCamera device if started straight away. |
| 95 | + // https://github.com/roboticslab-uc3m/vision/issues/88 |
| 96 | + yarp::os::SystemClock::delaySystem(1.0); |
| 97 | +#endif |
| 98 | + |
| 99 | + yarp::os::Property intrinsic; |
| 100 | + |
| 101 | + if (!iRGBDSensor->getRgbIntrinsicParam(intrinsic)) |
| 102 | + { |
| 103 | + yError() << "unable to retrieve RGB intrinsic parameters"; |
| 104 | + return 1; |
| 105 | + } |
| 106 | + |
| 107 | + colorParams.fromProperty(intrinsic); |
| 108 | + |
| 109 | + for (auto n = 0;; n++) |
| 110 | + { |
| 111 | + iRGBDSensor->getImages(colorImage, depthImage); |
| 112 | + |
| 113 | + if (colorImage.getRawImageSize() != 0 && depthImage.getRawImageSize() != 0) |
| 114 | + { |
| 115 | + break; |
| 116 | + } |
| 117 | + else if (n == 10) |
| 118 | + { |
| 119 | + yError() << "unable to acquire RGBD frames"; |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + yarp::os::SystemClock::delaySystem(0.1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + yarp::sig::ImageOf<yarp::sig::PixelRgb> temp; |
| 128 | + temp.copy(colorImage); |
| 129 | + auto cloud = yarp::sig::utils::depthRgbToPC<yarp::sig::DataXYZRGBA>(depthImage, temp, colorParams); |
| 130 | + yInfo() << "got cloud of" << cloud.size() << "points, organized as" << cloud.width() << "x" << cloud.height(); |
| 131 | + |
| 132 | + if (!fileCloud.empty()) |
| 133 | + { |
| 134 | + if (!roboticslab::YarpCloudUtils::savePLY(fileCloud, cloud, binary)) |
| 135 | + { |
| 136 | + yWarning() << "unable to export cloud to" << fileCloud; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + yInfo() << "cloud exported to" << fileCloud; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (!fileMesh.empty()) |
| 145 | + { |
| 146 | +#ifdef SAMPLE_CONFIG |
| 147 | + // set sensible defaults, most suitable for organized clouds |
| 148 | + std::string sampleConfigFile = SAMPLE_CONFIG; |
| 149 | + options.fromConfigFile(sampleConfigFile, false); |
| 150 | +#endif |
| 151 | + |
| 152 | + yarp::sig::PointCloudXYZRGBA meshPoints; |
| 153 | + yarp::sig::VectorOf<int> meshIndices; |
| 154 | + |
| 155 | + auto start = yarp::os::SystemClock::nowSystem(); |
| 156 | + |
| 157 | + if (!roboticslab::YarpCloudUtils::meshFromCloud(cloud, meshPoints, meshIndices, options, collection)) |
| 158 | + { |
| 159 | + yWarning() << "unable to reconstruct surface from cloud"; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + auto end = yarp::os::SystemClock::nowSystem(); |
| 164 | + auto elapsed = static_cast<int>((end - start) * 1000); |
| 165 | + |
| 166 | + yInfo() << "surface reconstructed in" << elapsed << "ms, got mesh of" << meshPoints.size() << "points and" |
| 167 | + << meshIndices.size() / 3 << "faces"; |
| 168 | + |
| 169 | + if (!roboticslab::YarpCloudUtils::savePLY(fileMesh, meshPoints, meshIndices, binary)) |
| 170 | + { |
| 171 | + yWarning() << "unable to export mesh to" << fileMesh; |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + yInfo() << "mesh exported to" << fileMesh; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
0 commit comments