Skip to content

Commit

Permalink
v2 projector calibration working fine
Browse files Browse the repository at this point in the history
  • Loading branch information
chparsons committed Apr 27, 2015
1 parent dcef9e6 commit 63207bd
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 136 deletions.
60 changes: 15 additions & 45 deletions src/cml/CalibrationParser.h
Expand Up @@ -19,22 +19,13 @@ namespace cml
OpticalDevice::Config& depth_cfg )
//OpticalDevice::Config& rgb_cfg )
{
//kinect:
//we can use kinect rgb calibration
//for depth camera bc we have registration
//which gives the depth map
//as if it were seen by the rgb camera
load_intrinsics( config.kinect_intrinsics_file, false, depth_cfg );
//set depth extrinsics
//to be the world origin
depth_cfg.extrinsics(
ofVec3f( 1.,0.,0. ),
ofVec3f( 0.,1.,0. ),
ofVec3f( 0.,0.,1. ),
ofVec3f( 0.,0.,0. ) );

//projector:
load_intrinsics( config.projector_intrinsics_file, false, proj_cfg );
depth_cfg.name("depth camera");
proj_cfg.name("projector");

//use kinect rgb calibration with registration turned on
load_intrinsics( config.kinect_intrinsics_file, true, depth_cfg );
load_intrinsics( config.projector_intrinsics_file, true, proj_cfg );

load_extrinsics( config.projector_kinect_extrinsics_file, proj_cfg );

};
Expand Down Expand Up @@ -84,19 +75,6 @@ namespace cml

device.size( imageSize.width, imageSize.height );
device.intrinsics(cx,cy,fx,fy);

ofLogNotice("cml::CalibrationParser")
<< "\n" << "intrinsics loaded"
<< "\n" << "filename: " << filename
<< "\n" << "image width: " << imageSize.width
<< "\n" << "image height: " << imageSize.height
<< "\n" << "principal point: " << principalPoint
<< "\n" << "cx: " << cx
<< "\n" << "cy: " << cy
<< "\n" << "fx: " << fx
<< "\n" << "fy: " << fy
<< "\n" << "camera matrix: \n" << cameraMatrix
<< "\n";
};

void load_extrinsics( string filename, OpticalDevice::Config& device )
Expand All @@ -115,6 +93,11 @@ namespace cml
if ( T.type() != CV_32FC1 )
T.convertTo( T, CV_32FC1 );

T *= 1000.0; //mts->mm units
//transpose to get proj->camera
T *= -1;
cv::transpose(R,R);

// opencv: row-major

// R x axis
Expand All @@ -138,22 +121,9 @@ namespace cml
ofVec3f Tvec = ofVec3f(
T.at<float>(0,0), //tx
T.at<float>(1,0), //ty
T.at<float>(2,0)); //tz

Tvec *= 100.0; //cm->mm scale

device.extrinsics( X, Y, Z, Tvec );

ofLogNotice("cml::CalibrationParser")
<< "\n" << "extrinsics loaded"
<< "\n" << "filename: " << filename
<< "\n" << "T: \n" << T
<< "\n" << "R: \n" << R
<< "\n" << "T vec: " << ofToString(Tvec)
<< "\n" << "R x axis: " << ofToString(X)
<< "\n" << "R y axis: " << ofToString(Y)
<< "\n" << "R z axis: " << ofToString(Z)
<< "\n";
T.at<float>(2,0)); //tz

device.extrinsics( X, Y, Z, Tvec );
};

};
Expand Down
3 changes: 3 additions & 0 deletions src/cml/CamaraLucida.cpp
Expand Up @@ -96,6 +96,9 @@ namespace cml
proj = new OpticalDevice( proj_cfg );
//rgb = new OpticalDevice( rgb_cfg );

depth->log(OF_LOG_NOTICE);
proj->log(OF_LOG_NOTICE);

_tex_width = config.tex_width;
_tex_height = config.tex_height;
_depth_width = depth->width;
Expand Down
2 changes: 1 addition & 1 deletion src/cml/Mesh.cpp
Expand Up @@ -46,7 +46,7 @@ namespace cml
to_depth( i, &xd, &yd, &idepth );

float zmm = depth_pix_mm[idepth];
zmm = CLAMP( ( zmm < epsilon ? 5000.0 : zmm ), 0.0, 5000.0 );
zmm = CLAMP( ( zmm < epsilon ? depth->far_clamp : zmm ), 0.0, depth->far_clamp );

