Skip to content

Commit

Permalink
Merge pull request #553 from Breakthru/kinfu_camera_pose
Browse files Browse the repository at this point in the history
pcl_kinfu_app saves camera pose to csv file.
  • Loading branch information
jspricke committed Mar 20, 2014
2 parents 42d6125 + a6238aa commit 2af739c
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 5 deletions.
105 changes: 105 additions & 0 deletions gpu/kinfu/tools/camera_pose.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2014-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Marco Paladini <marco.paladini@ocado.com>
* Date: March 2014
*/

#pragma once

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <fstream>

/**
* @brief The CameraPoseProcessor class is an interface to extract
* camera pose data generated by the pcl_kinfu_app program.
* Use the CameraPoseWriter implementation if writing the camera
* pose to a file is all you need, or provide an alternative implementation.
*/
class CameraPoseProcessor
{
public:
virtual ~CameraPoseProcessor () {}

/// process the camera pose, this method is called at every frame.
virtual void
processPose (const Eigen::Affine3f &pose)=0;
};

/**
* @brief CameraPoseWriter writes all camera poses computed by
* the KinfuTracker to a file on disk.
*
*/
class CameraPoseWriter : public CameraPoseProcessor
{
std::string output_filename_;
std::ofstream out_stream_;
public:
/**
* @param output_filename name of file to write
*/
CameraPoseWriter (std::string output_filename) :
output_filename_ (output_filename)
{
out_stream_.open (output_filename_.c_str () );
}

~CameraPoseWriter ()
{
if (out_stream_.is_open ())
{
out_stream_.close ();
std::cout << "wrote camera poses to file " << output_filename_ << std::endl;
}
}

void
processPose (const Eigen::Affine3f &pose)
{
if (out_stream_.good ())
{
// convert 3x4 affine transformation to quaternion and write to file
Eigen::Quaternionf q (pose.rotation ());
Eigen::Vector3f t (pose.translation ());
// write translation , quaternion in a row
out_stream_ << t[0] << "," << t[1] << "," << t[2]
<< "," << q.w () << "," << q.x ()
<< "," << q.y ()<< "," << q.z () << std::endl;
}
}

};
27 changes: 22 additions & 5 deletions gpu/kinfu/tools/kinfu_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
#include "tsdf_volume.h"
#include "tsdf_volume.hpp"

#include "camera_pose.h"

#ifdef HAVE_OPENCV
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
Expand Down Expand Up @@ -646,8 +648,8 @@ struct KinFuApp
{
enum { PCD_BIN = 1, PCD_ASCII = 2, PLY = 3, MESH_PLY = 7, MESH_VTK = 8 };

KinFuApp(pcl::Grabber& source, float vsz, int icp, int viz) : exit_ (false), scan_ (false), scan_mesh_(false), scan_volume_ (false), independent_camera_ (false),
registration_ (false), integrate_colors_ (false), focal_length_(-1.f), capture_ (source), scene_cloud_view_(viz), image_view_(viz), time_ms_(0), icp_(icp), viz_(viz)
KinFuApp(pcl::Grabber& source, float vsz, int icp, int viz, boost::shared_ptr<CameraPoseProcessor> pose_processor=boost::shared_ptr<CameraPoseProcessor> () ) : exit_ (false), scan_ (false), scan_mesh_(false), scan_volume_ (false), independent_camera_ (false),
registration_ (false), integrate_colors_ (false), focal_length_(-1.f), capture_ (source), scene_cloud_view_(viz), image_view_(viz), time_ms_(0), icp_(icp), viz_(viz), pose_processor_ (pose_processor)
{
//Init Kinfu Tracker
Eigen::Vector3f volume_size = Vector3f::Constant (vsz/*meters*/);
Expand All @@ -667,7 +669,6 @@ struct KinFuApp
if (!icp)
kinfu_.disableIcp();


//Init KinfuApp
tsdf_cloud_ptr_ = pcl::PointCloud<pcl::PointXYZI>::Ptr (new pcl::PointCloud<pcl::PointXYZI>);
image_view_.raycaster_ptr_ = RayCaster::Ptr( new RayCaster(kinfu_.rows (), kinfu_.cols ()) );
Expand Down Expand Up @@ -780,7 +781,13 @@ struct KinFuApp
else
has_image = kinfu_ (depth_device_);
}


// process camera pose
if (pose_processor_)
{
pose_processor_->processPose (kinfu_.getCameraPose ());
}

image_view_.showDepth (depth);
//image_view_.showGeneratedDepth(kinfu_, kinfu_.getCameraPose());
}
Expand Down Expand Up @@ -1055,6 +1062,8 @@ struct KinFuApp
int time_ms_;
int icp_, viz_;

