41 #include <pcl/filters/filter.h>
42 #include <pcl/io/ply_io.h>
43 #include <pcl/io/obj_io.h>
44 #include <pcl/common/common.h>
45 #include <pcl/surface/poisson.h>
61 "rtabmap-export [options] database.db\n"
62 " New version: we now have to explicitly set what we want to export to give a more fine-grained control.\n"
63 " At least one of these options is required:\n"
64 " --cloud (old version used this by default)\n"
75 " --output \"\" Output name (default: name of the database is used).\n"
76 " --output_dir \"\" Output directory (default: same directory than the database).\n"
77 " --ascii Export PLY in ascii format.\n"
78 " --las Export cloud in LAS instead of PLY (PDAL or libLAS dependency required).\n"
79 " --cloud Export assembled cloud.\n"
80 " --mesh Export assembled mesh.\n"
81 " --texture Texture the mesh. Used with --mesh option.\n"
82 " --texture_size # Texture size 1024, 2048, 4096, 8192, 16384 (default 8192).\n"
83 " --texture_count # Maximum textures generated (default 1). Ignored by --multiband option (adjust --multiband_contrib instead).\n"
84 " --texture_range # Maximum camera range for texturing a polygon (default 0 meters: no limit).\n"
85 " --texture_angle # Maximum camera angle for texturing a polygon (default 0 deg: no limit).\n"
86 " --texture_depth_error # Maximum depth error between reprojected mesh and depth image to texture a face\n"
87 " (-1=disabled, 0=edge length is used, default=0).\n"
88 " --texture_roi_ratios \"# # # #\" Region of interest from images to texture or to color scans. Format\n"
89 " is \"left right top bottom\" (e.g. \"0 0 0 0.1\" means 10%%\n"
90 " of the image bottom not used).\n"
91 " --texture_d2c Distance to camera policy.\n"
92 " --texture_blur # Motion blur threshold (default 0: disabled). Below this threshold, the image is\n"
93 " considered blurred. 0 means disabled. 50 can be good default.\n"
94 " --cam_projection Camera projection on assembled cloud and export node ID on each point (in PointSourceId field).\n"
95 " --cam_projection_keep_all Keep not colored points from cameras (node ID will be 0 and color will be red).\n"
96 " --cam_projection_decimation Decimate images before projecting the points.\n"
97 " --cam_projection_mask \"\" File path for a mask. Format should be 8-bits grayscale. The mask should\n"
98 " cover all cameras in case multi-camera is used and have the same resolution.\n"
99 " --opt # Optimization approach:\n"
100 " 0=Full Global Optimization (default)\n"
101 " 1=Iterative Global Optimization\n"
102 " 2=Use optimized poses already computed in the database instead\n"
103 " of re-computing them (fallback to default if optimized poses don't exist).\n"
104 " 3=No optimization, use odometry poses directly.\n"
105 " --poses Export optimized poses of the robot frame (e.g., base_link).\n"
106 " --poses_camera Export optimized poses of the camera frame (e.g., optical frame).\n"
107 " --poses_scan Export optimized poses of the scan frame.\n"
108 " --poses_gt Export ground truth poses of the robot frame (e.g., base_link).\n"
109 " --poses_gps Export GPS poses of the GPS frame in local coordinates.\n"
110 " --poses_format # Format used for exported poses (default is 11):\n"
111 " 0=Raw 3x4 transformation matrix (r11 r12 r13 tx r21 r22 r23 ty r31 r32 r33 tz)\n"
112 " 1=RGBD-SLAM (in motion capture coordinate frame)\n"
113 " 2=KITTI (same as raw but in optical frame)\n"
116 " 10=RGBD-SLAM in ROS coordinate frame (stamp x y z qx qy qz qw)\n"
117 " 11=RGBD-SLAM in ROS coordinate frame + ID (stamp x y z qx qy qz qw id)\n"
118 " --gps # Export GPS values of the GPS frame in world coordinates. Formats:\n"
119 " 0=Raw (stamp longitude latitude altitude error bearing)\n"
120 " 1=KML (Google Earth)\n"
121 " --images Export images with stamp as file name.\n"
122 " --images_id Export images with node id as file name.\n"
123 " --ba Do global bundle adjustment before assembling the clouds.\n"
124 " --gain # Gain compensation value (default 1, set 0 to disable).\n"
125 " --gain_gray Do gain estimation compensation on gray channel only (default RGB channels).\n"
126 " --no_blending Disable blending when texturing.\n"
127 " --no_clean Disable cleaning colorless polygons.\n"
128 " --min_cluster # When meshing, filter clusters of polygons with size less than this\n"
129 " threshold (default 200, -1 means keep only biggest contiguous surface).\n"
130 " --low_gain # Low brightness gain 0-100 (default 0).\n"
131 " --high_gain # High brightness gain 0-100 (default 10).\n"
132 " --multiband Enable multiband texturing (AliceVision dependency required). Used with --texture option.\n"
133 " --multiband_downscale # Downscaling reduce the texture quality but speed up the computation time (default 2).\n"
134 " --multiband_contrib \"# # # # \" Number of contributions per frequency band for the\n"
135 " multi-band blending, should be 4 values! (default \"1 5 10 0\").\n"
136 " --multiband_unwrap # Method to unwrap input mesh:\n"
137 " 0=basic (default, >600k faces, fast)\n"
138 " 1=ABF (<=300k faces, generate 1 atlas)\n"
139 " 2=LSCM (<=600k faces, optimize space).\n"
140 " --multiband_fillholes Fill Texture holes with plausible values.\n"
141 " --multiband_padding # Texture edge padding size in pixel (0-100) (default 5).\n"
142 " --multiband_scorethr # 0 to disable filtering based on threshold to relative best score (0.0-1.0). (default 0.1).\n"
143 " --multiband_anglethr # 0 to disable angle hard threshold filtering (0.0, 180.0) (default 90.0).\n"
144 " --multiband_forcevisible Triangle visibility is based on the union of vertices visibility.\n"
145 " --poisson_depth # Set Poisson depth for mesh reconstruction.\n"
146 " --poisson_size # Set target polygon size when computing Poisson's depth for mesh reconstruction (default 0.03 m).\n"
147 " --max_polygons # Maximum polygons when creating a mesh (default 300000, set 0 for no limit).\n"
148 " --min_range # Minimum range of the created clouds (default 0 m).\n"
149 " --max_range # Maximum range of the created clouds (default 4 m, 0 m with --scan).\n"
150 " --decimation # Depth image decimation before creating the clouds (default 4, 1 with --scan).\n"
151 " --voxel # Voxel size of the created clouds (default 0.01 m, 0 m with --scan).\n"
152 " --ground_normals_up # Flip ground normals up if close to -z axis (default 0, 0=disabled, value should be >0 and <1, typical 0.9).\n"
153 " --noise_radius # Noise filtering search radius (default 0, 0=disabled).\n"
154 " --noise_k # Noise filtering minimum neighbors in search radius (default 5, 0=disabled).\n"
155 " --prop_radius_factor # Proportional radius filter factor (default 0, 0=disabled). Start tuning from 0.01.\n"
156 " --prop_radius_scale # Proportional radius filter neighbor scale (default 2).\n"
157 " --random_samples # Number of output samples using a random filter (default 0, 0=disabled).\n"
158 " --color_radius # Radius used to colorize polygons (default 0.05 m, 0 m with --scan). Set 0 for nearest color.\n"
159 " --scan Use laser scan for the point cloud.\n"
160 " --save_in_db Save resulting assembled point cloud or mesh in the database.\n"
161 " --xmin # Minimum range on X axis to keep nodes to export.\n"
162 " --xmax # Maximum range on X axis to keep nodes to export.\n"
163 " --ymin # Minimum range on Y axis to keep nodes to export.\n"
164 " --ymax # Maximum range on Y axis to keep nodes to export.\n"
165 " --zmin # Minimum range on Z axis to keep nodes to export.\n"
166 " --zmax # Maximum range on Z axis to keep nodes to export.\n"
167 " --density_radius # Filter poses in a fixed radius (m) to keep only one to be exported in the assembled cloud.\n"
168 " --density_angle # Filter poses up to angle (deg) in the --density_radius.\n"
169 " --filter_ceiling # Filter points over a custom height (default 0 m, 0=disabled).\n"
170 " --filter_floor # Filter points below a custom height (default 0 m, 0=disabled).\n"
179 virtual bool callback(
const std::string & msg)
const
182 printf(
"%s\n",
msg.c_str());
199 bool exportCloud =
false;
200 bool exportMesh =
false;
201 bool texture =
false;
203 bool doGainCompensationRGB =
true;
205 bool doBlending =
true;
207 int minCluster = 200;
208 int poissonDepth = 0;
209 float poissonSize = 0.03;
210 int maxPolygons = 300000;
212 float minRange = 0.0f;
213 float maxRange = -1.0f;
214 float voxelSize = -1.0f;
215 float groundNormalsUp = 0.0f;
216 float noiseRadius = 0.0f;
217 int noiseMinNeighbors = 5;
218 float proportionalRadiusFactor = 0.0f;
219 float proportionalRadiusScale = 2.0f;
220 int randomSamples = 0;
221 int textureSize = 8192;
222 int textureCount = 1;
223 float textureRange = 0;
224 float textureAngle = 0;
225 float textureDepthError = 0;
226 std::vector<float> textureRoiRatios;
227 bool distanceToCamPolicy =
false;
228 int laplacianThr = 0;
229 bool multiband =
false;
230 int multibandDownScale = 2;
231 std::string multibandNbContrib =
"1 5 10 0";
232 int multibandUnwrap = 0;
233 bool multibandFillHoles =
false;
234 int multibandPadding = 5;
235 double multibandBestScoreThr = 0.1;
236 double multibandAngleHardthr = 90;
237 bool multibandForceVisible =
false;
238 float colorRadius = -1.0f;
239 bool cloudFromScan =
false;
240 bool saveInDb =
false;
241 int lowBrightnessGain = 0;
242 int highBrightnessGain = 10;
243 bool camProjection =
false;
244 bool camProjectionKeepAll =
false;
245 int cameraProjDecimation = 1;
246 std::string cameraProjMask;
248 bool exportPosesCamera =
false;
249 bool exportPosesScan =
false;
250 bool exportPosesGt =
false;
251 bool exportPosesGps =
false;
252 int exportPosesFormat = 11;
254 bool exportImages =
false;
255 bool exportImagesId =
false;
256 int optimizationApproach = 0;
257 std::string outputName;
258 std::string outputDir;
260 float densityRadius = 0.0f;
261 float densityAngle = 0.0f;
262 float filter_ceiling = 0.0f;
263 float filter_floor = 0.0f;
264 for(
int i=1;
i<argc; ++
i)
266 if(std::strcmp(
argv[
i],
"--help") == 0)
270 else if(std::strcmp(
argv[
i],
"--output") == 0)
275 outputName =
argv[
i];
282 else if(std::strcmp(
argv[
i],
"--output_dir") == 0)
294 else if(std::strcmp(
argv[
i],
"--bin") == 0)
296 printf(
"No need to set --bin anymore, ply are now automatically exported in binary by default. Set --ascii to export as text.\n");
298 else if(std::strcmp(
argv[
i],
"--ascii") == 0)
302 else if(std::strcmp(
argv[
i],
"--las") == 0)
306 #elif defined(RTABMAP_LIBLAS)
307 printf(
"\"--las\" option cannot be used with libLAS because the cloud has normals, build RTAB-Map with PDAL support to export in las with normals. Will export in PLY...\n");
309 printf(
"\"--las\" option cannot be used because RTAB-Map is not built with PDAL support. Will export in PLY...\n");
313 else if(std::strcmp(
argv[
i],
"--cloud") == 0)
317 else if(std::strcmp(
argv[
i],
"--mesh") == 0)
321 else if(std::strcmp(
argv[
i],
"--texture") == 0)
325 else if(std::strcmp(
argv[
i],
"--texture_size") == 0)
338 else if(std::strcmp(
argv[
i],
"--texture_count") == 0)
350 else if(std::strcmp(
argv[
i],
"--texture_range") == 0)
362 else if(std::strcmp(
argv[
i],
"--texture_angle") == 0)
374 else if(std::strcmp(
argv[
i],
"--texture_depth_error") == 0)
386 else if(std::strcmp(
argv[
i],
"--texture_roi_ratios") == 0)
391 std::list<std::string> strValues =
uSplit(
argv[
i],
' ');
392 if(strValues.size() != 4)
394 printf(
"The number of values must be 4 (roi=\"%s\")\n",
argv[
i]);
399 std::vector<float> tmpValues(4);
401 for(std::list<std::string>::iterator jter = strValues.begin(); jter!=strValues.end(); ++jter)
407 if(tmpValues[0] >= 0 && tmpValues[0] < 1 && tmpValues[0] < 1.0
f-tmpValues[1] &&
408 tmpValues[1] >= 0 && tmpValues[1] < 1 && tmpValues[1] < 1.0
f-tmpValues[0] &&
409 tmpValues[2] >= 0 && tmpValues[2] < 1 && tmpValues[2] < 1.0
f-tmpValues[3] &&
410 tmpValues[3] >= 0 && tmpValues[3] < 1 && tmpValues[3] < 1.0
f-tmpValues[2])
412 textureRoiRatios = tmpValues;
416 printf(
"The roi ratios are not valid (roi=\"%s\")\n",
argv[
i]);
426 else if(std::strcmp(
argv[
i],
"--texture_d2c") == 0)
428 distanceToCamPolicy =
true;
430 else if(std::strcmp(
argv[
i],
"--texture_blur") == 0)
442 else if(std::strcmp(
argv[
i],
"--cam_projection") == 0)
444 camProjection =
true;
446 else if(std::strcmp(
argv[
i],
"--cam_projection_keep_all") == 0)
448 camProjectionKeepAll =
true;
450 else if(std::strcmp(
argv[
i],
"--cam_projection_decimation") == 0)
456 if(cameraProjDecimation<1)
458 printf(
"--cam_projection_decimation cannot be <1! value=\"%s\"\n",
argv[
i]);
467 else if(std::strcmp(
argv[
i],
"--cam_projection_mask") == 0)
472 cameraProjMask =
argv[
i];
475 printf(
"--cam_projection_mask is set with a file not existing or don't have permissions to open it. Path=\"%s\"\n",
argv[
i]);
484 else if(std::strcmp(
argv[
i],
"--poses") == 0)
488 else if(std::strcmp(
argv[
i],
"--poses_camera") == 0)
490 exportPosesCamera =
true;
492 else if(std::strcmp(
argv[
i],
"--poses_scan") == 0)
494 exportPosesScan =
true;
496 else if(std::strcmp(
argv[
i],
"--poses_gt") == 0)
498 exportPosesGt =
true;
500 else if(std::strcmp(
argv[
i],
"--poses_gps") == 0)
502 exportPosesGps =
true;
504 else if(std::strcmp(
argv[
i],
"--poses_format") == 0)
516 else if(std::strcmp(
argv[
i],
"--gps") == 0)
522 if(exportGps<0 || exportGps>1)
524 printf(
"Wrong GPS format (%d), should be 0 or 1\n", exportGps);
533 else if(std::strcmp(
argv[
i],
"--opt") == 0)
539 if(optimizationApproach<0 || optimizationApproach >3)
541 printf(
"Invalid --opt (%d)\n", optimizationApproach);
550 else if(std::strcmp(
argv[
i],
"--images") == 0)
554 else if(std::strcmp(
argv[
i],
"--images_id") == 0)
557 exportImagesId =
true;
559 else if(std::strcmp(
argv[
i],
"--ba") == 0)
563 else if(std::strcmp(
argv[
i],
"--gain_gray") == 0)
565 doGainCompensationRGB =
false;
567 else if(std::strcmp(
argv[
i],
"--gain") == 0)
580 else if(std::strcmp(
argv[
i],
"--no_blending") == 0)
584 else if(std::strcmp(
argv[
i],
"--no_clean") == 0)
588 else if(std::strcmp(
argv[
i],
"--min_cluster") == 0)
600 else if(std::strcmp(
argv[
i],
"--multiband") == 0)
602 #ifdef RTABMAP_ALICE_VISION
605 printf(
"\"--multiband\" option cannot be used because RTAB-Map is not built with AliceVision support. Ignoring multiband...\n");
608 else if(std::strcmp(
argv[
i],
"--multiband_fillholes") == 0)
610 multibandFillHoles =
true;
612 else if(std::strcmp(
argv[
i],
"--multiband_downscale") == 0)
624 else if(std::strcmp(
argv[
i],
"--multiband_contrib") == 0)
631 printf(
"--multiband_contrib has wrong format! value=\"%s\"\n",
argv[
i]);
634 multibandNbContrib =
argv[
i];
641 else if(std::strcmp(
argv[
i],
"--multiband_unwrap") == 0)
653 else if(std::strcmp(
argv[
i],
"--multiband_padding") == 0)
665 else if(std::strcmp(
argv[
i],
"--multiband_forcevisible") == 0)
667 multibandForceVisible =
true;
669 else if(std::strcmp(
argv[
i],
"--multiband_scorethr") == 0)
681 else if(std::strcmp(
argv[
i],
"--multiband_anglethr") == 0)
693 else if(std::strcmp(
argv[
i],
"--poisson_depth") == 0)
705 else if(std::strcmp(
argv[
i],
"--poisson_size") == 0)
717 else if(std::strcmp(
argv[
i],
"--max_polygons") == 0)
729 else if(std::strcmp(
argv[
i],
"--min_range") == 0)
741 else if(std::strcmp(
argv[
i],
"--max_range") == 0)
753 else if(std::strcmp(
argv[
i],
"--decimation") == 0)
765 else if(std::strcmp(
argv[
i],
"--voxel") == 0)
777 else if(std::strcmp(
argv[
i],
"--ground_normals_up") == 0)
789 else if(std::strcmp(
argv[
i],
"--noise_radius") == 0)
801 else if(std::strcmp(
argv[
i],
"--noise_k") == 0)
813 else if(std::strcmp(
argv[
i],
"--prop_radius_factor") == 0)
825 else if(std::strcmp(
argv[
i],
"--prop_radius_scale") == 0)
831 UASSERT_MSG(proportionalRadiusScale>=1.0
f,
"--prop_radius_scale should be >= 1.0");
838 else if(std::strcmp(
argv[
i],
"--random_samples") == 0)
850 else if(std::strcmp(
argv[
i],
"--color_radius") == 0)
862 else if(std::strcmp(
argv[
i],
"--scan") == 0)
864 cloudFromScan =
true;
866 else if(std::strcmp(
argv[
i],
"--save_in_db") == 0)
870 else if(std::strcmp(
argv[
i],
"--low_gain") == 0)
882 else if(std::strcmp(
argv[
i],
"--high_gain") == 0)
894 else if(std::strcmp(
argv[
i],
"--xmin") == 0)
906 else if(std::strcmp(
argv[
i],
"--xmax") == 0)
918 else if(std::strcmp(
argv[
i],
"--ymin") == 0)
930 else if(std::strcmp(
argv[
i],
"--ymax") == 0)
942 else if(std::strcmp(
argv[
i],
"--zmin") == 0)
954 else if(std::strcmp(
argv[
i],
"--zmax") == 0)
966 else if(std::strcmp(
argv[
i],
"--density_radius") == 0)
979 else if(std::strcmp(
argv[
i],
"--density_angle") == 0)
992 else if(std::strcmp(
argv[
i],
"--filter_ceiling") == 0)
998 if(filter_floor!=0.0
f && filter_ceiling != 0.0
f && filter_ceiling<filter_floor)
1000 printf(
"Option --filter_ceiling (%f) should be higher than --filter_floor option (%f)!\n", filter_ceiling, filter_floor);
1009 else if(std::strcmp(
argv[
i],
"--filter_floor") == 0)
1015 if(filter_floor!=0.0
f && filter_ceiling != 0.0
f && filter_ceiling<filter_floor)
1017 printf(
"Option --filter_ceiling (%f) should be higher than --filter_floor option (%f)!\n", filter_ceiling, filter_floor);
1030 decimation = cloudFromScan?1:4;
1034 maxRange = cloudFromScan?0:4;
1036 if(voxelSize < 0.0
f)
1038 voxelSize = cloudFromScan?0:0.01f;
1040 if(colorRadius < 0.0
f)
1042 colorRadius = cloudFromScan?0:0.05f;
1049 printf(
"Option --multiband is not supported with --save_in_db option, disabling multiband...\n");
1054 printf(
"Option --texture_count > 1 is not supported with --save_in_db option, setting texture_count to 1...\n");
1064 exportPosesCamera ||
1070 printf(
"Launching the tool without any required option(s) is deprecated. We will add --cloud to keep compatibilty with old behavior.\n");
1074 if(texture && !exportMesh)
1076 printf(
"To use --texture option, --mesh should be also enabled. Enabling --mesh.\n");
1082 std::string dbPath =
argv[argc-1];
1086 UERROR(
"File \"%s\" doesn't exist!", dbPath.c_str());
1100 UERROR(
"Cannot open database %s!", dbPath.c_str());
1108 printf(
"Added custom parameter %s=%s\n",
iter->first.c_str(),
iter->second.c_str());
1113 printf(
"Opening database \"%s\"...\n", dbPath.c_str());
1116 if(!dbDriver->openConnection(dbPath))
1118 printf(
"Failed to open database \"%s\"!\n", dbPath.c_str());
1121 printf(
"Opening database \"%s\"... done (%fs).\n", dbPath.c_str(),
timer.ticks());
1123 std::map<int, Transform> optimizedPoses;
1124 std::map<int, Transform> odomPoses;
1125 std::multimap<int, Link> links;
1126 dbDriver->getAllOdomPoses(odomPoses,
true);
1127 dbDriver->getAllLinks(links,
true,
true);
1128 if(optimizationApproach == 3 || !(exportCloud || exportMesh ||
exportPoses || exportPosesCamera || exportPosesScan))
1131 optimizedPoses = odomPoses;
1132 if(optimizationApproach == 3)
1134 printf(
"Loaded %d odometry poses from database.\n", (
int)odomPoses.size());
1139 if(optimizationApproach == 2)
1141 printf(
"Loading optimized poses from database...\n");
1142 optimizedPoses = dbDriver->loadOptimizedPoses();
1143 if(optimizedPoses.empty())
1145 printf(
"The are no saved optimized poses in the database, we will do full global optimization instead.\n");
1146 optimizationApproach = 0;
1150 printf(
"Loading optimized poses from database... done (%d optimized poses loaded).\n", (
int)optimizedPoses.size());
1153 if(optimizationApproach <= 1)
1155 std::string optimizationApproachStr = optimizationApproach==1?
"Iterative global optimization":
"Full global optimization";
1156 printf(
"Optimizing the map (%s)...\n", optimizationApproachStr.c_str());
1157 if(odomPoses.empty())
1159 printf(
"The are no odometry poses!? Aborting...\n");
1163 std::map<int, Transform> posesOut;
1164 std::multimap<int, Link> linksOut;
1165 UASSERT(odomPoses.lower_bound(1) != odomPoses.end());
1169 double markerPriorsLinearVariance = Parameters::defaultMarkerPriorsVarianceLinear();
1170 double markerPriorsAngularVariance = Parameters::defaultMarkerPriorsVarianceAngular();
1171 std::map<int, Transform> markerPriors;
1172 Parameters::parse(parameters, Parameters::kMarkerPriorsVarianceLinear(), markerPriorsLinearVariance);
1173 UASSERT(markerPriorsLinearVariance>0.0
f);
1174 Parameters::parse(parameters, Parameters::kMarkerPriorsVarianceAngular(), markerPriorsAngularVariance);
1175 UASSERT(markerPriorsAngularVariance>0.0
f);
1176 std::string markerPriorsStr;
1179 std::list<std::string> strList =
uSplit(markerPriorsStr,
'|');
1180 for(std::list<std::string>::iterator
iter=strList.begin();
iter!=strList.end(); ++
iter)
1182 std::string markerStr = *
iter;
1183 while(!markerStr.empty() && !
uIsDigit(markerStr[0]))
1185 markerStr.erase(markerStr.begin());
1187 if(!markerStr.empty())
1192 if(!
prior.isNull() &&
id>0)
1194 markerPriors.insert(std::make_pair(-
id,
prior));
1198 UERROR(
"Failed to parse element \"%s\" in parameter %s", markerStr.c_str(), Parameters::kMarkerPriors().c_str());
1201 else if(!
iter->empty())
1203 UERROR(
"Failed to parse parameter %s, value=\"%s\"", Parameters::kMarkerPriors().
c_str(),
iter->c_str());
1207 for(std::multimap<int, rtabmap::Link>::iterator
iter=links.begin();
iter!=links.end(); ++
iter)
1212 int markerId =
iter->second.to();
1213 if(odomPoses.find(
iter->second.from()) != odomPoses.end() && odomPoses.find(markerId) == odomPoses.end())
1215 odomPoses.insert(std::make_pair(markerId, odomPoses.at(
iter->second.from())*
iter->second.transform()));
1217 if(markerPriors.find(markerId) != markerPriors.end())
1219 cv::Mat infMatrix = cv::Mat::eye(6, 6, CV_64FC1);
1220 infMatrix(cv::Range(0,3), cv::Range(0,3)) /= markerPriorsLinearVariance;
1221 infMatrix(cv::Range(3,6), cv::Range(3,6)) /= markerPriorsAngularVariance;
1222 links.insert(std::make_pair(markerId,
Link(markerId, markerId,
Link::kPosePrior, markerPriors.at(markerId), infMatrix)));
1223 printf(
"Added prior %d : %s (variance: lin=%f ang=%f)\n", markerId, markerPriors.at(markerId).prettyPrint().c_str(),
1224 markerPriorsLinearVariance, markerPriorsAngularVariance);
1231 optimizer->getConnectedGraph(odomPoses.lower_bound(1)->first, odomPoses, links, posesOut, linksOut);
1232 if(optimizationApproach == 1)
1234 std::list<std::map<int, Transform> > intermediateGraphes;
1235 optimizedPoses = optimizer->optimize(odomPoses.lower_bound(1)->first, posesOut, linksOut, &intermediateGraphes);
1239 optimizedPoses = optimizer->optimize(odomPoses.lower_bound(1)->first, posesOut, linksOut);
1241 printf(
"Optimizing the map (%s)... done (%fs, poses=%d, links=%d).\n", optimizationApproachStr.c_str(),
timer.ticks(), (
int)optimizedPoses.size(), (
int)linksOut.size());
1244 if(optimizedPoses.empty())
1246 printf(
"The optimized graph is empty!? Aborting...\n");
1252 cv::Vec3f minP,maxP;
1254 printf(
"Filtering poses (range: x=%.1f<->%.1f, y=%.1f<->%.1f, z=%.1f<->%.1f, map size=%.1f x %.1f x %.1f, map min/max: [%.1f, %.1f, %.1f] [%.1f, %.1f, %.1f])...\n",
1256 maxP[0]-minP[0],maxP[1]-minP[1],maxP[2]-minP[2],
1257 minP[0],minP[1],minP[2],maxP[0],maxP[1],maxP[2]);
1258 std::map<int, Transform> posesFiltered;
1259 for(std::map<int, Transform>::const_iterator
iter=optimizedPoses.begin();
iter!=optimizedPoses.end(); ++
iter)
1261 bool ignore =
false;
1276 posesFiltered.insert(*
iter);
1280 printf(
"Filtering poses... done! %d/%d remaining.\n", (
int)posesFiltered.size(), (
int)optimizedPoses.size());
1281 optimizedPoses = posesFiltered;
1282 if(optimizedPoses.empty())
1284 printf(
"All poses filtered! Exiting.\n");
1291 printf(
"Global bundle adjustment...\n");
1293 UASSERT(optimizedPoses.lower_bound(1) != optimizedPoses.end());
1296 for(std::map<int, Transform>::iterator
iter=optimizedPoses.lower_bound(1);
iter!=optimizedPoses.end(); ++
iter)
1298 ids.push_back(
iter->first);
1300 std::list<Signature *> signatures;
1301 dbDriver->loadSignatures(ids, signatures);
1302 std::map<int, Signature>
nodes;
1303 for(std::list<Signature *>::iterator
iter=signatures.begin();
iter!=signatures.end(); ++
iter)
1305 nodes.insert(std::make_pair((*iter)->id(), *(*
iter)));
1307 optimizedPoses = ((
Optimizer*)&
g2o)->optimizeBA(optimizedPoses.lower_bound(1)->first, optimizedPoses, links,
nodes,
true);
1308 printf(
"Global bundle adjustment... done (%fs).\n",
timer.ticks());
1312 std::string outputDirectory = outputDir.empty()?
UDirectory::getDir(dbPath):outputDir;
1317 std::string baseName = outputName.empty()?
uSplit(
UFile::getName(dbPath),
'.').front():outputName;
1320 if(exportCloud || exportMesh)
1322 printf(
"Create and assemble the clouds...\n");
1324 else if(exportImages || exportImagesId)
1326 printf(
"Export images...\n");
1328 pcl::PointCloud<pcl::PointXYZRGB>::Ptr assembledCloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
1329 pcl::PointCloud<pcl::PointXYZI>::Ptr assembledCloudI(
new pcl::PointCloud<pcl::PointXYZI>);
1330 std::map<int, rtabmap::Transform> robotPoses;
1331 std::vector<std::map<int, rtabmap::Transform> >
cameraPoses;
1332 std::map<int, rtabmap::Transform> scanPoses;
1333 std::map<int, rtabmap::Transform> gtPoses;
1334 std::map<int, rtabmap::Transform> gpsPoses;
1335 std::map<int, double> gpsStamps;
1337 std::map<int, rtabmap::GPS> gpsValues;
1338 std::map<int, double> cameraStamps;
1339 std::map<int, std::vector<rtabmap::CameraModel> > cameraModels;
1340 std::map<int, cv::Mat> cameraDepths;
1341 int imagesExported = 0;
1342 std::vector<int> rawViewpointIndices;
1343 std::map<int, Transform> rawViewpoints;
1344 std::map<int, Transform> densityPoses;
1345 if(densityRadius && (exportCloud || exportMesh))
1348 printf(
"Keeping %d/%d poses after density filtering (--density_radius = %f --density_angle = %f).\n",
1349 (
int)densityPoses.size(),
1350 (
int)optimizedPoses.size(),
1354 int processedNodes = 0;
1355 int lastPercent = 0;
1356 for(std::map<int, Transform>::iterator
iter=optimizedPoses.begin();
iter!=optimizedPoses.end(); ++
iter)
1361 robotPoses.insert(*
iter);
1362 cameraStamps.insert(std::make_pair(
iter->first, 0));
1370 std::vector<float>
v;
1374 dbDriver->getNodeInfo(
iter->first,
p,
m, weight, l, stamp, gt,
v, gps,
s);
1377 bool loadImages = ((exportCloud || exportMesh) && (!cloudFromScan || texture || camProjection)) || exportImages;
1378 bool loadScan = ((exportCloud || exportMesh) && cloudFromScan) || exportPosesScan;
1381 dbDriver->getNodeData(
1391 std::vector<CameraModel> models;
1392 std::vector<StereoCameraModel> stereoModels;
1393 if(loadImages || exportPosesCamera)
1395 dbDriver->getCalibration(
iter->first, models, stereoModels);
1398 if(exportCloud || exportMesh || exportImages)
1400 bool densityFiltered = !densityPoses.
empty() && densityPoses.find(
iter->first) == densityPoses.end();
1403 pcl::IndicesPtr
indices(
new std::vector<int>);
1404 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
1405 pcl::PointCloud<pcl::PointXYZI>::Ptr cloudI;
1408 if(!densityFiltered && cloudFromScan && (exportCloud || exportMesh))
1411 data.uncompressData(exportImages?&rgb:0, (texture||exportImages)&&!
data.depthOrRightCompressed().empty()?&depth:0, &scan);
1414 printf(
"Node %d doesn't have scan data, empty cloud is created.\n",
iter->first);
1416 if(decimation>1 || minRange>0.0
f || maxRange)
1423 if(noiseRadius>0.0
f && noiseMinNeighbors>0)
1431 if(noiseRadius>0.0
f && noiseMinNeighbors>0)
1439 data.uncompressData(&rgb, &depth);
1440 if(!densityFiltered && (exportCloud || exportMesh))
1444 printf(
"Node %d doesn't have depth or stereo data, empty cloud is "
1445 "created (if you want to create point cloud from scan, use --scan option).\n",
iter->first);
1453 if(noiseRadius>0.0
f && noiseMinNeighbors>0)
1461 if(exportImages && !rgb.empty())
1463 std::string dirSuffix = (depth.type() != CV_16UC1 && depth.type() != CV_32FC1 && !depth.empty())?
"left":
"rgb";
1464 std::string dir = outputDirectory+
"/"+baseName+
"_"+dirSuffix;
1469 cv::imwrite(outputPath, rgb);
1474 cv::Mat depthExported = depth;
1475 if(depth.type() != CV_16UC1 && depth.type() != CV_32FC1)
1478 dir = outputDirectory+
"/"+baseName+
"_right";
1483 dir = outputDirectory+
"/"+baseName+
"_depth";
1484 if(depth.type() == CV_32FC1)
1494 cv::imwrite(outputPath, depthExported);
1498 for(
size_t i=0;
i<models.size(); ++
i)
1502 if(models.size() > 1) {
1505 model.setName(modelName);
1506 std::string dir = outputDirectory+
"/"+baseName+
"_calib";
1512 for(
size_t i=0;
i<stereoModels.size(); ++
i)
1516 if(stereoModels.size() > 1) {
1519 model.setName(modelName,
"left",
"right");
1520 std::string dir = outputDirectory+
"/"+baseName+
"_calib";
1528 if(exportCloud || exportMesh)
1532 if(cloud.get() && !cloud->empty())
1534 else if(cloudI.get() && !cloudI->empty())
1537 if(cloud.get() && !cloud->empty())
1539 else if(cloudI.get() && !cloudI->empty())
1542 if(filter_ceiling != 0.0 || filter_floor != 0.0
f)
1544 if(cloud.get() && !cloud->empty())
1548 if(cloudI.get() && !cloudI->empty())
1557 rawViewpoints.insert(std::make_pair(
iter->first, lidarViewpoint));
1559 else if(!models.empty() && !models[0].localTransform().isNull())
1561 Transform cameraViewpoint =
iter->second * models[0].localTransform();
1562 rawViewpoints.insert(std::make_pair(
iter->first, cameraViewpoint));
1564 else if(!stereoModels.empty() && !stereoModels[0].localTransform().isNull())
1566 Transform cameraViewpoint =
iter->second * stereoModels[0].localTransform();
1567 rawViewpoints.insert(std::make_pair(
iter->first, cameraViewpoint));
1571 rawViewpoints.insert(*
iter);
1574 if(cloud.get() && !cloud->empty())
1576 if(assembledCloud->empty())
1578 *assembledCloud = *cloud;
1582 *assembledCloud += *cloud;
1584 rawViewpointIndices.resize(assembledCloud->size(),
iter->first);
1586 else if(cloudI.get() && !cloudI->empty())
1588 if(assembledCloudI->empty())
1590 *assembledCloudI = *cloudI;
1594 *assembledCloudI += *cloudI;
1596 rawViewpointIndices.resize(assembledCloudI->size(),
iter->first);
1598 if(texture && !depth.empty() && (depth.type() == CV_16UC1 || depth.type() == CV_32FC1))
1600 cameraDepths.insert(std::make_pair(
iter->first, depth));
1607 for(
size_t i=0;
i<stereoModels.size(); ++
i)
1609 models.push_back(stereoModels[
i].left());
1613 robotPoses.insert(std::make_pair(
iter->first,
iter->second));
1614 cameraStamps.insert(std::make_pair(
iter->first, stamp));
1615 if(models.empty() && weight == -1 && !cameraModels.empty())
1618 models = cameraModels.rbegin()->second;
1622 if(!
data.imageCompressed().empty())
1624 cameraModels.insert(std::make_pair(
iter->first, models));
1626 if(exportPosesCamera)
1632 UASSERT_MSG(models.size() ==
cameraPoses.size(),
"Not all nodes have same number of cameras to export camera poses.");
1633 for(
size_t i=0;
i<models.size(); ++
i)
1639 if(exportPosesScan && !
data.laserScanCompressed().empty())
1641 scanPoses.insert(std::make_pair(
iter->first,
iter->second*
data.laserScanCompressed().localTransform()));
1644 if(exportPosesGps || exportGps>=0)
1646 if(gps.
stamp() > 0.0)
1650 cv::Point3f
p(0.0
f,0.0
f,0.0
f);
1651 if(!gpsPoses.empty())
1661 gpsPoses.insert(std::make_pair(
iter->first, pose));
1665 gpsValues.insert(std::make_pair(
iter->first, gps));
1667 gpsStamps.insert(std::make_pair(
iter->first, gps.
stamp()));
1671 if(exportPosesGt && !gt.
isNull())
1673 gtPoses.insert(std::make_pair(
iter->first, gt));
1676 if(optimizedPoses.size() >= 500)
1679 int percent = processedNodes*100/(
int)optimizedPoses.size();
1680 if(percent != lastPercent)
1682 printf(
"Processed %d/%d (%d%%) nodes...\n",
1684 (
int)optimizedPoses.size(),
1686 lastPercent = percent;
1690 if(exportCloud || exportMesh)
1692 printf(
"Create and assemble the clouds... done (%fs, %d points).\n",
timer.ticks(), !assembledCloud->empty()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1695 if(exportImages || exportImagesId)
1697 printf(
"%d images exported!\n", imagesExported);
1698 if(!(exportCloud || exportMesh ||
exportPoses || exportPosesCamera || exportPosesScan)) {
1715 cv::Vec3f vmin, vmax;
1717 printf(
"Saved %d poses to database! (min=[%f,%f,%f] max=[%f,%f,%f])\n",
1718 (
int)robotPoses.size(),
1719 vmin[0], vmin[1], vmin[2],
1720 vmax[0], vmax[1], vmax[2]);
1724 std::string posesExt = (exportPosesFormat==3?
"toro":exportPosesFormat==4?
"g2o":
"txt");
1727 std::string outputPath=outputDirectory+
"/"+baseName+
"_poses." + posesExt;
1728 if(robotPoses.begin() != robotPoses.lower_bound(1) && !(exportPosesFormat == 4 || exportPosesFormat == 11))
1730 printf(
"Note that landmarks won't be exported because --poses_format is not 4 or 11 (currently using %d).\n", exportPosesFormat);
1734 std::map<int, Transform>(robotPoses.lower_bound(1), robotPoses.end()),
1736 std::map<int, double>(cameraStamps.lower_bound(1), cameraStamps.end()));
1742 cv::Vec3f vmin, vmax;
1744 printf(
"%d poses exported to \"%s\". (min=[%f,%f,%f] max=[%f,%f,%f])\n",
1745 (
int)robotPoses.size(),
1747 vmin[0], vmin[1], vmin[2],
1748 vmax[0], vmax[1], vmax[2]);
1750 if(exportPosesCamera)
1754 std::string outputPath;
1756 outputPath = outputDirectory+
"/"+baseName+
"_camera_poses." + posesExt;
1758 outputPath = outputDirectory+
"/"+baseName+
"_camera_poses_"+
uNumber2Str((
int)
i)+
"." + posesExt;
1760 cv::Vec3f vmin, vmax;
1762 printf(
"%d camera poses exported to \"%s\". (min=[%f,%f,%f] max=[%f,%f,%f])\n",
1765 vmin[0], vmin[1], vmin[2],
1766 vmax[0], vmax[1], vmax[2]);
1771 std::string outputPath=outputDirectory+
"/"+baseName+
"_scan_poses." + posesExt;
1775 printf(
"%d scan poses exported to \"%s\". (min=[%f,%f,%f] max=[%f,%f,%f])\n",
1776 (
int)scanPoses.size(),
1783 std::string outputPath=outputDirectory+
"/"+baseName+
"_gps_poses." + posesExt;
1785 printf(
"%d GPS poses exported to \"%s\".\n",
1786 (
int)gpsPoses.size(),
1787 outputPath.c_str());
1791 std::string outputPath=outputDirectory+
"/"+baseName+
"_gt_poses." + posesExt;
1793 printf(
"%d scan poses exported to \"%s\".\n",
1794 (
int)gtPoses.size(),
1795 outputPath.c_str());
1799 std::string outputPath=outputDirectory+
"/"+baseName+
"_gps." + (exportGps==0?
"txt":
"kml");
1801 printf(
"%d GPS values exported to \"%s\".\n",
1802 (
int)gpsValues.size(),
1803 outputPath.c_str());
1807 if(!(exportCloud || exportMesh))
1813 if(!assembledCloud->empty() || !assembledCloudI->empty())
1815 if(proportionalRadiusFactor>0.0
f && proportionalRadiusScale>=1.0
f)
1817 printf(
"Proportional radius filtering of the assembled cloud... (factor=%f scale=%f, %d points)\n", proportionalRadiusFactor, proportionalRadiusScale, !assembledCloud->empty()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1819 if(!assembledCloud->empty())
1822 pcl::PointCloud<pcl::PointXYZRGB> tmp;
1823 pcl::copyPointCloud(*assembledCloud, *
indices, tmp);
1824 *assembledCloud = tmp;
1826 else if(!assembledCloudI->empty())
1829 pcl::PointCloud<pcl::PointXYZI> tmp;
1830 pcl::copyPointCloud(*assembledCloudI, *
indices, tmp);
1831 *assembledCloudI = tmp;
1835 std::vector<int> rawCameraIndicesTmp(
indices->size());
1836 for (std::size_t
i = 0;
i <
indices->size(); ++
i)
1837 rawCameraIndicesTmp[
i] = rawViewpointIndices[
indices->at(
i)];
1838 rawViewpointIndices = rawCameraIndicesTmp;
1840 printf(
"Proportional radius filtering of the assembled cloud.... done! (%fs, %d points)\n",
timer.ticks(), !assembledCloud->empty()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1843 pcl::PointCloud<pcl::PointXYZ>::Ptr rawAssembledCloud(
new pcl::PointCloud<pcl::PointXYZ>);
1846 printf(
"Voxel grid filtering of the assembled cloud... (voxel=%f, %d points)\n", voxelSize, !assembledCloud->empty()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1847 if(!assembledCloud->empty()) {
1848 pcl::copyPointCloud(*assembledCloud, *rawAssembledCloud);
1851 else if(!assembledCloudI->empty()) {
1852 pcl::copyPointCloud(*assembledCloudI, *rawAssembledCloud);
1855 printf(
"Voxel grid filtering of the assembled cloud.... done! (%fs, %d points)\n",
timer.ticks(), !assembledCloud->empty()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1858 printf(
"Computing normals of the assembled cloud... (k=20, %d points)\n", (
int)assembledCloud->size()?(
int)assembledCloud->size():(
int)assembledCloudI->size());
1859 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloudToExport(
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
1860 pcl::PointCloud<pcl::PointXYZINormal>::Ptr cloudIToExport(
new pcl::PointCloud<pcl::PointXYZINormal>);
1861 if(!assembledCloud->empty()) {
1863 UASSERT(assembledCloud->size() == normals->size());
1864 pcl::concatenateFields(*assembledCloud, *normals, *cloudToExport);
1866 else if(!assembledCloudI->empty()) {
1868 UASSERT(assembledCloudI->size() == normals->size());
1869 pcl::concatenateFields(*assembledCloudI, *normals, *cloudIToExport);
1871 printf(
"Computing normals of the assembled cloud... done! (%fs, %d points)\n",
timer.ticks(), !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
1872 assembledCloud->clear();
1873 assembledCloudI->clear();
1876 printf(
"Adjust normals to viewpoints of the assembled cloud... (%d points)\n", !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
1877 if(!rawAssembledCloud->empty()) {
1878 if(!cloudToExport->empty()) {
1882 rawViewpointIndices,
1886 else if(!cloudIToExport->empty()) {
1890 rawViewpointIndices,
1895 else if(!cloudToExport->empty()) {
1898 rawViewpointIndices,
1902 else if(!cloudIToExport->empty()) {
1905 rawViewpointIndices,
1909 printf(
"Adjust normals to viewpoints of the assembled cloud... (%fs, %d points)\n",
timer.ticks(), !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
1913 printf(
"Random samples filtering of the assembled cloud... (samples=%d, %d points)\n", randomSamples, !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
1914 if(!cloudToExport->empty())
1918 else if(!cloudIToExport->empty())
1922 printf(
"Random samples filtering of the assembled cloud.... done! (%fs, %d points)\n",
timer.ticks(), !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
1925 std::vector<int> pointToCamId;
1926 std::vector<float> pointToCamIntensity;
1927 if(camProjection && !robotPoses.empty())
1929 printf(
"Camera projection... (cameras=%d)\n", (
int)cameraModels.size());
1930 std::map<int, std::vector<rtabmap::CameraModel> > cameraModelsProj;
1931 if(cameraProjDecimation>1)
1933 for(std::map<
int, std::vector<rtabmap::CameraModel> >::
iterator iter=cameraModels.begin();
1934 iter!=cameraModels.end();
1937 std::vector<rtabmap::CameraModel> models;
1938 for(
size_t i=0;
i<
iter->second.size(); ++
i)
1940 models.push_back(
iter->second[
i].scaled(1.0/
double(cameraProjDecimation)));
1942 cameraModelsProj.insert(std::make_pair(
iter->first, models));
1947 cameraModelsProj = cameraModels;
1952 printf(
"Camera projection... projecting cloud to individual cameras (--images option)\n");
1954 pcl::PCLPointCloud2::Ptr cloud2(
new pcl::PCLPointCloud2);
1955 if(!cloudToExport->empty())
1957 pcl::toPCLPointCloud2(*cloudToExport, *cloud2);
1959 else if(!cloudIToExport->empty())
1961 pcl::toPCLPointCloud2(*cloudIToExport, *cloud2);
1964 std::string dir = outputDirectory+
"/"+baseName+
"_depth_from_scan";
1969 for(std::map<
int, std::vector<rtabmap::CameraModel> >::
iterator iter=cameraModelsProj.begin();
1970 iter!=cameraModelsProj.end();
1973 cv::Mat depth(
iter->second.front().imageHeight(),
iter->second.front().imageWidth()*
iter->second.size(), CV_32FC1);
1974 for(
size_t i=0;
i<
iter->second.size(); ++
i)
1977 iter->second.at(
i).imageSize(),
1978 iter->second.at(
i).K(),
1980 robotPoses.at(
iter->first) *
iter->second.at(
i).localTransform());
1981 subDepth.copyTo(depth(
cv::Range::all(), cv::Range(
i*
iter->second.front().imageWidth(), (
i+1)*
iter->second.front().imageWidth())));
1986 cv::imwrite(outputPath, depth);
1991 if(!cameraProjMask.empty())
1993 projMask = cv::imread(cameraProjMask, cv::IMREAD_GRAYSCALE);
1994 if(cameraProjDecimation>1)
1996 cv::Mat
out = projMask;
1997 cv::resize(projMask,
out, cv::Size(), 1.0
f/
float(cameraProjDecimation), 1.0
f/
float(cameraProjDecimation), cv::INTER_NEAREST);
2002 printf(
"Camera projection... projecting cloud to all cameras\n");
2003 pointToCamId.resize(!cloudToExport->empty()?cloudToExport->size():cloudIToExport->size());
2004 std::vector<std::pair< std::pair<int, int>, pcl::PointXY> > pointToPixel;
2005 if(!cloudToExport->empty())
2015 distanceToCamPolicy,
2018 else if(!cloudIToExport->empty())
2028 distanceToCamPolicy,
2030 pointToCamIntensity.resize(pointToPixel.size());
2033 printf(
"Camera projection... coloring the cloud\n");
2035 UASSERT(pointToPixel.empty() || pointToPixel.size() == pointToCamId.size());
2036 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr assembledCloudValidPoints(
new pcl::PointCloud<pcl::PointXYZRGBNormal>());
2037 assembledCloudValidPoints->resize(pointToCamId.size());
2040 int coloredPoints = 0;
2041 for(std::map<int, rtabmap::Transform>::iterator
iter=robotPoses.lower_bound(1);
iter!=robotPoses.end(); ++
iter)
2043 int nodeID =
iter->first;
2046 dbDriver->getNodeData(nodeID,
data,
true,
false,
false,
false);
2047 if(!
data.imageCompressed().empty())
2049 data.uncompressDataConst(&image, 0);
2053 if(cameraProjDecimation>1)
2058 UASSERT(cameraModelsProj.find(nodeID) != cameraModelsProj.end());
2059 int modelsSize = cameraModelsProj.at(nodeID).size();
2060 for(
size_t i=0;
i<pointToPixel.size(); ++
i)
2062 int cameraIndex = pointToPixel[
i].first.second;
2063 if(nodeID == pointToPixel[
i].
first.first && cameraIndex>=0)
2065 pcl::PointXYZRGBNormal pt;
2066 float intensity = 0;
2067 if(!cloudToExport->empty())
2069 pt = cloudToExport->at(
i);
2071 else if(!cloudIToExport->empty())
2073 pt.x = cloudIToExport->at(
i).x;
2074 pt.y = cloudIToExport->at(
i).y;
2075 pt.z = cloudIToExport->at(
i).z;
2076 pt.normal_x = cloudIToExport->at(
i).normal_x;
2077 pt.normal_y = cloudIToExport->at(
i).normal_y;
2078 pt.normal_z = cloudIToExport->at(
i).normal_z;
2079 intensity = cloudIToExport->at(
i).intensity;
2084 int subImageWidth = image.cols / modelsSize;
2085 cv::Mat subImage = image(
cv::Range::all(), cv::Range(cameraIndex*subImageWidth, (cameraIndex+1)*subImageWidth));
2087 int x = pointToPixel[
i].second.x * (
float)subImage.cols;
2088 int y = pointToPixel[
i].second.y * (
float)subImage.rows;
2092 if(subImage.type()==CV_8UC3)
2094 cv::Vec3b bgr = subImage.at<cv::Vec3b>(
y,
x);
2101 UASSERT(subImage.type()==CV_8UC1);
2102 pt.r = pt.g = pt.b = subImage.at<
unsigned char>(pointToPixel[
i].second.y * subImage.rows, pointToPixel[
i].second.x * subImage.cols);
2107 int exportedId = nodeID;
2108 pointToCamId[
i] = exportedId;
2109 if(!pointToCamIntensity.empty())
2111 pointToCamIntensity[
i] = intensity;
2113 assembledCloudValidPoints->at(
i) = pt;
2118 printf(
"Processed %d/%d images\n", imagesDone++, (
int)robotPoses.size());
2122 printf(
"Node %d doesn't have image! (%d/%d)\n",
iter->first, imagesDone++, (
int)robotPoses.size());
2125 printf(
"Colored %d/%d points.\n", coloredPoints, (
int)assembledCloudValidPoints->size());
2127 pcl::IndicesPtr validIndices(
new std::vector<int>(pointToPixel.size()));
2129 for(
size_t i=0;
i<pointToPixel.size(); ++
i)
2131 if(pointToPixel[
i].
first.first <=0)
2133 if(camProjectionKeepAll)
2135 pcl::PointXYZRGBNormal pt;
2136 float intensity = 0;
2137 if(!cloudToExport->empty())
2139 pt = cloudToExport->at(
i);
2141 else if(!cloudIToExport->empty())
2143 pt.x = cloudIToExport->at(
i).x;
2144 pt.y = cloudIToExport->at(
i).y;
2145 pt.z = cloudIToExport->at(
i).z;
2146 pt.normal_x = cloudIToExport->at(
i).normal_x;
2147 pt.normal_y = cloudIToExport->at(
i).normal_y;
2148 pt.normal_z = cloudIToExport->at(
i).normal_z;
2149 intensity = cloudIToExport->at(
i).intensity;
2152 pointToCamId[
i] = 0;
2156 if(!pointToCamIntensity.empty())
2158 pointToCamIntensity[
i] = intensity;
2160 assembledCloudValidPoints->at(
i) = pt;
2161 validIndices->at(oi++) =
i;
2166 validIndices->at(oi++) =
i;
2169 if(oi != validIndices->size())
2171 validIndices->resize(oi);
2172 assembledCloudValidPoints =
util3d::extractIndices(assembledCloudValidPoints, validIndices,
false,
false);
2173 std::vector<int> pointToCamIdTmp(validIndices->size());
2174 std::vector<float> pointToCamIntensityTmp(pointToCamIntensity.empty()?0:validIndices->size());
2175 for(
size_t i=0;
i<validIndices->size(); ++
i)
2177 size_t index = validIndices->at(
i);
2178 UASSERT(index < pointToCamId.size());
2179 pointToCamIdTmp[
i] = pointToCamId[index];
2180 if(!pointToCamIntensity.empty())
2182 UASSERT(index < pointToCamIntensity.size());
2183 pointToCamIntensityTmp[
i] = pointToCamIntensity[index];
2186 pointToCamId = pointToCamIdTmp;
2187 pointToCamIntensity = pointToCamIntensityTmp;
2188 pointToCamIdTmp.clear();
2189 pointToCamIntensityTmp.clear();
2191 cloudToExport = assembledCloudValidPoints;
2192 cloudIToExport->clear();
2194 printf(
"Camera projection... done! (%fs)\n",
timer.ticks());
2203 printf(
"Option --save_in_db is set and both --cloud and --mesh are set, we will only save the mesh in the database.\n");
2207 printf(
"Saving cloud in db... (%d points)\n", !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
2208 if(!cloudToExport->empty())
2210 else if(!cloudIToExport->empty())
2212 printf(
"Saving cloud in db... done!\n");
2217 std::string ext = las?
"las":
"ply";
2218 std::string outputPath=outputDirectory+
"/"+baseName+
"_cloud."+ext;
2219 printf(
"Saving %s... (%d points)\n", outputPath.c_str(), !cloudToExport->empty()?(
int)cloudToExport->size():(
int)cloudIToExport->size());
2221 if(las || !pointToCamId.empty() || !pointToCamIntensity.empty())
2223 if(!cloudToExport->empty())
2225 if(!pointToCamIntensity.empty())
2234 else if(!cloudIToExport->empty())
2240 if(!pointToCamId.empty())
2242 if(!pointToCamIntensity.empty())
2244 printf(
"Option --cam_projection is enabled but rtabmap is not built "
2245 "with PDAL support, so camera IDs and lidar intensities won't be exported in the output cloud.\n");
2249 printf(
"Option --cam_projection is enabled but rtabmap is not built "
2250 "with PDAL support, so camera IDs won't be exported in the output cloud.\n");
2253 if(!cloudToExport->empty())
2254 pcl::io::savePLYFile(outputPath, *cloudToExport,
binary);
2255 else if(!cloudIToExport->empty())
2256 pcl::io::savePLYFile(outputPath, *cloudIToExport,
binary);
2258 printf(
"Saving %s... done!\n", outputPath.c_str());
2265 if(!cloudIToExport->empty())
2267 pcl::copyPointCloud(*cloudIToExport, *cloudToExport);
2268 cloudIToExport->clear();
2274 int optimizedDepth = 12;
2275 for(
int i=6;
i<12; ++
i)
2277 if(mapLength/
float(1<<
i) < poissonSize)
2285 optimizedDepth = poissonDepth;
2289 printf(
"Mesh reconstruction... depth=%d\n", optimizedDepth);
2290 pcl::PolygonMesh::Ptr mesh(
new pcl::PolygonMesh);
2291 pcl::Poisson<pcl::PointXYZRGBNormal> poisson;
2292 poisson.setDepth(optimizedDepth);
2293 poisson.setInputCloud(cloudToExport);
2294 poisson.reconstruct(*mesh);
2295 printf(
"Mesh reconstruction... done (%fs, %d polygons).\n",
timer.ticks(), (
int)mesh->polygons.size());
2297 if(mesh->polygons.size())
2299 printf(
"Mesh color transfer (max polygons=%d, color radius=%f, clean=%s)...\n",
2302 doClean?
"true":
"false");
2303 rtabmap::util3d::denseMeshPostProcessing<pcl::PointXYZRGBNormal>(
2312 printf(
"Mesh color transfer... done (%fs).\n",
timer.ticks());
2318 printf(
"Saving mesh in db...\n");
2319 std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > > polygons;
2324 printf(
"Saving mesh in db... done!\n");
2328 std::string outputPath=outputDirectory+
"/"+baseName+
"_mesh.ply";
2329 printf(
"Saving %s...\n", outputPath.c_str());
2331 pcl::io::savePLYFileBinary(outputPath, *mesh);
2333 pcl::io::savePLYFile(outputPath, *mesh);
2334 printf(
"Saving %s... done!\n", outputPath.c_str());
2340 std::map<int, rtabmap::Transform> robotPosesFiltered;
2343 printf(
"Filtering %ld images from texturing...\n", robotPoses.size());
2344 for(std::map<int, rtabmap::Transform>::iterator
iter=robotPoses.lower_bound(1);
iter!=robotPoses.end(); ++
iter)
2347 dbDriver->getNodeData(
iter->first,
data,
true,
false,
false,
false);
2349 data.uncompressDataConst(&
img, 0);
2352 cv::Mat imgLaplacian;
2353 cv::Laplacian(
img, imgLaplacian, CV_16S);
2355 cv::meanStdDev(imgLaplacian,
m,
s);
2356 double stddev_pxl =
s.at<
double>(0);
2357 double var = stddev_pxl*stddev_pxl;
2358 if(var < (
double)laplacianThr)
2360 printf(
"Camera's image %d is detected as blurry (var=%f < thr=%d), camera is ignored for texturing.\n",
iter->first, var, laplacianThr);
2364 robotPosesFiltered.insert(*
iter);
2368 printf(
"Filtered %ld/%ld images from texturing", robotPosesFiltered.size(), robotPoses.size());
2372 robotPosesFiltered = robotPoses;
2375 printf(
"Texturing %d polygons... robotPoses=%d, cameraModels=%d, cameraDepths=%d\n", (
int)mesh->polygons.size(), (
int)robotPosesFiltered.size(), (
int)cameraModels.size(), (
int)cameraDepths.size());
2376 std::vector<std::map<int, pcl::PointXY> > vertexToPixels;
2389 distanceToCamPolicy);
2390 printf(
"Texturing... done (%fs).\n",
timer.ticks());
2393 if(doClean && textureMesh->tex_coordinates.size())
2395 printf(
"Cleanup mesh...\n");
2397 printf(
"Cleanup mesh... done (%fs).\n",
timer.ticks());
2400 if(textureMesh->tex_materials.size())
2404 printf(
"Merging %d texture(s) to single one (multiband enabled)...\n", (
int)textureMesh->tex_materials.size());
2408 printf(
"Merging %d texture(s)... (%d max textures)\n", (
int)textureMesh->tex_materials.size(), textureCount);
2410 std::map<int, std::map<int, cv::Vec4d> > gains;
2411 std::map<int, std::map<int, cv::Mat> > blendingGains;
2412 std::pair<float, float> contrastValues(0,0);
2415 std::map<int, cv::Mat>(),
2416 std::map<
int, std::vector<rtabmap::CameraModel> >(),
2420 multiband?1:textureCount,
2422 gainValue>0.0f, gainValue, doGainCompensationRGB,
2424 lowBrightnessGain, highBrightnessGain,
2431 printf(
"Merging to %d texture(s)... done (%fs).\n", (
int)textureMesh->tex_materials.size(),
timer.ticks());
2435 printf(
"Saving texture mesh in db...\n");
2439 textureMesh->tex_coordinates,
2441 printf(
"Saving texture mesh in db... done!\n");
2449 std::string outputPath=outputDirectory+
"/"+baseName+
"_mesh_multiband.obj";
2450 printf(
"MultiBand texturing (size=%d, downscale=%d, unwrap method=%s, fill holes=%s, padding=%d, best score thr=%f, angle thr=%f, force visible=%s)... \"%s\"\n",
2453 multibandUnwrap==1?
"ABF":multibandUnwrap==2?
"LSCM":
"Basic",
2454 multibandFillHoles?
"true":
"false",
2456 multibandBestScoreThr,
2457 multibandAngleHardthr,
2458 multibandForceVisible?
"false":
"true",
2459 outputPath.c_str());
2462 textureMesh->tex_polygons[0],
2465 std::map<int, cv::Mat >(),
2466 std::map<
int, std::vector<CameraModel> >(),
2476 doGainCompensationRGB,
2480 multibandBestScoreThr,
2481 multibandAngleHardthr,
2482 multibandForceVisible))
2484 printf(
"MultiBand texturing...done (%fs).\n",
timer.ticks());
2488 printf(
"MultiBand texturing...failed! (%fs)\n",
timer.ticks());
2494 bool success =
false;
2496 for(
size_t i=0;
i<textureMesh->tex_materials.size(); ++
i)
2500 textureMesh->tex_materials[
i].tex_file.clear();
2503 if(
v.compare(
"texture") == 0){
2504 textureMesh->tex_materials[
i].tex_file.append(baseName+
"_mesh");
2507 textureMesh->tex_materials[
i].tex_file.append(
v);
2510 textureMesh->tex_materials[
i].tex_file +=
".jpg";
2511 printf(
"Saving texture to %s.\n", textureMesh->tex_materials[
i].tex_file.c_str());
2512 UASSERT(textures.cols % textures.rows == 0);
2513 success = cv::imwrite(outputDirectory+
"/"+textureMesh->tex_materials[
i].tex_file, cv::Mat(textures,
cv::Range::all(), cv::Range(textures.rows*
i, textures.rows*(
i+1))));
2516 UERROR(
"Failed saving %s!", textureMesh->tex_materials[
i].tex_file.c_str());
2520 printf(
"Saved %s.\n", textureMesh->tex_materials[
i].tex_file.c_str());
2525 std::string outputPath=outputDirectory+
"/"+baseName+
"_mesh.obj";
2526 printf(
"Saving obj (%d vertices) to %s.\n", (
int)textureMesh->cloud.data.size()/textureMesh->cloud.point_step, outputPath.c_str());
2527 #if PCL_VERSION_COMPARE(>=, 1, 13, 0)
2528 textureMesh->tex_coord_indices = std::vector<std::vector<pcl::Vertices>>();
2529 auto nr_meshes =
static_cast<unsigned>(textureMesh->tex_polygons.size());
2531 for (
unsigned m = 0;
m < nr_meshes;
m++) {
2532 std::vector<pcl::Vertices> ci = textureMesh->tex_polygons[
m];
2533 for(std::size_t
i = 0;
i < ci.size();
i++) {
2534 for (std::size_t
j = 0;
j < ci[
i].vertices.size();
j++) {
2535 ci[
i].vertices[
j] = ci[
i].vertices.size() * (
i + f_idx) +
j;
2538 textureMesh->tex_coord_indices.push_back(ci);
2539 f_idx +=
static_cast<unsigned>(textureMesh->tex_polygons[
m].size());
2542 success = pcl::io::saveOBJFile(outputPath, *textureMesh) == 0;
2546 printf(
"Saved obj to %s!\n", outputPath.c_str());
2550 UERROR(
"Failed saving obj to %s!", outputPath.c_str());
2562 printf(
"Export failed! The cloud is empty.\n");