float x, y;
depth->unproject( xd, yd, zmm, &x, &y );
Expand Down
1 change: 1 addition & 0 deletions src/cml/OpticalDevice.cpp
Expand Up @@ -10,6 +10,7 @@ namespace cml
height = config.height;
near = config.near;
far = config.far;
far_clamp = config.far_clamp;
cx = config.cx;
cy = config.cy;
fx = config.fx;
Expand Down
78 changes: 58 additions & 20 deletions src/cml/OpticalDevice.h
@@ -1,7 +1,6 @@
#pragma once

#include "ofVectorMath.h"
#include "ofLog.h"
#include "ofMain.h"

namespace cml
{
Expand All @@ -15,9 +14,16 @@ namespace cml

Config()
{
//mm
//mm units: near far
near = 200.0;
far = 6000.0;
far_clamp = 5000.0;

//init at world origin
X = ofVec3f( 1.,0.,0. );
Y = ofVec3f( 0.,1.,0. );
Z = ofVec3f( 0.,0.,1. );
T = ofVec3f( 0.,0.,0. );
};

void size( int w, int h )
Expand Down Expand Up @@ -46,13 +52,19 @@ namespace cml
Y.set( _Y );
Z.set( _Z );
T.set( _T );
};
};

float near, far;
float near, far, far_clamp;
int width, height;

float cx, cy, fx, fy;
ofVec3f X, Y, Z, T;

string _name;
void name(string n)
{ _name = n; };
string name()
{ return _name; };
};

struct Frustum
Expand Down Expand Up @@ -96,13 +108,45 @@ namespace cml
ofVec3f& up() { return _up; };
ofVec3f& trg() { return _trg; };

int width, height, near, far;
int width, height, near, far, far_clamp;
int cx, cy, fx, fy;

OpticalDevice::Config config;

void log( ofLogLevel level )
{
Frustum& F = _frustum;
ofLog(level)
<< "cml::OpticalDevice:" << "\n"
<< "name: " << config.name() << "\n"
<< "\n"
<< "frustum:" << "\n"
<< "L: " << F.left << "\n"
<< "R: " << F.right << "\n"
<< "T: " << F.top << "\n"
<< "B: " << F.bottom << "\n"
<< "N: " << F.near << "\n"
<< "F: " << F.far << "\n"
<< "\n"
<< "extrinsics:" << "\n"
<< "T: " << config.T << "\n"
<< "R x axis: " << ofToString(config.X)
<< "\n"
<< "R y axis: " << ofToString(config.Y)
<< "\n"
<< "R z axis: " << ofToString(config.Z)
<< "\n"
<< "\n";
};

private:

Frustum _frustum; //glFrustum( ... )
float _KK[16]; //glMultMatrixf( KK )
float _RT[16]; //glMultMatrixf( RT )

ofVec3f _loc, _fwd, _up, _trg;

/*
* Intrinsics from opencv to opengl
*
Expand All @@ -118,7 +162,7 @@ namespace cml

void make_frustum(
OpticalDevice::Config& _cfg,
OpticalDevice::Frustum& frs )
OpticalDevice::Frustum& F )
{

float w = _cfg.width;
Expand All @@ -130,14 +174,14 @@ namespace cml
float far = _cfg.far;
float near = _cfg.near;

frs.left = near * (-cx) / fx;
frs.right = near * (w - cx) / fx;
F.left = near * (-cx) / fx;
F.right = near * (w - cx) / fx;

frs.bottom = near * (cy - h) / fy;
frs.top = near * (cy) / fy;
F.bottom = near * (cy - h) / fy;
F.top = near * (cy) / fy;

frs.near = near;
frs.far = far;
F.near = near;
F.far = far;
};

/*
Expand Down Expand Up @@ -187,13 +231,7 @@ namespace cml
RT[1]= x.y; RT[5]= y.y; RT[9]= z.y; RT[13]= t.y;
RT[2]= x.z; RT[6]= y.z; RT[10]=z.z; RT[14]= t.z;
RT[3]= 0.; RT[7]= 0.; RT[11]= 0.; RT[15]= 1.;
};

Frustum _frustum; //glFrustum( ... )
float _KK[16]; //glMultMatrixf( KK )
float _RT[16]; //glMultMatrixf( RT )

ofVec3f _loc, _fwd, _up, _trg;
};
};
};

0 comments on commit 63207bd

Please sign in to comment.