39 #include <pcl/io/pcd_io.h>
40 #include <pcl/io/ply_io.h>
41 #include <pcl/common/transforms.h>
42 #include <pcl/common/common.h>
43 #include <opencv2/imgproc/imgproc.hpp>
44 #include <opencv2/imgproc/types_c.h>
52 cv::Mat
bgrFromCloud(
const pcl::PointCloud<pcl::PointXYZRGBA> & cloud,
bool bgrOrder)
54 cv::Mat frameBGR = cv::Mat(cloud.height,cloud.width,CV_8UC3);
56 for(
unsigned int h = 0;
h < cloud.height;
h++)
58 for(
unsigned int w = 0;
w < cloud.width;
w++)
62 frameBGR.at<cv::Vec3b>(
h,
w)[0] = cloud.at(
h*cloud.width +
w).b;
63 frameBGR.at<cv::Vec3b>(
h,
w)[1] = cloud.at(
h*cloud.width +
w).g;
64 frameBGR.at<cv::Vec3b>(
h,
w)[2] = cloud.at(
h*cloud.width +
w).r;
68 frameBGR.at<cv::Vec3b>(
h,
w)[0] = cloud.at(
h*cloud.width +
w).r;
69 frameBGR.at<cv::Vec3b>(
h,
w)[1] = cloud.at(
h*cloud.width +
w).g;
70 frameBGR.at<cv::Vec3b>(
h,
w)[2] = cloud.at(
h*cloud.width +
w).b;
79 const pcl::PointCloud<pcl::PointXYZRGBA> & cloud,
84 cv::Mat frameDepth = cv::Mat(cloud.height,cloud.width,depth16U?CV_16UC1:CV_32FC1);
87 for(
unsigned int h = 0;
h < cloud.height;
h++)
89 for(
unsigned int w = 0;
w < cloud.width;
w++)
91 float depth = cloud.at(
h*cloud.width +
w).z;
95 unsigned short depthMM = 0;
96 if(depth <= (
float)USHRT_MAX)
98 depthMM = (
unsigned short)depth;
100 frameDepth.at<
unsigned short>(
h,
w) = depthMM;
104 frameDepth.at<
float>(
h,
w) = depth;
111 w != cloud.width/2 &&
114 fx = cloud.at(
h*cloud.width +
w).x / ((
float(
w) -
float(cloud.width)/2.0f) * depth);
123 h != cloud.height/2 &&
126 fy = cloud.at(
h*cloud.width +
w).y / ((
float(
h) -
float(cloud.height)/2.0f) * depth);
140 cv::Mat & frameDepth,
146 frameDepth = cv::Mat(cloud.height,cloud.width,depth16U?CV_16UC1:CV_32FC1);
147 frameBGR = cv::Mat(cloud.height,cloud.width,CV_8UC3);
151 for(
unsigned int h = 0;
h < cloud.height;
h++)
153 for(
unsigned int w = 0;
w < cloud.width;
w++)
158 frameBGR.at<cv::Vec3b>(
h,
w)[0] = cloud.at(
h*cloud.width +
w).b;
159 frameBGR.at<cv::Vec3b>(
h,
w)[1] = cloud.at(
h*cloud.width +
w).g;
160 frameBGR.at<cv::Vec3b>(
h,
w)[2] = cloud.at(
h*cloud.width +
w).r;
164 frameBGR.at<cv::Vec3b>(
h,
w)[0] = cloud.at(
h*cloud.width +
w).r;
165 frameBGR.at<cv::Vec3b>(
h,
w)[1] = cloud.at(
h*cloud.width +
w).g;
166 frameBGR.at<cv::Vec3b>(
h,
w)[2] = cloud.at(
h*cloud.width +
w).b;
170 float depth = cloud.at(
h*cloud.width +
w).z;
174 unsigned short depthMM = 0;
175 if(depth <= (
float)USHRT_MAX)
177 depthMM = (
unsigned short)depth;
179 frameDepth.at<
unsigned short>(
h,
w) = depthMM;
183 frameDepth.at<
float>(
h,
w) = depth;
190 w != cloud.width/2 &&
193 fx = 1.0f/(cloud.at(
h*cloud.width +
w).x / ((
float(
w) -
float(cloud.width)/2.0f) * depth));
202 h != cloud.height/2 &&
205 fy = 1.0f/(cloud.at(
h*cloud.width +
w).y / ((
float(
h) -
float(cloud.height)/2.0f) * depth));
216 const cv::Mat & depthImage,
221 float depthErrorRatio)
223 UASSERT(depthImage.type() == CV_16UC1 || depthImage.type() == CV_32FC1);
231 cx =
cx > 0.0f ?
cx :
float(depthImage.cols/2) - 0.5f;
232 cy =
cy > 0.0f ?
cy :
float(depthImage.rows/2) - 0.5f;
235 pt.x = (
x -
cx) * depth /
fx;
236 pt.y = (
y -
cy) * depth /
fy;
241 pt.x = pt.y = pt.z = std::numeric_limits<float>::quiet_NaN();
247 const cv::Size & imageSize,
255 cx =
cx > 0.0f ?
cx :
float(imageSize.width/2) - 0.5f;
256 cy =
cy > 0.0f ?
cy :
float(imageSize.height/2) - 0.5f;
259 ray[0] = (
x -
cx) /
fx;
260 ray[1] = (
y -
cy) /
fy;
267 const cv::Mat & imageDepth,
273 std::vector<int> * validIndices)
280 const cv::Mat & imageDepthIn,
285 std::vector<int> * validIndices)
287 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
292 float rgbToDepthFactorX = 1.0f;
293 float rgbToDepthFactorY = 1.0f;
296 UASSERT(!imageDepthIn.empty() && (imageDepthIn.type() == CV_16UC1 || imageDepthIn.type() == CV_32FC1));
298 cv::Mat imageDepth = imageDepthIn;
299 if(
model.imageHeight()>0 &&
model.imageWidth()>0)
301 UASSERT(
model.imageHeight() % imageDepthIn.rows == 0 &&
model.imageWidth() % imageDepthIn.cols == 0);
305 UDEBUG(
"Decimation from model (%d)", decimation);
306 if(
model.imageHeight() % decimation != 0)
308 UERROR(
"Decimation is not valid for current image size (model.imageHeight()=%d decimation=%d). The cloud is not created.",
model.imageHeight(), decimation);
311 if(
model.imageWidth() % decimation != 0)
313 UERROR(
"Decimation is not valid for current image size (model.imageWidth()=%d decimation=%d). The cloud is not created.",
model.imageWidth(), decimation);
318 decimation = -1*decimation;
320 int targetSize =
model.imageHeight() / decimation;
321 if(targetSize > imageDepthIn.rows)
323 UDEBUG(
"Depth interpolation factor=%d", targetSize/imageDepthIn.rows);
327 else if(targetSize == imageDepthIn.rows)
333 UASSERT(imageDepthIn.rows % targetSize == 0);
334 decimation = imageDepthIn.rows / targetSize;
339 if(imageDepthIn.rows % decimation != 0)
341 UERROR(
"Decimation is not valid for current image size (imageDepth.rows=%d decimation=%d). The cloud is not created.", imageDepthIn.rows, decimation);
344 if(imageDepthIn.cols % decimation != 0)
346 UERROR(
"Decimation is not valid for current image size (imageDepth.cols=%d decimation=%d). The cloud is not created.", imageDepthIn.cols, decimation);
351 rgbToDepthFactorX = 1.0f/
float((
model.imageWidth() / imageDepth.cols));
352 rgbToDepthFactorY = 1.0f/
float((
model.imageHeight() / imageDepth.rows));
356 decimation =
abs(decimation);
357 UASSERT_MSG(imageDepth.rows % decimation == 0,
uFormat(
"rows=%d decimation=%d", imageDepth.rows, decimation).c_str());
358 UASSERT_MSG(imageDepth.cols % decimation == 0,
uFormat(
"cols=%d decimation=%d", imageDepth.cols, decimation).c_str());
362 cloud->height = imageDepth.rows/decimation;
363 cloud->width = imageDepth.cols/decimation;
364 cloud->is_dense =
false;
365 cloud->resize(cloud->height * cloud->width);
368 validIndices->resize(cloud->size());
371 float depthFx =
model.fx() * rgbToDepthFactorX;
372 float depthFy =
model.fy() * rgbToDepthFactorY;
373 float depthCx =
model.cx() * rgbToDepthFactorX;
374 float depthCy =
model.cy() * rgbToDepthFactorY;
376 UDEBUG(
"depth=%dx%d fx=%f fy=%f cx=%f cy=%f (depth factors=%f %f) decimation=%d",
377 imageDepth.cols, imageDepth.rows,
384 for(
int h = 0;
h < imageDepth.rows &&
h/decimation < (
int)cloud->height;
h+=decimation)
386 for(
int w = 0;
w < imageDepth.cols &&
w/decimation < (
int)cloud->width;
w+=decimation)
388 pcl::PointXYZ & pt = cloud->at((
h/decimation)*cloud->width + (
w/decimation));
390 pcl::PointXYZ ptXYZ =
projectDepthTo3D(imageDepth,
w,
h, depthCx, depthCy, depthFx, depthFy,
false);
391 if(pcl::isFinite(ptXYZ) && ptXYZ.z>=minDepth && (maxDepth<=0.0f || ptXYZ.z <= maxDepth))
398 validIndices->at(oi++) = (
h/decimation)*cloud->width + (
w/decimation);
403 pt.x = pt.y = pt.z = std::numeric_limits<float>::quiet_NaN();
410 validIndices->resize(oi);
417 const cv::Mat & imageRgb,
418 const cv::Mat & imageDepth,
424 std::vector<int> * validIndices)
431 const cv::Mat & imageRgb,
432 const cv::Mat & imageDepthIn,
437 std::vector<int> * validIndices)
439 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
447 (
model.imageHeight() == imageRgb.rows &&
model.imageWidth() == imageRgb.cols),
448 uFormat(
"model=%dx%d rgb=%dx%d",
model.imageWidth(),
model.imageHeight(), imageRgb.cols, imageRgb.rows).c_str());
451 UASSERT(!imageDepthIn.empty() && (imageDepthIn.type() == CV_16UC1 || imageDepthIn.type() == CV_32FC1));
454 if(imageRgb.rows % decimation != 0 || imageRgb.cols % decimation != 0)
456 int oldDecimation = decimation;
457 while(decimation <= -1)
459 if(imageRgb.rows % decimation == 0 && imageRgb.cols % decimation == 0)
466 if(imageRgb.rows % oldDecimation != 0 || imageRgb.cols % oldDecimation != 0)
468 UWARN(
"Decimation (%d) is not valid for current image size (rgb=%dx%d). Highest compatible decimation used=%d.", oldDecimation, imageRgb.cols, imageRgb.rows, decimation);
474 if(imageDepthIn.rows % decimation != 0 || imageDepthIn.cols % decimation != 0)
476 int oldDecimation = decimation;
477 while(decimation >= 1)
479 if(imageDepthIn.rows % decimation == 0 && imageDepthIn.cols % decimation == 0)
486 if(imageDepthIn.rows % oldDecimation != 0 || imageDepthIn.cols % oldDecimation != 0)
488 UWARN(
"Decimation (%d) is not valid for current image size (depth=%dx%d). Highest compatible decimation used=%d.", oldDecimation, imageDepthIn.cols, imageDepthIn.rows, decimation);
493 cv::Mat imageDepth = imageDepthIn;
496 UDEBUG(
"Decimation from RGB image (%d)", decimation);
498 decimation = -1*decimation;
500 int targetSize = imageRgb.rows / decimation;
501 if(targetSize > imageDepthIn.rows)
503 UDEBUG(
"Depth interpolation factor=%d", targetSize/imageDepthIn.rows);
507 else if(targetSize == imageDepthIn.rows)
513 UASSERT(imageDepthIn.rows % targetSize == 0);
514 decimation = imageDepthIn.rows / targetSize;
519 if(imageRgb.channels() == 3)
523 else if(imageRgb.channels() == 1)
533 cloud->height = imageDepth.rows/decimation;
534 cloud->width = imageDepth.cols/decimation;
535 cloud->is_dense =
false;
536 cloud->resize(cloud->height * cloud->width);
539 validIndices->resize(cloud->size());
542 float rgbToDepthFactorX =
float(imageRgb.cols) /
float(imageDepth.cols);
543 float rgbToDepthFactorY =
float(imageRgb.rows) /
float(imageDepth.rows);
544 float depthFx =
model.fx() / rgbToDepthFactorX;
545 float depthFy =
model.fy() / rgbToDepthFactorY;
546 float depthCx =
model.cx() / rgbToDepthFactorX;
547 float depthCy =
model.cy() / rgbToDepthFactorY;
549 UDEBUG(
"rgb=%dx%d depth=%dx%d fx=%f fy=%f cx=%f cy=%f (depth factors=%f %f) decimation=%d",
550 imageRgb.cols, imageRgb.rows,
551 imageDepth.cols, imageDepth.rows,
558 for(
int h = 0;
h < imageDepth.rows &&
h/decimation < (
int)cloud->height;
h+=decimation)
560 for(
int w = 0;
w < imageDepth.cols &&
w/decimation < (
int)cloud->width;
w+=decimation)
562 pcl::PointXYZRGB & pt = cloud->at((
h/decimation)*cloud->width + (
w/decimation));
564 int x =
int(
w*rgbToDepthFactorX);
565 int y =
int(
h*rgbToDepthFactorY);
566 UASSERT(
x >=0 && x<imageRgb.cols && y >=0 &&
y<imageRgb.rows);
569 const unsigned char * bgr = imageRgb.ptr<
unsigned char>(
y,
x);
576 unsigned char v = imageRgb.at<
unsigned char>(
y,
x);
582 pcl::PointXYZ ptXYZ =
projectDepthTo3D(imageDepth,
w,
h, depthCx, depthCy, depthFx, depthFy,
false);
583 if (pcl::isFinite(ptXYZ) && ptXYZ.z >= minDepth && (maxDepth <= 0.0f || ptXYZ.z <= maxDepth))
590 validIndices->at(oi) = (
h / decimation)*cloud->width + (
w / decimation);
596 pt.x = pt.y = pt.z = std::numeric_limits<float>::quiet_NaN();
602 validIndices->resize(oi);
606 UWARN(
"Cloud with only NaN values created!");
613 const cv::Mat & imageDisparity,
618 std::vector<int> * validIndices)
620 UASSERT(imageDisparity.type() == CV_32FC1 || imageDisparity.type()==CV_16SC1);
623 if(imageDisparity.rows % decimation != 0 || imageDisparity.cols % decimation != 0)
625 int oldDecimation = decimation;
626 while(decimation >= 1)
628 if(imageDisparity.rows % decimation == 0 && imageDisparity.cols % decimation == 0)
635 if(imageDisparity.rows % oldDecimation != 0 || imageDisparity.cols % oldDecimation != 0)
637 UWARN(
"Decimation (%d) is not valid for current image size (depth=%dx%d). Highest compatible decimation used=%d.", oldDecimation, imageDisparity.cols, imageDisparity.rows, decimation);
641 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
644 cloud->height = imageDisparity.rows/decimation;
645 cloud->width = imageDisparity.cols/decimation;
646 cloud->is_dense =
false;
647 cloud->resize(cloud->height * cloud->width);
650 validIndices->resize(cloud->size());
654 if(imageDisparity.type()==CV_16SC1)
656 for(
int h = 0;
h < imageDisparity.rows &&
h/decimation < (
int)cloud->height;
h+=decimation)
658 for(
int w = 0;
w < imageDisparity.cols &&
w/decimation < (
int)cloud->width;
w+=decimation)
660 float disp =
float(imageDisparity.at<
short>(
h,
w))/16.0f;
662 if(pt.z >= minDepth && (maxDepth <= 0.0f || pt.z <= maxDepth))
664 cloud->at((
h/decimation)*cloud->width + (
w/decimation)) = pcl::PointXYZ(pt.x, pt.y, pt.z);
667 validIndices->at(oi++) = (
h/decimation)*cloud->width + (
w/decimation);
672 cloud->at((
h/decimation)*cloud->width + (
w/decimation)) = pcl::PointXYZ(
673 std::numeric_limits<float>::quiet_NaN(),
674 std::numeric_limits<float>::quiet_NaN(),
675 std::numeric_limits<float>::quiet_NaN());
682 for(
int h = 0;
h < imageDisparity.rows &&
h/decimation < (
int)cloud->height;
h+=decimation)
684 for(
int w = 0;
w < imageDisparity.cols &&
w/decimation < (
int)cloud->width;
w+=decimation)
686 float disp = imageDisparity.at<
float>(
h,
w);
688 if(pt.z > minDepth && (maxDepth <= 0.0f || pt.z <= maxDepth))
690 cloud->at((
h/decimation)*cloud->width + (
w/decimation)) = pcl::PointXYZ(pt.x, pt.y, pt.z);
693 validIndices->at(oi++) = (
h/decimation)*cloud->width + (
w/decimation);
698 cloud->at((
h/decimation)*cloud->width + (
w/decimation)) = pcl::PointXYZ(
699 std::numeric_limits<float>::quiet_NaN(),
700 std::numeric_limits<float>::quiet_NaN(),
701 std::numeric_limits<float>::quiet_NaN());
708 validIndices->resize(oi);
714 const cv::Mat & imageRgb,
715 const cv::Mat & imageDisparity,
720 std::vector<int> * validIndices)
722 UASSERT(!imageRgb.empty() && !imageDisparity.empty());
723 UASSERT(imageRgb.rows == imageDisparity.rows &&
724 imageRgb.cols == imageDisparity.cols &&
725 (imageDisparity.type() == CV_32FC1 || imageDisparity.type()==CV_16SC1));
726 UASSERT(imageRgb.channels() == 3 || imageRgb.channels() == 1);
729 if(imageDisparity.rows % decimation != 0 || imageDisparity.cols % decimation != 0)
731 int oldDecimation = decimation;
732 while(decimation >= 1)
734 if(imageDisparity.rows % decimation == 0 && imageDisparity.cols % decimation == 0)
741 if(imageDisparity.rows % oldDecimation != 0 || imageDisparity.cols % oldDecimation != 0)
743 UWARN(
"Decimation (%d) is not valid for current image size (depth=%dx%d). Highest compatible decimation used=%d.", oldDecimation, imageDisparity.cols, imageDisparity.rows, decimation);
747 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
750 if(imageRgb.channels() == 3)
760 cloud->height = imageRgb.rows/decimation;
761 cloud->width = imageRgb.cols/decimation;
762 cloud->is_dense =
false;
763 cloud->resize(cloud->height * cloud->width);
766 validIndices->resize(cloud->size());
770 for(
int h = 0;
h < imageRgb.rows &&
h/decimation < (
int)cloud->height;
h+=decimation)
772 for(
int w = 0;
w < imageRgb.cols &&
w/decimation < (
int)cloud->width;
w+=decimation)
774 pcl::PointXYZRGB & pt = cloud->at((
h/decimation)*cloud->width + (
w/decimation));
777 pt.b = imageRgb.at<cv::Vec3b>(
h,
w)[0];
778 pt.g = imageRgb.at<cv::Vec3b>(
h,
w)[1];
779 pt.r = imageRgb.at<cv::Vec3b>(
h,
w)[2];
783 unsigned char v = imageRgb.at<
unsigned char>(
h,
w);
789 float disp = imageDisparity.type()==CV_16SC1?
float(imageDisparity.at<
short>(
h,
w))/16.0f:imageDisparity.at<
float>(
h,
w);
791 if(
util3d::isFinite(ptXYZ) && ptXYZ.z >= minDepth && (maxDepth<=0.0f || ptXYZ.z <= maxDepth))
798 validIndices->at(oi++) = (
h/decimation)*cloud->width + (
w/decimation);
803 pt.x = pt.y = pt.z = std::numeric_limits<float>::quiet_NaN();
809 validIndices->resize(oi);
815 const cv::Mat & imageLeft,
816 const cv::Mat & imageRight,
821 std::vector<int> * validIndices,
824 UASSERT(!imageLeft.empty() && !imageRight.empty());
825 UASSERT(imageRight.type() == CV_8UC1 || imageRight.type() == CV_8UC3);
826 UASSERT(imageLeft.channels() == 3 || imageLeft.channels() == 1);
827 UASSERT(imageLeft.rows == imageRight.rows &&
828 imageLeft.cols == imageRight.cols);
831 cv::Mat leftColor = imageLeft;
832 cv::Mat rightColor = imageRight;
835 if(leftColor.channels() == 3)
837 cv::cvtColor(leftColor, leftMono, CV_BGR2GRAY);
841 leftMono = leftColor;
845 if(rightColor.channels() == 3)
847 cv::cvtColor(rightColor, rightMono, CV_BGR2GRAY);
851 rightMono = rightColor;
869 std::vector<pcl::IndicesPtr> * validIndices,
871 const std::vector<float> & roiRatios)
878 std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> clouds;
887 clouds.push_back(pcl::PointCloud<pcl::PointXYZ>::Ptr(
new pcl::PointCloud<pcl::PointXYZ>));
890 validIndices->push_back(pcl::IndicesPtr(
new std::vector<int>()));
894 cv::Mat depth = cv::Mat(sensorData.
depthRaw(), cv::Rect(subImageWidth*
i, 0, subImageWidth, sensorData.
depthRaw().rows));
896 if( roiRatios.size() == 4 &&
897 (roiRatios[0] > 0.0f ||
898 roiRatios[1] > 0.0f ||
899 roiRatios[2] > 0.0f ||
900 roiRatios[3] > 0.0f))
908 if( roiDepth.width%decimation==0 &&
909 roiDepth.height%decimation==0 &&
910 (roiRgb.width != 0 ||
911 (roiRgb.width%decimation==0 &&
912 roiRgb.height%decimation==0)))
914 depth = cv::Mat(depth, roiDepth);
915 if(
model.imageWidth() != 0 &&
model.imageHeight() != 0)
926 UERROR(
"Cannot apply ROI ratios [%f,%f,%f,%f] because resulting "
927 "dimension (depth=%dx%d rgb=%dx%d) cannot be divided exactly "
928 "by decimation parameter (%d). Ignoring ROI ratios...",
947 validIndices?validIndices->back().get():0);
951 if(!
model.localTransform().isNull() && !
model.localTransform().isIdentity())
960 UERROR(
"Camera model %d is invalid",
i);
970 if(sensorData.
imageRaw().channels() == 3)
972 cv::cvtColor(sensorData.
imageRaw(), leftMono, CV_BGR2GRAY);
980 if(sensorData.
rightRaw().channels() == 3)
982 cv::cvtColor(sensorData.
rightRaw(), rightMono, CV_BGR2GRAY);
994 clouds.push_back(pcl::PointCloud<pcl::PointXYZ>::Ptr(
new pcl::PointCloud<pcl::PointXYZ>));
997 validIndices->push_back(pcl::IndicesPtr(
new std::vector<int>()));
1001 cv::Mat left(leftMono, cv::Rect(subImageWidth*
i, 0, subImageWidth, leftMono.rows));
1002 cv::Mat right(rightMono, cv::Rect(subImageWidth*
i, 0, subImageWidth, rightMono.rows));
1004 if( roiRatios.size() == 4 &&
1005 ((roiRatios[0] > 0.0f && roiRatios[0] <= 1.0f) ||
1006 (roiRatios[1] > 0.0f && roiRatios[1] <= 1.0f) ||
1007 (roiRatios[2] > 0.0f && roiRatios[2] <= 1.0f) ||
1008 (roiRatios[3] > 0.0f && roiRatios[3] <= 1.0f)))
1011 if( roi.width%decimation==0 &&
1012 roi.height%decimation==0)
1014 left = cv::Mat(left, roi);
1015 right = cv::Mat(right, roi);
1020 UERROR(
"Cannot apply ROI ratios [%f,%f,%f,%f] because resulting "
1021 "dimension (left=%dx%d) cannot be divided exactly "
1022 "by decimation parameter (%d). Ignoring ROI ratios...",
1039 validIndices?validIndices->back().get():0);
1043 if(!
model.localTransform().isNull() && !
model.localTransform().isIdentity())
1047 clouds.back() = tmp;
1052 UERROR(
"Stereo camera model %d is invalid",
i);
1065 std::vector<int> * validIndices,
1067 const std::vector<float> & roiRatios)
1069 std::vector<pcl::IndicesPtr> validIndicesV;
1075 validIndices?&validIndicesV:0,
1081 UASSERT(validIndicesV.size() == clouds.size());
1084 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
1086 if(clouds.size() == 1)
1091 *validIndices = *validIndicesV[0];
1096 for(
size_t i=0;
i<clouds.size(); ++
i)
1103 validIndices->resize(cloud->size());
1104 for(
size_t i=0;
i<cloud->size(); ++
i)
1106 validIndices->at(
i) =
i;
1118 std::vector<pcl::IndicesPtr> * validIndices,
1120 const std::vector<float> & roiRatios)
1127 std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> clouds;
1142 clouds.push_back(pcl::PointCloud<pcl::PointXYZRGB>::Ptr(
new pcl::PointCloud<pcl::PointXYZRGB>));
1145 validIndices->push_back(pcl::IndicesPtr(
new std::vector<int>()));
1149 cv::Mat rgb(sensorData.
imageRaw(), cv::Rect(subRGBWidth*
i, 0, subRGBWidth, sensorData.
imageRaw().rows));
1150 cv::Mat depth(sensorData.
depthRaw(), cv::Rect(subDepthWidth*
i, 0, subDepthWidth, sensorData.
depthRaw().rows));
1152 if( roiRatios.size() == 4 &&
1153 ((roiRatios[0] > 0.0f && roiRatios[0] <= 1.0f) ||
1154 (roiRatios[1] > 0.0f && roiRatios[1] <= 1.0f) ||
1155 (roiRatios[2] > 0.0f && roiRatios[2] <= 1.0f) ||
1156 (roiRatios[3] > 0.0f && roiRatios[3] <= 1.0f)))
1160 if( roiDepth.width%decimation==0 &&
1161 roiDepth.height%decimation==0 &&
1162 roiRgb.width%decimation==0 &&
1163 roiRgb.height%decimation==0)
1165 depth = cv::Mat(depth, roiDepth);
1166 rgb = cv::Mat(rgb, roiRgb);
1171 UERROR(
"Cannot apply ROI ratios [%f,%f,%f,%f] because resulting "
1172 "dimension (depth=%dx%d rgb=%dx%d) cannot be divided exactly "
1173 "by decimation parameter (%d). Ignoring ROI ratios...",
1193 validIndices?validIndices->back().get():0);
1197 if(!
model.localTransform().isNull() && !
model.localTransform().isIdentity())
1201 clouds.back() = tmp;
1206 UERROR(
"Camera model %d is invalid",
i);
1220 clouds.push_back(pcl::PointCloud<pcl::PointXYZRGB>::Ptr(
new pcl::PointCloud<pcl::PointXYZRGB>));
1223 validIndices->push_back(pcl::IndicesPtr(
new std::vector<int>()));
1227 cv::Mat left(sensorData.
imageRaw(), cv::Rect(subImageWidth*
i, 0, subImageWidth, sensorData.
imageRaw().rows));
1228 cv::Mat right(sensorData.
rightRaw(), cv::Rect(subImageWidth*
i, 0, subImageWidth, sensorData.
rightRaw().rows));
1230 if( roiRatios.size() == 4 &&
1231 ((roiRatios[0] > 0.0f && roiRatios[0] <= 1.0f) ||
1232 (roiRatios[1] > 0.0f && roiRatios[1] <= 1.0f) ||
1233 (roiRatios[2] > 0.0f && roiRatios[2] <= 1.0f) ||
1234 (roiRatios[3] > 0.0f && roiRatios[3] <= 1.0f)))
1237 if( roi.width%decimation==0 &&
1238 roi.height%decimation==0)
1240 left = cv::Mat(left, roi);
1241 right = cv::Mat(right, roi);
1246 UERROR(
"Cannot apply ROI ratios [%f,%f,%f,%f] because resulting "
1247 "dimension (left=%dx%d) cannot be divided exactly "
1248 "by decimation parameter (%d). Ignoring ROI ratios...",
1266 validIndices?validIndices->back().get():0,
1271 if(!
model.localTransform().isNull() && !
model.localTransform().isIdentity())
1275 clouds.back() = tmp;
1280 UERROR(
"Stereo camera model %d is invalid",
i);
1293 std::vector<int> * validIndices,
1295 const std::vector<float> & roiRatios)
1297 std::vector<pcl::IndicesPtr> validIndicesV;
1303 validIndices?&validIndicesV:0,
1309 UASSERT(validIndicesV.size() == clouds.size());
1312 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
1314 if(clouds.size() == 1)
1319 *validIndices = *validIndicesV[0];
1324 for(
size_t i=0;
i<clouds.size(); ++
i)
1331 validIndices->resize(cloud->size());
1332 for(
size_t i=0;
i<cloud->size(); ++
i)
1334 validIndices->at(
i) =
i;
1342 const cv::Mat & depthImage,
1351 UASSERT(depthImage.type() == CV_16UC1 || depthImage.type() == CV_32FC1);
1354 pcl::PointCloud<pcl::PointXYZ> scan;
1355 int middle = depthImage.rows/2;
1358 scan.resize(depthImage.cols);
1360 for(
int i=depthImage.cols-1;
i>=0; --
i)
1363 if(pcl::isFinite(pt) && pt.z >= minDepth && (maxDepth == 0 || pt.z < maxDepth))
1378 const cv::Mat & depthImages,
1379 const std::vector<CameraModel> & cameraModels,
1383 pcl::PointCloud<pcl::PointXYZ> scan;
1384 UASSERT(
int((depthImages.cols/cameraModels.size())*cameraModels.size()) == depthImages.cols);
1385 int subImageWidth = depthImages.cols/cameraModels.size();
1386 for(
int i=(
int)cameraModels.size()-1;
i>=0; --
i)
1388 if(cameraModels[
i].isValidForProjection())
1390 cv::Mat depth = cv::Mat(depthImages, cv::Rect(subImageWidth*
i, 0, subImageWidth, depthImages.rows));
1394 cameraModels[
i].
fx(),
1395 cameraModels[
i].
fy(),
1396 cameraModels[
i].
cx(),
1397 cameraModels[
i].
cy(),
1400 cameraModels[
i].localTransform());
1404 UERROR(
"Camera model %d is invalid",
i);
1422 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC3);
1423 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1426 if(!filterNaNs || pcl::isFinite(cloud.at(index)))
1428 float * ptr = laserScan.ptr<
float>(0, oi++);
1438 ptr[0] = cloud.at(index).x;
1439 ptr[1] = cloud.at(index).y;
1440 ptr[2] = cloud.at(index).z;
1447 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC3);
1448 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1450 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
1452 float * ptr = laserScan.ptr<
float>(0, oi++);
1462 ptr[0] = cloud.at(
i).x;
1463 ptr[1] = cloud.at(
i).y;
1464 ptr[2] = cloud.at(
i).z;
1487 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC(6));
1488 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1491 if(!filterNaNs || (pcl::isFinite(cloud.at(index)) &&
1496 float * ptr = laserScan.ptr<
float>(0, oi++);
1503 ptr[3] = pt.normal_x;
1504 ptr[4] = pt.normal_y;
1505 ptr[5] = pt.normal_z;
1509 ptr[0] = cloud.at(index).x;
1510 ptr[1] = cloud.at(index).y;
1511 ptr[2] = cloud.at(index).z;
1512 ptr[3] = cloud.at(index).normal_x;
1513 ptr[4] = cloud.at(index).normal_y;
1514 ptr[5] = cloud.at(index).normal_z;
1521 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(6));
1522 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1524 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) &&
1529 float * ptr = laserScan.ptr<
float>(0, oi++);
1536 ptr[3] = pt.normal_x;
1537 ptr[4] = pt.normal_y;
1538 ptr[5] = pt.normal_z;
1542 ptr[0] = cloud.at(
i).x;
1543 ptr[1] = cloud.at(
i).y;
1544 ptr[2] = cloud.at(
i).z;
1545 ptr[3] = cloud.at(
i).normal_x;
1546 ptr[4] = cloud.at(
i).normal_y;
1547 ptr[5] = cloud.at(
i).normal_z;
1561 UASSERT(cloud.size() == normals.size());
1562 cv::Mat laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(6));
1565 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1567 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) && pcl::isFinite(normals.at(
i))))
1569 float * ptr = laserScan.ptr<
float>(0, oi++);
1572 pcl::PointNormal pt;
1573 pt.x = cloud.at(
i).x;
1574 pt.y = cloud.at(
i).y;
1575 pt.z = cloud.at(
i).z;
1576 pt.normal_x = normals.at(
i).normal_x;
1577 pt.normal_y = normals.at(
i).normal_y;
1578 pt.normal_z = normals.at(
i).normal_z;
1583 ptr[3] = pt.normal_x;
1584 ptr[4] = pt.normal_y;
1585 ptr[5] = pt.normal_z;
1589 ptr[0] = cloud.at(
i).x;
1590 ptr[1] = cloud.at(
i).y;
1591 ptr[2] = cloud.at(
i).z;
1592 ptr[3] = normals.at(
i).normal_x;
1593 ptr[4] = normals.at(
i).normal_y;
1594 ptr[5] = normals.at(
i).normal_z;
1618 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC(4));
1619 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1622 if(!filterNaNs || pcl::isFinite(cloud.at(index)))
1624 float * ptr = laserScan.ptr<
float>(0, oi++);
1634 ptr[0] = cloud.at(index).x;
1635 ptr[1] = cloud.at(index).y;
1636 ptr[2] = cloud.at(index).z;
1638 int * ptrInt = (
int*)ptr;
1639 ptrInt[3] =
int(cloud.at(index).b) | (
int(cloud.at(index).g) << 8) | (
int(cloud.at(index).r) << 16);
1645 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(4));
1646 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1648 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
1650 float * ptr = laserScan.ptr<
float>(0, oi++);
1660 ptr[0] = cloud.at(
i).x;
1661 ptr[1] = cloud.at(
i).y;
1662 ptr[2] = cloud.at(
i).z;
1664 int * ptrInt = (
int*)ptr;
1665 ptrInt[3] =
int(cloud.at(
i).b) | (
int(cloud.at(
i).g) << 8) | (
int(cloud.at(
i).r) << 16);
1689 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC(4));
1690 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1693 if(!filterNaNs || pcl::isFinite(cloud.at(index)))
1695 float * ptr = laserScan.ptr<
float>(0, oi++);
1705 ptr[0] = cloud.at(index).x;
1706 ptr[1] = cloud.at(index).y;
1707 ptr[2] = cloud.at(index).z;
1709 ptr[3] = cloud.at(index).intensity;
1715 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(4));
1716 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1718 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
1720 float * ptr = laserScan.ptr<
float>(0, oi++);
1730 ptr[0] = cloud.at(
i).x;
1731 ptr[1] = cloud.at(
i).y;
1732 ptr[2] = cloud.at(
i).z;
1734 ptr[3] = cloud.at(
i).intensity;
1747 UASSERT(cloud.size() == normals.size());
1748 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(7));
1751 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1753 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
1755 float * ptr = laserScan.ptr<
float>(0, oi++);
1758 pcl::PointXYZRGBNormal pt;
1759 pt.x = cloud.at(
i).x;
1760 pt.y = cloud.at(
i).y;
1761 pt.z = cloud.at(
i).z;
1762 pt.normal_x = normals.at(
i).normal_x;
1763 pt.normal_y = normals.at(
i).normal_y;
1764 pt.normal_z = normals.at(
i).normal_z;
1769 ptr[4] = pt.normal_x;
1770 ptr[5] = pt.normal_y;
1771 ptr[6] = pt.normal_z;
1775 ptr[0] = cloud.at(
i).x;
1776 ptr[1] = cloud.at(
i).y;
1777 ptr[2] = cloud.at(
i).z;
1778 ptr[4] = normals.at(
i).normal_x;
1779 ptr[5] = normals.at(
i).normal_y;
1780 ptr[6] = normals.at(
i).normal_z;
1782 int * ptrInt = (
int*)ptr;
1783 ptrInt[3] =
int(cloud.at(
i).b) | (
int(cloud.at(
i).g) << 8) | (
int(cloud.at(
i).r) << 16);
1804 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC(7));
1805 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1808 if(!filterNaNs || (pcl::isFinite(cloud.at(index)) &&
1813 float * ptr = laserScan.ptr<
float>(0, oi++);
1820 ptr[4] = pt.normal_x;
1821 ptr[5] = pt.normal_y;
1822 ptr[6] = pt.normal_z;
1826 ptr[0] = cloud.at(index).x;
1827 ptr[1] = cloud.at(index).y;
1828 ptr[2] = cloud.at(index).z;
1829 ptr[4] = cloud.at(index).normal_x;
1830 ptr[5] = cloud.at(index).normal_y;
1831 ptr[6] = cloud.at(index).normal_z;
1833 int * ptrInt = (
int*)ptr;
1834 ptrInt[3] =
int(cloud.at(index).b) | (
int(cloud.at(index).g) << 8) | (
int(cloud.at(index).r) << 16);
1840 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(7));
1841 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1843 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) &&
1848 float * ptr = laserScan.ptr<
float>(0, oi++);
1855 ptr[4] = pt.normal_x;
1856 ptr[5] = pt.normal_y;
1857 ptr[6] = pt.normal_z;
1861 ptr[0] = cloud.at(
i).x;
1862 ptr[1] = cloud.at(
i).y;
1863 ptr[2] = cloud.at(
i).z;
1864 ptr[4] = cloud.at(
i).normal_x;
1865 ptr[5] = cloud.at(
i).normal_y;
1866 ptr[6] = cloud.at(
i).normal_z;
1868 int * ptrInt = (
int*)ptr;
1869 ptrInt[3] =
int(cloud.at(
i).b) | (
int(cloud.at(
i).g) << 8) | (
int(cloud.at(
i).r) << 16);
1882 UASSERT(cloud.size() == normals.size());
1883 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(7));
1886 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1888 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) && pcl::isFinite(normals.at(
i))))
1890 float * ptr = laserScan.ptr<
float>(0, oi++);
1893 pcl::PointXYZINormal pt;
1894 pt.x = cloud.at(
i).x;
1895 pt.y = cloud.at(
i).y;
1896 pt.z = cloud.at(
i).z;
1897 pt.normal_x = normals.at(
i).normal_x;
1898 pt.normal_y = normals.at(
i).normal_y;
1899 pt.normal_z = normals.at(
i).normal_z;
1904 ptr[4] = pt.normal_x;
1905 ptr[5] = pt.normal_y;
1906 ptr[6] = pt.normal_z;
1910 ptr[0] = cloud.at(
i).x;
1911 ptr[1] = cloud.at(
i).y;
1912 ptr[2] = cloud.at(
i).z;
1913 ptr[4] = normals.at(
i).normal_x;
1914 ptr[5] = normals.at(
i).normal_y;
1915 ptr[6] = normals.at(
i).normal_z;
1917 ptr[3] = cloud.at(
i).intensity;
1938 laserScan = cv::Mat(1, (
int)
indices->size(), CV_32FC(7));
1939 for(
unsigned int i=0;
i<
indices->size(); ++
i)
1942 if(!filterNaNs || (pcl::isFinite(cloud.at(index)) &&
1947 float * ptr = laserScan.ptr<
float>(0, oi++);
1954 ptr[4] = pt.normal_x;
1955 ptr[5] = pt.normal_y;
1956 ptr[6] = pt.normal_z;
1960 ptr[0] = cloud.at(index).x;
1961 ptr[1] = cloud.at(index).y;
1962 ptr[2] = cloud.at(index).z;
1963 ptr[4] = cloud.at(index).normal_x;
1964 ptr[5] = cloud.at(index).normal_y;
1965 ptr[6] = cloud.at(index).normal_z;
1967 ptr[3] = cloud.at(
i).intensity;
1973 laserScan = cv::Mat(1, (
int)cloud.size(), CV_32FC(7));
1974 for(
unsigned int i=0;
i<cloud.size(); ++
i)
1976 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) &&
1981 float * ptr = laserScan.ptr<
float>(0, oi++);
1988 ptr[4] = pt.normal_x;
1989 ptr[5] = pt.normal_y;
1990 ptr[6] = pt.normal_z;
1994 ptr[0] = cloud.at(
i).x;
1995 ptr[1] = cloud.at(
i).y;
1996 ptr[2] = cloud.at(
i).z;
1997 ptr[4] = cloud.at(
i).normal_x;
1998 ptr[5] = cloud.at(
i).normal_y;
1999 ptr[6] = cloud.at(
i).normal_z;
2001 ptr[3] = cloud.at(
i).intensity;
2014 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC2);
2015 bool nullTransform =
transform.isNull();
2018 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2020 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
2022 float * ptr = laserScan.ptr<
float>(0, oi++);
2031 ptr[0] = cloud.at(
i).x;
2032 ptr[1] = cloud.at(
i).y;
2046 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC3);
2047 bool nullTransform =
transform.isNull();
2050 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2052 if(!filterNaNs || pcl::isFinite(cloud.at(
i)))
2054 float * ptr = laserScan.ptr<
float>(0, oi++);
2060 ptr[2] = pt.intensity;
2064 ptr[0] = cloud.at(
i).x;
2065 ptr[1] = cloud.at(
i).y;
2066 ptr[2] = cloud.at(
i).intensity;
2080 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(5));
2081 bool nullTransform =
transform.isNull();
2083 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2085 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) &&
2090 float * ptr = laserScan.ptr<
float>(0, oi++);
2096 ptr[2] = pt.normal_x;
2097 ptr[3] = pt.normal_y;
2098 ptr[4] = pt.normal_z;
2102 const pcl::PointNormal & pt = cloud.at(
i);
2105 ptr[2] = pt.normal_x;
2106 ptr[3] = pt.normal_y;
2107 ptr[4] = pt.normal_z;
2120 UASSERT(cloud.size() == normals.size());
2121 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(5));
2124 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2126 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) && pcl::isFinite(normals.at(
i))))
2128 float * ptr = laserScan.ptr<
float>(0, oi++);
2131 pcl::PointNormal pt;
2132 pt.x = cloud.at(
i).x;
2133 pt.y = cloud.at(
i).y;
2134 pt.z = cloud.at(
i).z;
2135 pt.normal_x = normals.at(
i).normal_x;
2136 pt.normal_y = normals.at(
i).normal_y;
2137 pt.normal_z = normals.at(
i).normal_z;
2141 ptr[2] = pt.normal_x;
2142 ptr[3] = pt.normal_y;
2143 ptr[4] = pt.normal_z;
2147 ptr[0] = cloud.at(
i).x;
2148 ptr[1] = cloud.at(
i).y;
2149 ptr[2] = normals.at(
i).normal_x;
2150 ptr[3] = normals.at(
i).normal_y;
2151 ptr[4] = normals.at(
i).normal_z;
2164 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(6));
2165 bool nullTransform =
transform.isNull();
2167 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2169 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) &&
2174 float * ptr = laserScan.ptr<
float>(0, oi++);
2180 ptr[2] = pt.intensity;
2181 ptr[3] = pt.normal_x;
2182 ptr[4] = pt.normal_y;
2183 ptr[5] = pt.normal_z;
2187 const pcl::PointXYZINormal & pt = cloud.at(
i);
2190 ptr[2] = pt.intensity;
2191 ptr[3] = pt.normal_x;
2192 ptr[4] = pt.normal_y;
2193 ptr[5] = pt.normal_z;
2206 UASSERT(cloud.size() == normals.size());
2207 cv::Mat laserScan(1, (
int)cloud.size(), CV_32FC(6));
2210 for(
unsigned int i=0;
i<cloud.size(); ++
i)
2212 if(!filterNaNs || (pcl::isFinite(cloud.at(
i)) && pcl::isFinite(normals.at(
i))))
2214 float * ptr = laserScan.ptr<
float>(0, oi++);
2217 pcl::PointXYZINormal pt;
2218 pt.x = cloud.at(
i).x;
2219 pt.y = cloud.at(
i).y;
2220 pt.z = cloud.at(
i).z;
2221 pt.normal_x = normals.at(
i).normal_x;
2222 pt.normal_y = normals.at(
i).normal_y;
2223 pt.normal_z = normals.at(
i).normal_z;
2227 ptr[2] = pt.intensity;
2228 ptr[3] = pt.normal_x;
2229 ptr[4] = pt.normal_y;
2230 ptr[5] = pt.normal_z;
2234 ptr[0] = cloud.at(
i).x;
2235 ptr[1] = cloud.at(
i).y;
2236 ptr[2] = cloud.at(
i).intensity;
2237 ptr[3] = normals.at(
i).normal_x;
2238 ptr[4] = normals.at(
i).normal_y;
2239 ptr[5] = normals.at(
i).normal_z;
2252 pcl::PCLPointCloud2::Ptr cloud(
new pcl::PCLPointCloud2);
2284 UERROR(
"Unknown conversion from LaserScan format %d to PointCloud2.", laserScan.
format());
2291 pcl::PointCloud<pcl::PointXYZ>::Ptr output(
new pcl::PointCloud<pcl::PointXYZ>);
2294 output->width = laserScan.
data().cols;
2295 output->height = laserScan.
data().rows;
2296 output->is_dense =
false;
2300 output->is_dense =
true;
2302 output->resize(laserScan.
size());
2303 bool nullTransform =
transform.isNull();
2305 for(
int i=0;
i<laserScan.
size(); ++
i)
2318 pcl::PointCloud<pcl::PointNormal>::Ptr output(
new pcl::PointCloud<pcl::PointNormal>);
2321 output->width = laserScan.
data().cols;
2322 output->height = laserScan.
data().rows;
2323 output->is_dense =
false;
2327 output->is_dense =
true;
2329 output->resize(laserScan.
size());
2330 bool nullTransform =
transform.isNull();
2331 for(
int i=0;
i<laserScan.
size(); ++
i)
2344 pcl::PointCloud<pcl::PointXYZRGB>::Ptr output(
new pcl::PointCloud<pcl::PointXYZRGB>);
2347 output->width = laserScan.
data().cols;
2348 output->height = laserScan.
data().rows;
2349 output->is_dense =
false;
2353 output->is_dense =
true;
2355 output->resize(laserScan.
size());
2358 for(
int i=0;
i<laserScan.
size(); ++
i)
2371 pcl::PointCloud<pcl::PointXYZI>::Ptr output(
new pcl::PointCloud<pcl::PointXYZI>);
2374 output->width = laserScan.
data().cols;
2375 output->height = laserScan.
data().rows;
2376 output->is_dense =
false;
2380 output->is_dense =
true;
2382 output->resize(laserScan.
size());
2385 for(
int i=0;
i<laserScan.
size(); ++
i)
2398 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr output(
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
2401 output->width = laserScan.
data().cols;
2402 output->height = laserScan.
data().rows;
2403 output->is_dense =
false;
2407 output->is_dense =
true;
2409 output->resize(laserScan.
size());
2411 for(
int i=0;
i<laserScan.
size(); ++
i)
2424 pcl::PointCloud<pcl::PointXYZINormal>::Ptr output(
new pcl::PointCloud<pcl::PointXYZINormal>);
2427 output->width = laserScan.
data().cols;
2428 output->height = laserScan.
data().rows;
2429 output->is_dense =
false;
2433 output->is_dense =
true;
2435 output->resize(laserScan.
size());
2437 for(
int i=0;
i<laserScan.
size(); ++
i)
2451 pcl::PointXYZ output;
2452 int row = index / laserScan.
data().cols;
2453 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2456 if(!laserScan.
is2d())
2466 pcl::PointNormal output;
2467 int row = index / laserScan.
data().cols;
2468 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2471 if(!laserScan.
is2d())
2478 output.normal_x = ptr[
offset];
2479 output.normal_y = ptr[
offset+1];
2480 output.normal_z = ptr[
offset+2];
2488 pcl::PointXYZRGB output;
2489 int row = index / laserScan.
data().cols;
2490 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2493 if(!laserScan.
is2d())
2500 int * ptrInt = (
int*)ptr;
2502 output.b = (
unsigned char)(ptrInt[indexRGB] & 0xFF);
2503 output.g = (
unsigned char)((ptrInt[indexRGB] >> 8) & 0xFF);
2504 output.r = (
unsigned char)((ptrInt[indexRGB] >> 16) & 0xFF);
2509 int * ptrInt = (
int*)ptr;
2511 output.r = (
unsigned char)(ptrInt[indexIntensity] & 0xFF);
2512 output.g = (
unsigned char)((ptrInt[indexIntensity] >> 8) & 0xFF);
2513 output.b = (
unsigned char)((ptrInt[indexIntensity] >> 16) & 0xFF);
2514 output.a = (
unsigned char)((ptrInt[indexIntensity] >> 24) & 0xFF);
2528 pcl::PointXYZI output;
2529 int row = index / laserScan.
data().cols;
2530 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2533 if(!laserScan.
is2d())
2541 output.intensity = ptr[
offset];
2545 output.intensity = intensity;
2554 pcl::PointXYZRGBNormal output;
2555 int row = index / laserScan.
data().cols;
2556 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2559 if(!laserScan.
is2d())
2566 int * ptrInt = (
int*)ptr;
2568 output.b = (
unsigned char)(ptrInt[indexRGB] & 0xFF);
2569 output.g = (
unsigned char)((ptrInt[indexRGB] >> 8) & 0xFF);
2570 output.r = (
unsigned char)((ptrInt[indexRGB] >> 16) & 0xFF);
2574 int * ptrInt = (
int*)ptr;
2576 output.r = (
unsigned char)(ptrInt[indexIntensity] & 0xFF);
2577 output.g = (
unsigned char)((ptrInt[indexIntensity] >> 8) & 0xFF);
2578 output.b = (
unsigned char)((ptrInt[indexIntensity] >> 16) & 0xFF);
2579 output.a = (
unsigned char)((ptrInt[indexIntensity] >> 24) & 0xFF);
2591 output.normal_x = ptr[
offset];
2592 output.normal_y = ptr[
offset+1];
2593 output.normal_z = ptr[
offset+2];
2602 pcl::PointXYZINormal output;
2603 int row = index / laserScan.
data().cols;
2604 const float * ptr = laserScan.
data().ptr<
float>(
row, index -
row*laserScan.
data().cols);
2607 if(!laserScan.
is2d())
2615 output.intensity = ptr[
offset];
2619 output.intensity = intensity;
2625 output.normal_x = ptr[
offset];
2626 output.normal_y = ptr[
offset+1];
2627 output.normal_z = ptr[
offset+2];
2636 UASSERT(laserScan.type() == CV_32FC2 || laserScan.type() == CV_32FC3 || laserScan.type() == CV_32FC(4) || laserScan.type() == CV_32FC(5) || laserScan.type() == CV_32FC(6) || laserScan.type() == CV_32FC(7));
2638 const float * ptr = laserScan.ptr<
float>(0, 0);
2641 bool is3d = laserScan.channels() >= 3 && laserScan.channels() != 5;
2642 min.z =
max.z = is3d?ptr[2]:0.0f;
2643 for(
int i=1;
i<laserScan.cols; ++
i)
2645 ptr = laserScan.ptr<
float>(0,
i);
2647 if(ptr[0] <
min.x)
min.x = ptr[0];
2648 else if(ptr[0] >
max.x)
max.x = ptr[0];
2650 if(ptr[1] <
min.y)
min.y = ptr[1];
2651 else if(ptr[1] >
max.y)
max.y = ptr[1];
2655 if(ptr[2] <
min.z)
min.z = ptr[2];
2656 else if(ptr[2] >
max.z)
max.z = ptr[2];
2662 cv::Point3f minCV, maxCV;
2674 const cv::Point2f & pt,
2678 if(disparity > 0.0
f &&
model.baseline() > 0.0f &&
model.left().fx() > 0.0f)
2682 if(
model.right().cx()>0.0f &&
model.left().cx()>0.0f)
2686 float W =
model.baseline()/(disparity +
c);
2687 return cv::Point3f((pt.x -
model.left().cx())*
W,
2690 float bad_point = std::numeric_limits<float>::quiet_NaN ();
2691 return cv::Point3f(bad_point, bad_point, bad_point);
2695 const cv::Point2f & pt,
2696 const cv::Mat & disparity,
2699 UASSERT(!disparity.empty() && (disparity.type() == CV_32FC1 || disparity.type() == CV_16SC1));
2700 int u =
int(pt.x+0.5f);
2701 int v =
int(pt.y+0.5f);
2702 float bad_point = std::numeric_limits<float>::quiet_NaN ();
2706 float d = disparity.type() == CV_16SC1?
float(disparity.at<
short>(
v,u))/16.0f:disparity.at<
float>(
v,u);
2709 return cv::Point3f(bad_point, bad_point, bad_point);
2714 const cv::Size & imageSize,
2715 const cv::Mat & cameraMatrixK,
2716 const cv::Mat & laserScan,
2721 UASSERT(laserScan.type() == CV_32FC2 || laserScan.type() == CV_32FC3 || laserScan.type() == CV_32FC(4) || laserScan.type() == CV_32FC(5) || laserScan.type() == CV_32FC(6) || laserScan.type() == CV_32FC(7));
2722 UASSERT(cameraMatrixK.type() == CV_64FC1 && cameraMatrixK.cols == 3 && cameraMatrixK.cols == 3);
2724 float fx = cameraMatrixK.at<
double>(0,0);
2725 float fy = cameraMatrixK.at<
double>(1,1);
2726 float cx = cameraMatrixK.at<
double>(0,2);
2727 float cy = cameraMatrixK.at<
double>(1,2);
2729 cv::Mat registered = cv::Mat::zeros(imageSize, CV_32FC1);
2733 for(
int i=0;
i<laserScan.cols; ++
i)
2735 const float* ptr = laserScan.ptr<
float>(0,
i);
2739 if(laserScan.type() == CV_32FC2 || laserScan.type() == CV_32FC(5))
2760 float invZ = 1.0f/
z;
2761 float dx = (
fx*ptScan.x)*invZ +
cx;
2762 float dy = (
fy*ptScan.y)*invZ +
cy;
2765 int dx_high = dx + 0.5f;
2766 int dy_high = dy + 0.5f;
2770 float &zReg = registered.at<
float>(dy_low, dx_low);
2771 if(zReg == 0 ||
z < zReg)
2777 if((dx_low != dx_high || dy_low != dy_high) &&
2780 float &zReg = registered.at<
float>(dy_high, dx_high);
2781 if(zReg == 0 ||
z < zReg)
2793 UDEBUG(
"Points in camera=%d/%d",
count, laserScan.cols);
2799 const cv::Size & imageSize,
2800 const cv::Mat & cameraMatrixK,
2801 const pcl::PointCloud<pcl::PointXYZ>::Ptr laserScan,
2806 UASSERT(cameraMatrixK.type() == CV_64FC1 && cameraMatrixK.cols == 3 && cameraMatrixK.cols == 3);
2808 float fx = cameraMatrixK.at<
double>(0,0);
2809 float fy = cameraMatrixK.at<
double>(1,1);
2810 float cx = cameraMatrixK.at<
double>(0,2);
2811 float cy = cameraMatrixK.at<
double>(1,2);
2813 cv::Mat registered = cv::Mat::zeros(imageSize, CV_32FC1);
2817 for(
int i=0;
i<(
int)laserScan->size(); ++
i)
2820 pcl::PointXYZ ptScan = laserScan->at(
i);
2828 float invZ = 1.0f/
z;
2829 float dx = (
fx*ptScan.x)*invZ +
cx;
2830 float dy = (
fy*ptScan.y)*invZ +
cy;
2833 int dx_high = dx + 0.5f;
2834 int dy_high = dy + 0.5f;
2838 float &zReg = registered.at<
float>(dy_low, dx_low);
2839 if(zReg == 0 ||
z < zReg)
2844 if((dx_low != dx_high || dy_low != dy_high) &&
2848 float &zReg = registered.at<
float>(dy_high, dx_high);
2849 if(zReg == 0 ||
z < zReg)
2860 UDEBUG(
"Points in camera=%d/%d",
count, (
int)laserScan->size());
2866 const cv::Size & imageSize,
2867 const cv::Mat & cameraMatrixK,
2868 const pcl::PCLPointCloud2::Ptr laserScan,
2872 UASSERT(!laserScan->data.empty());
2873 UASSERT(cameraMatrixK.type() == CV_64FC1 && cameraMatrixK.cols == 3 && cameraMatrixK.cols == 3);
2875 float fx = cameraMatrixK.at<
double>(0,0);
2876 float fy = cameraMatrixK.at<
double>(1,1);
2877 float cx = cameraMatrixK.at<
double>(0,2);
2878 float cy = cameraMatrixK.at<
double>(1,2);
2880 cv::Mat registered = cv::Mat::zeros(imageSize, CV_32FC1);
2883 pcl::MsgFieldMap field_map;
2884 pcl::createMapping<pcl::PointXYZ> (laserScan->fields, field_map);
2887 if(field_map.size() == 1)
2891 const uint8_t* row_data = &laserScan->data[
row * laserScan->row_step];
2894 const uint8_t* msg_data = row_data +
col * laserScan->point_step;
2895 pcl::PointXYZ ptScan;
2896 memcpy (&ptScan, msg_data + field_map.front().serialized_offset, field_map.front().size);
2904 float invZ = 1.0f/
z;
2905 float dx = (
fx*ptScan.x)*invZ +
cx;
2906 float dy = (
fy*ptScan.y)*invZ +
cy;
2909 int dx_high = dx + 0.5f;
2910 int dy_high = dy + 0.5f;
2914 float &zReg = registered.at<
float>(dy_low, dx_low);
2915 if(zReg == 0 ||
z < zReg)
2920 if((dx_low != dx_high || dy_low != dy_high) &&
2924 float &zReg = registered.at<
float>(dy_high, dx_high);
2925 if(zReg == 0 ||
z < zReg)
2940 UERROR(
"field map pcl::pointXYZ not found!");
2942 UDEBUG(
"Points in camera=%d/%d",
count, (
int)laserScan->data.size());
2949 UASSERT(registeredDepth.type() == CV_32FC1);
2950 if(verticalDirection)
2953 for(
int x=0;
x<registeredDepth.cols; ++
x)
2955 float valueA = 0.0f;
2957 for(
int y=0;
y<registeredDepth.rows; ++
y)
2959 float v = registeredDepth.at<
float>(
y,
x);
2960 if(fillToBorder &&
y == registeredDepth.rows-1 && v<=0.0f && indexA>=0)
2966 if(fillToBorder && indexA < 0)
2976 float slope = (
v-valueA)/(
range);
2977 for(
int k=1; k<
range; ++k)
2979 registeredDepth.at<
float>(indexA+k,
x) = valueA+slope*
float(k);
2992 for(
int y=0;
y<registeredDepth.rows; ++
y)
2994 float valueA = 0.0f;
2996 for(
int x=0;
x<registeredDepth.cols; ++
x)
2998 float v = registeredDepth.at<
float>(
y,
x);
2999 if(fillToBorder &&
x == registeredDepth.cols-1 && v<=0.0f && indexA>=0)
3005 if(fillToBorder && indexA < 0)
3015 float slope = (
v-valueA)/(
range);
3016 for(
int k=1; k<
range; ++k)
3018 registeredDepth.at<
float>(
y,indexA+k) = valueA+slope*
float(k);
3030 cv::Mat
filterFloor(
const cv::Mat & depth,
const std::vector<CameraModel> & cameraModels,
float threshold, cv::Mat * depthBelow)
3032 cv::Mat output = depth.clone();
3039 *depthBelow = cv::Mat::zeros(output.size(), output.type());
3042 UASSERT(!cameraModels.empty());
3043 UASSERT(cameraModels[0].isValidForReprojection());
3045 float rgbToDepthFactorX =
float(cameraModels[0].imageWidth()) /
float(output.cols/cameraModels.size());
3046 float rgbToDepthFactorY =
float(cameraModels[0].imageHeight()) /
float(output.rows);
3047 int depthWidth = output.cols/cameraModels.size();
3048 UASSERT(depthWidth*(
int)cameraModels.size() == output.cols);
3051 for(
size_t i=0;
i<cameraModels.size(); ++
i)
3055 const Transform & localTransform =
cam.localTransform();
3060 UASSERT(
cam.imageWidth() == cameraModels[
i-1].imageWidth());
3061 UASSERT(
cam.imageHeight() == cameraModels[
i-1].imageHeight());
3064 float depthFx =
cam.fx() / rgbToDepthFactorX;
3065 float depthFy =
cam.fy() / rgbToDepthFactorY;
3066 float depthCx =
cam.cx() / rgbToDepthFactorX;
3067 float depthCy =
cam.cy() / rgbToDepthFactorY;
3069 cv::Mat subImage = output.colRange(cv::Range(
i*depthWidth, (
i+1)*depthWidth));
3070 cv::Mat subImageBelow;
3072 subImageBelow = depthBelow->colRange(cv::Range(
i*depthWidth, (
i+1)*depthWidth));
3074 for(
int y=0;
y<subImage.rows; ++
y)
3076 if(subImage.type() == CV_16UC1)
3078 unsigned short * ptr = (
unsigned short *)subImage.row(
y).ptr();
3079 unsigned short * ptrBelow = 0;
3082 ptrBelow = (
unsigned short *)subImageBelow.row(
y).ptr();
3084 for(
int x=0;
x<subImage.cols; ++
x)
3088 float d =
float(ptr[
x])/1000.0f;
3090 pt.x = (
x - depthCx) *
d / depthFx;
3091 pt.y = (
y - depthCy) *
d / depthFy;
3094 if(pt.z < threshold)
3098 ptrBelow[
x] = ptr[
x];
3107 float * ptr = (
float *)subImage.row(
y).ptr();
3108 float * ptrBelow = 0;
3111 ptrBelow = (
float *)subImageBelow.row(
y).ptr();
3113 for(
int x=0;
x<subImage.cols; ++
x)
3119 pt.x = (
x - depthCx) *
d / depthFx;
3120 pt.y = (
y - depthCy) *
d / depthFy;
3123 if(pt.z < threshold)
3127 ptrBelow[
x] = ptr[
x];
3156 template<
class Po
intT>
3158 const typename pcl::PointCloud<PointT> & cloud,
3159 const std::map<int, Transform> & cameraPoses,
3160 const std::map<
int, std::vector<CameraModel> > & cameraModels,
3163 const std::vector<float> & roiRatios,
3164 const cv::Mat & projMask,
3165 bool distanceToCamPolicy,
3168 UINFO(
"cloud=%d points", (
int)cloud.size());
3170 UINFO(
"cameraModels=%d", (
int)cameraModels.size());
3171 UINFO(
"maxDistance=%f", maxDistance);
3172 UINFO(
"maxAngle=%f", maxAngle);
3173 UINFO(
"distanceToCamPolicy=%s", distanceToCamPolicy?
"true":
"false");
3174 UINFO(
"roiRatios=%s", roiRatios.size() == 4?
uFormat(
"%f %f %f %f", roiRatios[0], roiRatios[1], roiRatios[2], roiRatios[3]).
c_str():
"");
3175 UINFO(
"projMask=%dx%d", projMask.cols, projMask.rows);
3176 std::vector<std::pair< std::pair<int, int>, pcl::PointXY> > pointToPixel;
3178 if (cloud.empty() ||
cameraPoses.empty() || cameraModels.empty())
3179 return pointToPixel;
3181 std::string
msg =
uFormat(
"Computing visible points per cam (%d points, %d cams)", (
int)cloud.size(), (
int)
cameraPoses.size());
3186 UWARN(
"Projecting to cameras cancelled!");
3187 return pointToPixel;
3190 std::vector<ProjectionInfo> invertedIndex(cloud.size());
3191 int cameraProcessed = 0;
3192 bool wrongMaskFormatWarned =
false;
3193 for(std::map<int, Transform>::const_iterator pter =
cameraPoses.lower_bound(0); pter!=
cameraPoses.end(); ++pter)
3195 std::map<int, std::vector<CameraModel> >::const_iterator
iter=cameraModels.find(pter->first);
3196 if(
iter!=cameraModels.end() && !
iter->second.empty())
3198 cv::Mat validProjMask;
3199 if(!projMask.empty())
3201 if(projMask.type() != CV_8UC1)
3203 if(!wrongMaskFormatWarned)
3204 UERROR(
"Wrong camera projection mask type %d, should be CV_8UC1", projMask.type());
3205 wrongMaskFormatWarned =
true;
3207 else if(projMask.cols ==
iter->second[0].imageWidth() * (
int)
iter->second.size() &&
3208 projMask.rows ==
iter->second[0].imageHeight())
3210 validProjMask = projMask;
3214 UWARN(
"Camera projection mask (%dx%d) is not valid for current "
3215 "camera model(s) (count=%ld, image size=%dx%d). It will be "
3216 "ignored for node %d",
3217 projMask.cols, projMask.rows,
3218 iter->second.size(),
3219 iter->second[0].imageWidth(),
3220 iter->second[0].imageHeight(),
3225 for(
size_t camIndex=0; camIndex<
iter->second.size(); ++camIndex)
3227 Transform cameraTransform = (pter->second *
iter->second[camIndex].localTransform());
3229 cv::Mat cameraMatrixK =
iter->second[camIndex].K();
3230 UASSERT(cameraMatrixK.type() == CV_64FC1 && cameraMatrixK.cols == 3 && cameraMatrixK.cols == 3);
3231 const cv::Size & imageSize =
iter->second[camIndex].imageSize();
3233 float fx = cameraMatrixK.at<
double>(0,0);
3234 float fy = cameraMatrixK.at<
double>(1,1);
3235 float cx = cameraMatrixK.at<
double>(0,2);
3236 float cy = cameraMatrixK.at<
double>(1,2);
3239 cv::Mat registered = cv::Mat::zeros(imageSize, CV_32SC2);
3242 cv::Rect roi(0,0,imageSize.width, imageSize.height);
3243 if(roiRatios.size()==4)
3249 for(
size_t i=0;
i<cloud.size(); ++
i)
3252 PointT ptScan = cloud.at(
i);
3258 if(
z > 0.0
f && (maxDistance<=0 ||
z<maxDistance))
3260 float invZ = 1.0f/
z;
3261 float dx = (
fx*ptScan.x)*invZ +
cx;
3262 float dy = (
fy*ptScan.y)*invZ +
cy;
3265 int dx_high = dx + 0.5f;
3266 int dy_high = dy + 0.5f;
3269 (validProjMask.empty() || validProjMask.at<
unsigned char>(dy_low, imageSize.width*camIndex+dx_low) > 0))
3272 cv::Vec2i &zReg = registered.at<cv::Vec2i>(dy_low, dx_low);
3273 if(zReg[0] == 0 || zMM < zReg[0])
3279 if((dx_low != dx_high || dy_low != dy_high) &&
3281 (validProjMask.empty() || validProjMask.at<
unsigned char>(dy_high, imageSize.width*camIndex+dx_high) > 0))
3284 cv::Vec2i &zReg = registered.at<cv::Vec2i>(dy_high, dx_high);
3285 if(zReg[0] == 0 || zMM < zReg[0])
3299 registered = cv::Mat();
3300 UINFO(
"No points projected in camera %d/%d", pter->first, camIndex);
3304 UDEBUG(
"%d points projected in camera %d/%d",
count, pter->first, camIndex);
3306 for(
int u=0; u<registered.cols; ++u)
3308 for(
int v=0;
v<registered.rows; ++
v)
3310 cv::Vec2i &zReg = registered.at<cv::Vec2i>(
v, u);
3314 info.nodeID = pter->first;
3315 info.cameraIndex = camIndex;
3319 const PointT & pt = cloud.at(zReg[1]);
3320 Eigen::Vector4f camDir(
cam.x()-pt.x,
cam.y()-pt.y,
cam.z()-pt.z, 0);
3321 Eigen::Vector4f
normal(pt.normal_x, pt.normal_y, pt.normal_z, 0);
3322 float angleToCam = maxAngle<=0?0:pcl::getAngle3D(
normal, camDir);
3323 float distanceToCam = zReg[0]/1000.0f;
3324 if( (maxAngle<=0 || (camDir.dot(
normal) > 0 && angleToCam < maxAngle)) &&
3325 (maxDistance<=0 || distanceToCam<maxDistance))
3327 float vx =
info.uv.x-0.5f;
3328 float vy =
info.uv.y-0.5f;
3330 float distanceToCenter =
vx*
vx+
vy*
vy;
3332 if(distanceToCamPolicy)
3339 if(invertedIndex[zReg[1]].
distance != -1.0
f)
3343 invertedIndex[zReg[1]] =
info;
3348 invertedIndex[zReg[1]] =
info;
3362 UWARN(
"Projecting to cameras cancelled!");
3363 return pointToPixel;
3368 msg =
uFormat(
"Select best camera for %d points...", (
int)cloud.size());
3373 UWARN(
"Projecting to cameras cancelled!");
3374 return pointToPixel;
3377 pointToPixel.resize(invertedIndex.size());
3381 for(
size_t i=0;
i<invertedIndex.size(); ++
i)
3384 int cameraIndex = -1;
3385 pcl::PointXY uv_coords;
3388 nodeID = invertedIndex[
i].nodeID;
3389 cameraIndex = invertedIndex[
i].cameraIndex;
3390 uv_coords = invertedIndex[
i].uv;
3393 if(nodeID>-1 && cameraIndex> -1)
3395 pointToPixel[
i].first.first = nodeID;
3396 pointToPixel[
i].first.second = cameraIndex;
3397 pointToPixel[
i].second = uv_coords;
3402 msg =
uFormat(
"Process %d points...done! (%d [%d%%] projected in cameras)", (
int)cloud.size(), colorized, colorized*100/cloud.size());
3409 return pointToPixel;
3413 const typename pcl::PointCloud<pcl::PointXYZRGBNormal> & cloud,
3414 const std::map<int, Transform> & cameraPoses,
3415 const std::map<
int, std::vector<CameraModel> > & cameraModels,
3418 const std::vector<float> & roiRatios,
3419 const cv::Mat & projMask,
3420 bool distanceToCamPolicy,
3430 distanceToCamPolicy,
3435 const typename pcl::PointCloud<pcl::PointXYZINormal> & cloud,
3436 const std::map<int, Transform> & cameraPoses,
3437 const std::map<
int, std::vector<CameraModel> > & cameraModels,
3440 const std::vector<float> & roiRatios,
3441 const cv::Mat & projMask,
3442 bool distanceToCamPolicy,
3452 distanceToCamPolicy,
3461 pcl::PointCloud<pcl::PointXYZ>::Ptr
concatenateClouds(
const std::list<pcl::PointCloud<pcl::PointXYZ>::Ptr> & clouds)
3463 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
3464 for(std::list<pcl::PointCloud<pcl::PointXYZ>::Ptr>::const_iterator
iter = clouds.begin();
iter!=clouds.end(); ++
iter)
3471 pcl::PointCloud<pcl::PointXYZRGB>::Ptr
concatenateClouds(
const std::list<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> & clouds)
3473 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
3474 for(std::list<pcl::PointCloud<pcl::PointXYZRGB>::Ptr>::const_iterator
iter = clouds.begin();
iter!=clouds.end(); ++
iter)
3481 pcl::IndicesPtr
concatenate(
const std::vector<pcl::IndicesPtr> & indices)
3484 unsigned int totalSize = 0;
3489 pcl::IndicesPtr
ind(
new std::vector<int>(totalSize));
3490 unsigned int io = 0;
3501 pcl::IndicesPtr
concatenate(
const pcl::IndicesPtr & indicesA,
const pcl::IndicesPtr & indicesB)
3503 pcl::IndicesPtr
ind(
new std::vector<int>(*indicesA));
3504 ind->resize(
ind->size()+indicesB->size());
3505 unsigned int oi = (
unsigned int)indicesA->size();
3506 for(
unsigned int i=0;
i<indicesB->size(); ++
i)
3508 ind->at(oi++) = indicesB->at(
i);
3514 const std::string & fileName,
3515 const std::multimap<int, pcl::PointXYZ> & words,
3520 pcl::PointCloud<pcl::PointXYZ> cloud;
3521 cloud.resize(words.size());
3523 for(std::multimap<int, pcl::PointXYZ>::const_iterator
iter=words.begin();
iter!=words.end(); ++
iter)
3527 pcl::io::savePCDFile(fileName, cloud);
3532 const std::string & fileName,
3533 const std::multimap<int, cv::Point3f> & words,
3538 pcl::PointCloud<pcl::PointXYZ> cloud;
3539 cloud.resize(words.size());
3541 for(std::multimap<int, cv::Point3f>::const_iterator
iter=words.begin();
iter!=words.end(); ++
iter)
3544 cloud[
i++] = pcl::PointXYZ(pt.x, pt.y, pt.z);
3546 pcl::io::savePCDFile(fileName, cloud);
3560 output = cv::Mat(1, num/
dim, CV_32FC(
dim));
3564 stream = fopen (fileName.c_str(),
"rb");
3565 size_t actualReadNum = fread(output.data,
sizeof(
float),num,
stream);
3566 UASSERT(num == actualReadNum);
3573 pcl::PointCloud<pcl::PointXYZ>::Ptr
loadBINCloud(
const std::string & fileName)
3578 pcl::PointCloud<pcl::PointXYZ>::Ptr
loadBINCloud(
const std::string & fileName,
int dim)
3592 pcl::PCLPointCloud2::Ptr cloud(
new pcl::PCLPointCloud2);
3596 pcl::io::loadPCDFile(
path, *cloud);
3600 pcl::io::loadPLYFile(
path, *cloud);
3602 if(cloud->height > 1)
3604 cloud->is_dense =
false;
3608 if(!cloud->data.empty())
3612 for(
unsigned int i=0;
i<cloud->fields.size(); ++
i)
3614 if(cloud->fields[
i].name.compare(
"z") == 0)
3616 zOffset = cloud->fields[
i].offset;
3625 const uint8_t* row_data = &cloud->data[
row * cloud->row_step];
3628 const uint8_t* msg_data = row_data +
col * cloud->point_step;
3629 float z = *(
float*)(msg_data + zOffset);
3642 const std::string & path,
3648 UDEBUG(
"Loading cloud (step=%d, voxel=%f m) : %s", downsampleStep, voxelSize,
path.c_str());
3650 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZ>);
3657 pcl::io::loadPCDFile(
path, *cloud);
3661 pcl::io::loadPLYFile(
path, *cloud);
3663 int previousSize = (
int)cloud->size();
3664 if(downsampleStep > 1 && cloud->size())
3667 UDEBUG(
"Downsampling scan (step=%d): %d -> %d", downsampleStep, previousSize, (
int)cloud->size());
3669 previousSize = (
int)cloud->size();
3670 if(voxelSize > 0.0
f && cloud->size())
3673 UDEBUG(
"Voxel filtering scan (voxel=%f m): %d -> %d", voxelSize, previousSize, (
int)cloud->size());
3689 UERROR(
"velocity should be valid!");
3701 UERROR(
"input scan is empty!");
3710 firstStamp = inputStamp + input.
data().ptr<
float>(0, 0)[offsetTime];
3711 lastStamp = inputStamp + input.
data().ptr<
float>(0, input.
size()-1)[offsetTime];
3713 if(lastStamp <= firstStamp)
3715 UERROR(
"First and last stamps in the scan are the same!");
3722 float vx,
vy,vz, vroll,vpitch,vyaw;
3723 velocity.getTranslationAndEulerAngles(
vx,
vy,vz, vroll,vpitch,vyaw);
3727 double dt1 = firstStamp - inputStamp;
3728 double dt2 = lastStamp - inputStamp;
3735 UERROR(
"Could not get transform between stamps %f and %f!",
3742 UERROR(
"Could not get transform between stamps %f and %f!",
3750 double scanTime = lastStamp - firstStamp;
3751 cv::Mat output(1, input.
size(), CV_32FC4);
3756 bool timeOnColumns = input.
data().cols > input.
data().rows;
3765 for(
int u=0; u<input.
data().
cols; ++u)
3767 const float * inputPtr = input.
data().ptr<
float>(0, u);
3768 stamp = inputStamp + inputPtr[offsetTime];
3771 for(
int v=0;
v<input.
data().rows; ++
v)
3773 inputPtr = input.
data().ptr<
float>(
v, u);
3774 pcl::PointXYZ pt(inputPtr[0],inputPtr[1],inputPtr[2]);
3775 if(pcl::isFinite(pt))
3777 if(!isLocalTransformIdentity)
3782 if(!isLocalTransformIdentity)
3786 float * dataPtr = output.ptr<
float>(0, oi++);
3790 dataPtr[3] = input.
data().ptr<
float>(
v, u)[offsetIntensity];
3802 for(
int v=0;
v<input.
data().rows; ++
v)
3804 const float * inputPtr = input.
data().ptr<
float>(
v, 0);
3805 stamp = inputStamp + inputPtr[offsetTime];
3808 for(
int u=0; u<input.
data().cols; ++u)
3810 inputPtr = input.
data().ptr<
float>(
v, u);
3811 pcl::PointXYZ pt(inputPtr[0],inputPtr[1],inputPtr[2]);
3812 if(pcl::isFinite(pt))
3814 if(!isLocalTransformIdentity)
3819 if(!isLocalTransformIdentity)
3823 float * dataPtr = output.ptr<
float>(0, oi++);
3827 dataPtr[3] = input.
data().ptr<
float>(
v, u)[offsetIntensity];
3833 UDEBUG(
"Lidar deskewing time=%fs", processingTime.
elapsed());