boost::shared_ptr<CameraPoseProcessor> pose_processor_;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void
keyboard_callback (const visualization::KeyboardEvent &e, void *cookie)
Expand Down Expand Up @@ -1154,6 +1163,7 @@ print_cli_help ()
cout << " --scale-truncation, -st : scale the truncation distance and raycaster based on the volume size" << endl;
cout << " -volume_size <size_in_meters> : define integration volume size" << endl;
cout << " --depth-intrinsics <fx>,<fy>[,<cx>,<cy> : set the intrinsics of the depth camera" << endl;
cout << " -save_pose <pose_file.csv> : write tracked camera positions to the specified file" << endl;
cout << "Valid depth data sources:" << endl;
cout << " -dev <device> (default), -oni <oni_file>, -pcd <pcd_file or directory>" << endl;
cout << "";
Expand Down Expand Up @@ -1233,7 +1243,14 @@ main (int argc, char* argv[])
pc::parse_argument (argc, argv, "--icp", icp);
pc::parse_argument (argc, argv, "--viz", visualization);

KinFuApp app (*capture, volume_size, icp, visualization);
std::string camera_pose_file;
boost::shared_ptr<CameraPoseProcessor> pose_processor;
if (pc::parse_argument (argc, argv, "-save_pose", camera_pose_file) && camera_pose_file.size () > 0)
{
pose_processor.reset (new CameraPoseWriter (camera_pose_file));
}

KinFuApp app (*capture, volume_size, icp, visualization, pose_processor);

if (pc::parse_argument (argc, argv, "-eval", eval_folder) > 0)
app.toggleEvaluationMode(eval_folder, match_file);
Expand Down
94 changes: 94 additions & 0 deletions gpu/kinfu/tools/plot_camera_poses.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
% Copyright (c) 2014-, Open Perception, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above
% copyright notice, this list of conditions and the following
% disclaimer in the documentation and/or other materials provided
% with the distribution.
% * Neither the name of the copyright holder(s) nor the names of its
% contributors may be used to endorse or promote products derived
% from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
% COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
% Author: Marco Paladini <marco.paladini@ocado.com>

% sample octave script to load camera poses from file and plot them
% example usage: run 'pcl_kinfu_app -save_pose camera.csv' to save
% camera poses in a 'camera.csv' file
% run octave and cd into the directory where this script resides
% and call plot_camera_poses('<path-to-camera.csv-file>')

function plot_camera_poses(filename)
poses=load(filename);
%% show data on a 2D graph
h=figure();
plot(poses,'*-');
legend('x','y','z','qw','qx','qy','qz');

%% show data as 3D axis
h=figure();
for n=1:size(poses,1)
t=poses(n,1:3);
q=poses(n,4:7);
r=q2rot(q);
coord(h,r,t);
end
octave_axis_equal(h);

%% prevent Octave from quitting if called from the command line
input('Press enter to continue');
end

function coord(h,r,t)
figure(h);
hold on;
c={'r','g','b'};
p=0.1*[1 0 0;0 1 0;0 0 1];
for n=1:3
a=r*p(n,:)';
plot3([t(1),t(1)+a(1)], [t(2),t(2)+a(2)], [t(3),t(3)+a(3)], 'color', c{n});
end

function R=q2rot(q)
% conversion code from http://en.wikipedia.org/wiki/Rotation_matrix%Quaternion
Nq = q(1)^2 + q(2)^2 + q(3)^2 + q(4)^2;
if Nq>0; s=2/Nq; else s=0; end
X = q(2)*s; Y = q(3)*s; Z = q(4)*s;
wX = q(1)*X; wY = q(1)*Y; wZ = q(1)*Z;
xX = q(2)*X; xY = q(2)*Y; xZ = q(2)*Z;
yY = q(3)*Y; yZ = q(3)*Z; zZ = q(4)*Z;
R=[ 1.0-(yY+zZ) xY-wZ xZ+wY ;
xY+wZ 1.0-(xX+zZ) yZ-wX ;
xZ-wY yZ+wX 1.0-(xX+yY) ];
end

function octave_axis_equal(h)
% workaround for axis auto not working in 3d
% tanks http://octave.1599824.n4.nabble.com/axis-equal-help-tp1636701p1636702.html
figure(h);
xl = get (gca, "xlim");
yl = get (gca, "ylim");
zl = get (gca, "zlim");
span = max ([diff(xl), diff(yl), diff(zl)]);
xlim (mean (xl) + span*[-0.5, 0.5])
ylim (mean (yl) + span*[-0.5, 0.5])
zlim (mean (zl) + span*[-0.5, 0.5])
end

0 comments on commit 2af739c

Please sign in to comment.