Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

old changes to CalibrateProjector

  • Loading branch information...
commit cd6b58612310c3c5c09d4e70b8949b379018c451 1 parent 115f90b
@elliotwoods authored
View
79 CalibrateProjector/src/CorrelateXYZtoXY.cpp
@@ -13,12 +13,16 @@
CorrelateXYZtoXY::CorrelateXYZtoXY() {
cvParameters.insert(pair<string, bool>("CV_CALIB_USE_INTRINSIC_GUESS", true));
- cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_PRINCIPAL_POINT", true));
- cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_ASPECT_RATIO", true));
+ cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_PRINCIPAL_POINT", false));
+ cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_ASPECT_RATIO", false));
cvParameters.insert(pair<string, bool>("CV_CALIB_ZERO_TANGENT_DIST", true));
+ cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_K1", true));
+ cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_K2", true));
+ cvParameters.insert(pair<string, bool>("CV_CALIB_FIX_K3", true));
- projectorResolution.x = 1024;
- projectorResolution.y = 768;
+
+ projectorResolution.x = 1280;
+ projectorResolution.y = 800;
clear();
@@ -30,8 +34,8 @@ void CorrelateXYZtoXY::push(ofVec3f &xyz, ofVec2f &xy) {
return;
++count;
- this->xyz.push_back(xyz);
- this->xy.push_back(xy);
+ this->xyz.push_back(toCv(xyz));
+ this->xy.push_back(toCv(xy));
xyzPreview.addVertex(xyz);
}
@@ -42,6 +46,7 @@ void CorrelateXYZtoXY::clear() {
xyzPreview.clear();
count=0;
error=0;
+ ready=false;
}
int CorrelateXYZtoXY::size() {
@@ -70,35 +75,69 @@ void CorrelateXYZtoXY::correlate() {
flags |= cvParameters["CV_CALIB_FIX_PRINCIPAL_POINT"] & CV_CALIB_FIX_PRINCIPAL_POINT;
flags |= cvParameters["CV_CALIB_FIX_ASPECT_RATIO"] & CV_CALIB_FIX_ASPECT_RATIO;
flags |= cvParameters["CV_CALIB_ZERO_TANGENT_DIST"] & CV_CALIB_ZERO_TANGENT_DIST;
+ flags |= cvParameters["CV_CALIB_FIX_K1"] & CV_CALIB_FIX_K1;
+ flags |= cvParameters["CV_CALIB_FIX_K2"] & CV_CALIB_FIX_K2;
+ flags |= cvParameters["CV_CALIB_FIX_K3"] & CV_CALIB_FIX_K3;
+
//pull values into the 3d calibration
- vvo[0] = *(vector<Point3f >*)&xyz;
- vvi[0] = *(vector<Point2f >*)&xy;
+ //vvo[0] = *(vector<Point3f >*)&xyz;
+
+ for (int i=0; i<xy.size(); ++i) {
+ vvo[0] = xyz;
+ vvi[0] = xy;
+ }
//initialised throw ratio = 1.0f
- float f = projectorResolution.x;
+ float f = projectorResolution.y;
cameraMatrix = (Mat1d(3, 3) <<
- f, 0, f/2,
- 0, f, f/2,
+ f, 0, projectorResolution.x / 2.,
+ 0, f, projectorResolution.y / 2.,
0, 0, 1);
-
- error = calibrateCamera(vvo, vvi, cv::Size(projectorResolution.x, projectorResolution.y), cameraMatrix, distCoeffs, boardRotations, boardTranslations, flags);
+ try {
+ error = calibrateCamera(vvo, vvi, cv::Size(projectorResolution.x, projectorResolution.y), cameraMatrix, distCoeffs, boardRotations, boardTranslations, flags);
+ ready=true;
+ } catch (...) {
+ ofLogError() << "Calibration threw exception. Check parameters.";
+ }
+
+ for(int i = 0; i < boardRotations.size(); i++) {
+ cout << Mat(boardRotations[i]) << " " << boardTranslations[i] << endl;
+ }
+
+ Mat mt = (Mat1d(3,1) << 0, 0, 0);
+ Mat mr = (Mat1d(3,1) << 0, 0, 0);
+ vector<Point2f> projected;
+ projectPoints(Mat(xyz), mr, mt, cameraMatrix, distCoeffs, projected);
+ for(int i = 0; i < projected.size(); i++) {
+ cout << Mat(xyz[i]) << "->" << Mat(projected[i]) << endl;
+ }
+
+
+ intrinsics.setup(cameraMatrix, cv::Size(projectorResolution.x, projectorResolution.y));
cout << cameraMatrix << endl;
cout << distCoeffs << endl;
-
+ /*
+ ofVec2f calced;
+ for (int i=0; i<xyz.size(); i++) {
+ cout << ofToString(xy[i].x,0) << "," << ofToString(xy[i].y,0);
+ calced = project(xyz[i]);
+ cout << " -> ";
+ cout << ofToString(calced.x,0) << "," << ofToString(calced.y,0);
+ cout << endl;
+ }*/
}
-ofVec2f CorrelateXYZtoXY::project(const ofVec3f xyz) const {
- Mat1f mo(3, 1), mt(3, 1), mr(3, 1);
+ofVec2f CorrelateXYZtoXY::project(const Point3f o) const {
+ vector<cv::Point3f> vo(1, o);
- *(ofVec3f*)&mo(0,0) = xyz;
- *(ofVec3f*)&mt(0,0) = ofVec3f(0,0,0);
- *(ofVec3f*)&mr(0,0) = ofVec3f(0,0,0);
+ Mat mt = (Mat1d(3,1) << 0, 0, 0);
+ Mat mr = (Mat1d(3,1) << 0, 0, 0);
vector<Point2f> projected;
- projectPoints(mo, mr, mt, cameraMatrix, distCoeffs, projected);
+ projectPoints(Mat(vo), mr, mt, cameraMatrix, distCoeffs, projected);
return toOf(projected[0]);
}
View
12 CalibrateProjector/src/CorrelateXYZtoXY.h
@@ -17,6 +17,7 @@
using namespace cv;
using namespace ofxCv;
+
class CorrelateXYZtoXY {
public:
CorrelateXYZtoXY();
@@ -28,19 +29,20 @@ class CorrelateXYZtoXY {
void draw();
void correlate();
- ofVec2f project(const ofVec3f xyz) const;
+ ofVec2f project(const Point3f o) const;
ofVec2f projectorResolution;
- vector<ofVec3f> xyz;
- vector<ofVec2f> xy;
+ vector<Point3f> xyz;
+ vector<Point2f> xy;
unsigned int count;
float error;
Mat cameraMatrix, distCoeffs;
+ map<string, bool> cvParameters;
+ Intrinsics intrinsics;
+ bool ready;
protected:
ofMesh xyzPreview;
-
- map<string, bool> cvParameters;
};
View
100 CalibrateProjector/src/testApp.cpp
@@ -6,31 +6,44 @@ kinectView(kinect),
scrPreviewBoard("Chessboard preview", board),
scrPreviewRGB("RGB camera", kinect.getRGBTextureReference()),
scrKinectView("Kinect", kinectView),
+scrControls("Controls"),
+scrCVFlags("CV flags"),
wdgScale("Chessboard scale", board.scale, 0, 1.0f, 0.01f),
wdgWhiteBackground("White background", board.whiteBackground),
wdgBrightness("Brightness", board.brightness, 0, 1.0f, 0.01f),
wdgCapture("Capture"),
wdgCursor("World cursor", worldCursor, -3.0f, 3.0f, 0.01f, "m"),
-wdgError("Reprojection error", correlation.error, 0, 200, 0.01f, "pixels") {
- scrMain.push(scrControl);
+wdgError("Reprojection error", calibrate.error, 0, 200, 0.01f, "pixels"),
+wdgClear("Clear")
+{
+ scrMain.push(scrControls);
scrMain.push(scrPreviewBoard);
scrMain.push(scrPreviewRGB);
scrMain.push(scrKinectView);
- scrControl.push(wdgScale);
- scrControl.push(wdgWhiteBackground);
- scrControl.push(wdgBrightness);
- scrControl.push(wdgCapture);
- scrControl.push(wdgCursor);
- scrControl.push(wdgError);
- scrControl.push(new wdgCounter("Datapoints", correlation.count));
+ scrControls.push(scrCaptureControls);
+ scrControls.push(scrCVFlags);
+
+ scrCaptureControls.push(wdgScale);
+ scrCaptureControls.push(wdgWhiteBackground);
+ scrCaptureControls.push(wdgBrightness);
+ scrCaptureControls.push(wdgCapture);
+ scrCaptureControls.push(wdgCursor);
+ scrCaptureControls.push(wdgError);
+ scrCaptureControls.push(new wdgCounter("Datapoints", calibrate.count));
wdgError.enabled = false;
- scrControl.push(new wdgButton("Reproject chess", showMarkers));
-
- showMarkers = false;
+ scrCaptureControls.push(new wdgButton("Reproject chess", showMarkers));
+ scrCaptureControls.push(wdgClear);
+ scrCaptureControls.push(new wdgButton("Show past finds", showPastFinds));
+
+ map<string, bool>::iterator it;
+ for (it = calibrate.cvParameters.begin(); it != calibrate.cvParameters.end(); it++) {
+ scrCVFlags.push(new wdgButton(it->first, it->second));
+ }
wdgCapture.setHotKey(' ');
+ wdgClear.setHotKey('c');
}
//--------------------------------------------------------------
@@ -49,8 +62,12 @@ void testApp::setup(){
ofAddListener(scrPreviewRGB.evtDraw, this, &testApp::drawFoundCorners2D);
ofAddListener(scrKinectView.evtDraw3D, this, &testApp::drawFoundCorners3D);
ofAddListener(scrPreviewRGB.evtMousePressed, this, &testApp::pipetRGB);
+ ofAddListener(scrPreviewBoard.evtDraw, this, &testApp::drawProjection);
scrKinectView.getNodeReference().pan(180.0f);
+
+ showMarkers = true;
+ showPastFinds = false;
}
//--------------------------------------------------------------
@@ -64,21 +81,26 @@ void testApp::update(){
if (wdgCapture.getBang())
capture();
- if (showMarkers) {
- vector<ofVec3f>::iterator it;
+ if (wdgClear.getBang())
+ calibrate.clear();
+
+ if (showMarkers && calibrate.ready) {
+ vector<Point3f>::iterator it;
int iMarker = 0;
- for (it = foundCornersW.begin(); it != foundCornersW.end(); ++it) {
+
+ vector<Point3f> &xyzMarkers(showPastFinds ? calibrate.xyz : *(vector<Point3f>*)&foundCornersW);
+ for (it = xyzMarkers.begin(); it != xyzMarkers.end(); ++it) {
board.markers[iMarker].enabled = true;
- board.markers[iMarker].xy = correlation.project(*it);
- if (iMarker++ >= 10)
+ board.markers[iMarker].xy = calibrate.project(*it) / calibrate.projectorResolution;
+ if (++iMarker >= MAX_MARKERS)
break;
}
} else {
- for (int i=0; i<10; ++i) {
+ for (int i=0; i<MAX_MARKERS; ++i) {
board.markers[i].enabled = false;
}
}
-
+
if (wdgScale.isValueNew() || wdgWhiteBackground.isValueNew() || wdgBrightness.isValueNew() || showMarkers || ofGetFrameNum() % 10 == 0) {
TalkyMessage msg;
msg << board;
@@ -88,7 +110,6 @@ void testApp::update(){
//--------------------------------------------------------------
void testApp::draw(){
-
}
//--------------------------------------------------------------
@@ -153,11 +174,12 @@ void testApp::drawFoundCorners3D(ofNode &n){
//////////////
//
ofPushStyle();
- correlation.draw();
+ calibrate.draw();
ofPopStyle();
//
//////////////
+
//////////////
// Cursor
//////////////
@@ -177,12 +199,42 @@ void testApp::drawFoundCorners3D(ofNode &n){
}
//--------------------------------------------------------------
+void testApp::drawProjection(ofRectangle &r) {
+
+ ofPushStyle();
+ ofNoFill();
+
+ Point2f xyProjector, xyInView;
+ for (int i=0; i<calibrate.xy.size(); i++) {
+ xyProjector = calibrate.xy[i];
+ xyInView.x = r.x + r.width * xyProjector.x / calibrate.projectorResolution.x;
+ xyInView.y = r.y + r.height * xyProjector.y / calibrate.projectorResolution.y;
+ ofCircle(xyInView.x,xyInView.y , 10);
+ }
+
+ ofPopStyle();
+//
+// ofPushView();
+//
+// calibrate.intrinsics.loadProjectionMatrix(0.1);
+//
+// for (int i=0; i<foundCornersW.size(); i++) {
+// ofPushMatrix();
+// ofTranslate(foundCornersW[i]);
+// ofSphere(0.01);
+// ofPopMatrix();
+// }
+//
+// ofPopView();
+}
+
+//--------------------------------------------------------------
void testApp::capture() {
projectedCornersP = board.getProjectionSpaceCorners();
for (int i=0; i<foundCornersC.size(); ++i)
- if (foundCornersW[i].length() > 0.5f)
- correlation.push(foundCornersW[i], projectedCornersP[i]);
+ if (foundCornersW[i].length() > 0.1f)
+ calibrate.push(foundCornersW[i], projectedCornersP[i]);
- correlation.correlate();
+ calibrate.correlate();
}
View
13 CalibrateProjector/src/testApp.h
@@ -10,12 +10,12 @@
#include "OpenNI2ViewNode.h"
#include "CorrelateXYZtoXY.h"
-
class testApp : public ofBaseApp{
public:
testApp();
+
void setup();
void update();
void draw();
@@ -54,7 +54,9 @@ class testApp : public ofBaseApp{
scrDraw2D scrPreviewBoard;
scrDraw2D scrPreviewRGB;
scrDraw3D scrKinectView;
- scrWidgets scrControl;
+ scrGroupTabbed scrControls;
+ scrWidgets scrCaptureControls;
+ scrWidgets scrCVFlags;
wdgSlider wdgScale;
wdgButton wdgWhiteBackground;
@@ -62,6 +64,7 @@ class testApp : public ofBaseApp{
wdgButton wdgCapture;
wdgSlider wdgCursor;
wdgSlider wdgError;
+ wdgButton wdgClear;
//
//////////////////
@@ -73,6 +76,8 @@ class testApp : public ofBaseApp{
void drawFoundCorners2D(ofRectangle &r);
void drawFoundCorners3D(ofNode &n);
+ void drawProjection(ofRectangle &r);
+
Chessboard board;
vector<ofVec2f> projectedCornersP;
vector<ofVec2f> foundCornersC;
@@ -87,8 +92,10 @@ class testApp : public ofBaseApp{
//
void capture();
- CorrelateXYZtoXY correlation;
+ CorrelateXYZtoXY calibrate;
+
bool showMarkers;
+ bool showPastFinds;
//
//////////////////
};
View
3  RGBDPreview/RGBDPreview.xcodeproj/project.pbxproj
@@ -1408,6 +1408,7 @@
isa = PBXProject;
buildConfigurationList = E4B69B4D0A3A1720003C02F2 /* Build configuration list for PBXProject "RGBDPreview" */;
compatibilityVersion = "Xcode 2.4";
+ developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
@@ -1557,6 +1558,7 @@
"-D__MACOSX_CORE__",
"-lpthread",
);
+ SDKROOT = "$(DEVELOPER_SDK_DIR)/MacOSX10.6.sdk";
};
name = Debug;
};
@@ -1586,6 +1588,7 @@
"-D__MACOSX_CORE__",
"-lpthread",
);
+ SDKROOT = "$(DEVELOPER_SDK_DIR)/MacOSX10.6.sdk";
};
name = Release;
};
View
33 common src/render/Chessboard.cpp
@@ -59,28 +59,39 @@ void Chessboard::draw(float x, float y, float w, float h) {
ofPopStyle();
ofPushStyle();
+ ofEnableSmoothing();
ofSetLineWidth(3.0f);
ofSetColor(255,0,0);
- for (int i=0; i<10; ++i)
- ofCircle(markers[i].xy.x, markers[i].xy.y, 10);
+ ofNoFill();
+ for (int i=0; i<MAX_MARKERS; ++i) {
+ if (!markers[i].enabled || markers[i].xy.x < 0 || markers[i].xy.y < 0 || markers[i].xy.x > 1 || markers[i].xy.y > 1)
+ continue;
+ ofCircle(markers[i].xy.x * w + x , markers[i].xy.y * h + y, 10);
+ }
ofPopStyle();
}
vector<ofVec2f> Chessboard::getProjectionSpaceCorners() const {
- vector<ofVec2f> corners;
+ vector<ofVec2f> out;
- ofVec2f step = scale * ofVec2f(2.0f / float(squaresX),
- 2.0f / float(squaresY));
+ ofVec2f step = ofVec2f(2.0f / float(squaresX),
+ 2.0f / float(squaresY));
- ofVec2f inset = ofVec2f(1.0f - scale, 1.0f - scale);
+ ofVec2f inset = ofVec2f(-1.0f, 1.0f);
- for (int i=1; i<squaresX; ++i)
- for (int j=1; j<squaresY; ++j) {
- corners.push_back(inset + step * ofVec2f(i, j));
+ ofVec2f corner, xyPix;
+ for (int j=1; j<squaresY; ++j)
+ for (int i=1; i<squaresX; ++i) {
+ corner = inset + step * ofVec2f(i, -j);
+ corner *= scale;
+ xyPix.x = (corner.x + 1.0f) / 2.0f * 1280;
+ xyPix.y = (1.0f - corner.y) / 2.0f * 800;
+
+ out.push_back(xyPix);
}
-
- return corners;
+
+ return out;
}
bool Chessboard::findCorners(ofPixels &image, vector<ofVec2f> &points) const {
View
6 common src/render/Chessboard.h
@@ -13,6 +13,8 @@
#include "ofMain.h"
#include "ofxCv.h"
+#define MAX_MARKERS 512
+
using namespace cv;
using namespace ofxCv;
@@ -34,7 +36,7 @@ class Chessboard : public ofBaseDraws {
virtual void draw(float x,float y);
virtual void draw(float x,float y,float w, float h);
/**
- Actually the drawing is scale invariant, so these functions don't have their usual meaning.
+ Since the drawing is scale invariant, so these functions don't have their usual meaning.
*/
virtual float getHeight() { return squaresX; };
virtual float getWidth() { return squaresY; };
@@ -56,5 +58,5 @@ class Chessboard : public ofBaseDraws {
bool whiteBackground;
float brightness;
- BoardMarker markers[10];
+ BoardMarker markers[MAX_MARKERS];
};
Please sign in to comment.
Something went wrong with that request. Please try again.