Refactored texture projection (avoid saving image on disk at this step)

This commit is contained in:
matlabbe
2017-01-16 18:29:14 -05:00
parent 2b8f40e505
commit a011a6af64
7 changed files with 208 additions and 127 deletions

View File

@@ -73,18 +73,18 @@ public:
void apply(
int id,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud) const;
void apply(
int id,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
const pcl::IndicesPtr & indices);
const pcl::IndicesPtr & indices) const;
void apply(
int id,
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud,
const pcl::IndicesPtr & indices);
const pcl::IndicesPtr & indices) const;
void apply(
int id,
cv::Mat & image);
cv::Mat & image) const;
double getGain(int id) const;
int getIndex(int id) const;

View File

@@ -136,8 +136,6 @@ pcl::TextureMesh::Ptr RTABMAP_EXP createTextureMesh(
const pcl::PolygonMesh::Ptr & mesh,
const std::map<int, Transform> & poses,
const std::map<int, CameraModel> & cameraModels,
const std::map<int, cv::Mat> & images,
const std::string & tmpDirectory = ".",
int kNormalSearch = 20); // if mesh doesn't have normals, compute them with k neighbors
pcl::PointCloud<pcl::Normal>::Ptr RTABMAP_EXP computeNormals(

View File

@@ -375,7 +375,7 @@ void applyImpl(
void GainCompensator::apply(
int id,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud)
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud) const
{
pcl::IndicesPtr indices(new std::vector<int>);
apply(id, cloud, indices);
@@ -383,7 +383,7 @@ void GainCompensator::apply(
void GainCompensator::apply(
int id,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
const pcl::IndicesPtr & indices)
const pcl::IndicesPtr & indices) const
{
UASSERT_MSG(uContains(idToIndex_, id), uFormat("id=%d idToIndex_.size()=%d", id, (int)idToIndex_.size()).c_str());
applyImpl<pcl::PointXYZRGB>(idToIndex_.at(id), cloud, indices, gains_);
@@ -391,7 +391,7 @@ void GainCompensator::apply(
void GainCompensator::apply(
int id,
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud,
const pcl::IndicesPtr & indices)
const pcl::IndicesPtr & indices) const
{
UASSERT_MSG(uContains(idToIndex_, id), uFormat("id=%d idToIndex_.size()=%d", id, (int)idToIndex_.size()).c_str());
applyImpl<pcl::PointXYZRGBNormal>(idToIndex_.at(id), cloud, indices, gains_);
@@ -399,7 +399,7 @@ void GainCompensator::apply(
void GainCompensator::apply(
int id,
cv::Mat & image)
cv::Mat & image) const
{
UASSERT_MSG(uContains(idToIndex_, id), uFormat("id=%d idToIndex_.size()=%d", id, (int)idToIndex_.size()).c_str());
cv::multiply(image, gains_(idToIndex_.at(id), 0), image);

View File

@@ -569,21 +569,16 @@ pcl::PolygonMesh::Ptr createMesh(
pcl::texture_mapping::CameraVector createTextureCameras(
const std::map<int, Transform> & poses,
const std::map<int, CameraModel> & cameraModels,
const std::map<int, cv::Mat> & images,
const std::string & tmpDirectory)
const std::map<int, CameraModel> & cameraModels)
{
UASSERT(poses.size() == cameraModels.size() && poses.size() == images.size());
UASSERT(UDirectory::exists(tmpDirectory));
UASSERT(poses.size() == cameraModels.size());
pcl::texture_mapping::CameraVector cameras(poses.size());
std::map<int, Transform>::const_iterator poseIter=poses.begin();
std::map<int, CameraModel>::const_iterator modelIter=cameraModels.begin();
std::map<int, cv::Mat>::const_iterator imageIter=images.begin();
int oi=0;
for(; poseIter!=poses.end(); ++poseIter, ++modelIter, ++imageIter)
for(; poseIter!=poses.end(); ++poseIter, ++modelIter)
{
UASSERT(poseIter->first == modelIter->first);
UASSERT(poseIter->first == imageIter->first);
pcl::TextureMapping<pcl::PointXYZ>::Camera cam;
// transform into optical referential
@@ -595,22 +590,17 @@ pcl::texture_mapping::CameraVector createTextureCameras(
cam.pose = t.toEigen3f();
UASSERT(modelIter->second.fx()>0 && imageIter->second.rows>0 && imageIter->second.cols>0);
if(modelIter->second.imageHeight() <=0 || modelIter->second.imageWidth() <=0)
{
UERROR("Should have camera models with width/height set to create texture cameras!");
return pcl::texture_mapping::CameraVector();
}
UASSERT(modelIter->second.fx()>0 && modelIter->second.imageHeight()>0 && modelIter->second.imageWidth()>0);
cam.focal_length=modelIter->second.fx();
cam.height=imageIter->second.rows;
cam.width=imageIter->second.cols;
std::string fileName = uFormat("%s/%s%d.png", tmpDirectory.c_str(), "texture_", poseIter->first);
if(!cv::imwrite(fileName, imageIter->second))
{
UERROR("Cannot save texture of image %d", poseIter->first);
}
else
{
UINFO("Saved temporary texture: \"%s\"", fileName.c_str());
}
cam.texture_file = fileName;
cam.height=modelIter->second.imageHeight();
cam.width=modelIter->second.imageWidth();
cam.texture_file = uFormat("%d.png", poseIter->first);
cameras[oi++] = cam;
}
return cameras;
@@ -620,8 +610,6 @@ pcl::TextureMesh::Ptr createTextureMesh(
const pcl::PolygonMesh::Ptr & mesh,
const std::map<int, Transform> & poses,
const std::map<int, CameraModel> & cameraModels,
const std::map<int, cv::Mat> & images,
const std::string & tmpDirectory,
int kNormalSearch)
{
pcl::TextureMesh::Ptr textureMesh(new pcl::TextureMesh);
@@ -636,9 +624,7 @@ pcl::TextureMesh::Ptr createTextureMesh(
// create cameras
pcl::texture_mapping::CameraVector cameras = createTextureCameras(
poses,
cameraModels,
images,
tmpDirectory);
cameraModels);
// Create materials for each texture (and one extra for occluded faces)
textureMesh->tex_materials.resize (cameras.size () + 1);
@@ -671,17 +657,7 @@ pcl::TextureMesh::Ptr createTextureMesh(
}
else
{
mesh_material.tex_file = tmpDirectory+UDirectory::separator()+"occluded.png";
cv::Mat emptyImage;
if(i>0)
{
emptyImage = cv::Mat::ones(cameras[i-1].height,cameras[i-1].width, CV_8UC1)*255;
}
else
{
emptyImage = cv::Mat::ones(480, 640, CV_8UC1)*255;
}
cv::imwrite(mesh_material.tex_file, emptyImage);
mesh_material.tex_file = "occluded.png";
}
textureMesh->tex_materials[i] = mesh_material;