Merge pull request #220 from Queuecumber/master

More camera calibration functions
This commit is contained in:
Peter Braden 2015-02-02 18:48:42 +01:00
commit 6aaea0978c
7 changed files with 264 additions and 1 deletions

View File

@ -15,6 +15,7 @@
, "src/BackgroundSubtractor.cc"
, "src/Constants.cc"
, "src/Calib3D.cc"
, "src/ImgProc.cc"
]
, 'libraries': [
'<!@(pkg-config --libs opencv)'

View File

@ -11,6 +11,7 @@ void Calib3D::Init(Handle<Object> target)
NODE_SET_METHOD(obj, "drawChessboardCorners", DrawChessboardCorners);
NODE_SET_METHOD(obj, "calibrateCamera", CalibrateCamera);
NODE_SET_METHOD(obj, "solvePnP", SolvePnP);
NODE_SET_METHOD(obj, "getOptimalNewCameraMatrix", GetOptimalNewCameraMatrix);
target->Set(NanNew("calib3d"), obj);
}
@ -183,7 +184,7 @@ NAN_METHOD(Calib3D::CalibrateCamera)
if (args[2]->IsArray()) {
Local<Object> v8sz = args[2]->ToObject();
imageSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue());
imageSize = cv::Size(v8sz->Get(1)->IntegerValue(), v8sz->Get(0)->IntegerValue());
} else {
JSTHROW_TYPE("Must pass pattern size");
}
@ -312,3 +313,63 @@ NAN_METHOD(Calib3D::SolvePnP)
NanReturnUndefined();
}
}
// cv::solvePnP
NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
{
NanEscapableScope();
try {
// Get the arguments
// Arg 0 is the original camera matrix
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
cv::Mat Kin = m0->mat;
// Arg 1 is the distortion coefficients
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
cv::Mat dist = m1->mat;
// Arg 2, the image size
cv::Size imageSize;
if (args[2]->IsArray()) {
Local<Object> v8sz = args[2]->ToObject();
imageSize = cv::Size(v8sz->Get(1)->IntegerValue(), v8sz->Get(0)->IntegerValue());
} else {
JSTHROW_TYPE("Must pass original image size");
}
// Arg 3 is the alpha free scaling parameter
double alpha = args[3]->ToNumber()->Value();
// Arg 4, the new image size
cv::Size newImageSize;
if (args[4]->IsArray()) {
Local<Object> v8sz = args[4]->ToObject();
newImageSize = cv::Size(v8sz->Get(1)->IntegerValue(), v8sz->Get(0)->IntegerValue());
} else {
JSTHROW_TYPE("Must pass new image size");
}
// Arg 5, valid ROI, skip for now
// Arg 6, center principal point, skip for now
// Get the optimal new camera matrix
cv::Mat Kout = cv::getOptimalNewCameraMatrix(Kin, dist, imageSize, alpha, newImageSize);
// Wrap the output K
Local<Object> KMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *KMatrix = ObjectWrap::Unwrap<Matrix>(KMatrixWrap);
KMatrix->mat = Kout;
// Return the new K matrix
NanReturnValue(KMatrixWrap);
} catch (cv::Exception &e) {
const char *err_msg = e.what();
NanThrowError(err_msg);
NanReturnUndefined();
}
}

View File

@ -16,6 +16,8 @@ public:
static NAN_METHOD(CalibrateCamera);
static NAN_METHOD(SolvePnP);
static NAN_METHOD(GetOptimalNewCameraMatrix);
};
#endif

View File

@ -4,6 +4,9 @@
#define CONST(C) \
obj->Set(NanNew<String>(#C), NanNew<Integer>(C));
#define CONST_ENUM(C) \
obj->Set(NanNew<String>(#C), NanNew<Integer>((int)(cv::C)));
void
Constants::Init(Handle<Object> target) {
Persistent<Object> inner;
@ -54,6 +57,12 @@ Constants::Init(Handle<Object> target) {
CONST(CV_64FC3);
CONST(CV_64FC4);
CONST_ENUM(INTER_NEAREST);
CONST_ENUM(INTER_LINEAR);
CONST_ENUM(INTER_AREA);
CONST_ENUM(INTER_CUBIC);
CONST_ENUM(INTER_LANCZOS4);
target->Set(NanNew("Constants"), obj);
}

169
src/ImgProc.cc Normal file
View File

@ -0,0 +1,169 @@
#include "ImgProc.h"
#include "Matrix.h"
void ImgProc::Init(Handle<Object> target)
{
Persistent<Object> inner;
Local<Object> obj = NanNew<Object>();
NanAssignPersistent(inner, obj);
NODE_SET_METHOD(obj, "undistort", Undistort);
NODE_SET_METHOD(obj, "initUndistortRectifyMap", InitUndistortRectifyMap);
NODE_SET_METHOD(obj, "remap", Remap);
target->Set(NanNew("imgproc"), obj);
}
// cv::undistort
NAN_METHOD(ImgProc::Undistort)
{
NanEscapableScope();
try {
// Get the arguments
// Arg 0 is the image
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
cv::Mat inputImage = m0->mat;
// Arg 1 is the camera matrix
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
cv::Mat K = m1->mat;
// Arg 2 is the distortion coefficents
Matrix* m2 = ObjectWrap::Unwrap<Matrix>(args[2]->ToObject());
cv::Mat dist = m2->mat;
// Make an mat to hold the result image
cv::Mat outputImage;
// Undistort
cv::undistort(inputImage, outputImage, K, dist);
// Wrap the output image
Local<Object> outMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *outMatrix = ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = outputImage;
// Return the output image
NanReturnValue(outMatrixWrap);
} catch (cv::Exception &e) {
const char *err_msg = e.what();
NanThrowError(err_msg);
NanReturnUndefined();
}
}
// cv::initUndistortRectifyMap
NAN_METHOD(ImgProc::InitUndistortRectifyMap)
{
NanEscapableScope();
try {
// Arg 0 is the camera matrix
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
cv::Mat K = m0->mat;
// Arg 1 is the distortion coefficents
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
cv::Mat dist = m1->mat;
// Arg 2 is the recification transformation
Matrix* m2 = ObjectWrap::Unwrap<Matrix>(args[2]->ToObject());
cv::Mat R = m2->mat;
// Arg 3 is the new camera matrix
Matrix* m3 = ObjectWrap::Unwrap<Matrix>(args[3]->ToObject());
cv::Mat newK = m3->mat;
// Arg 4 is the image size
cv::Size imageSize;
if (args[4]->IsArray()) {
Local<Object> v8sz = args[4]->ToObject();
imageSize = cv::Size(v8sz->Get(1)->IntegerValue(), v8sz->Get(0)->IntegerValue());
} else {
JSTHROW_TYPE("Must pass image size");
}
// Arg 5 is the first map type, skip for now
int m1type = args[5]->IntegerValue();
// Make matrices to hold the output maps
cv::Mat map1, map2;
// Compute the rectification map
cv::initUndistortRectifyMap(K, dist, R, newK, imageSize, m1type, map1, map2);
// Wrap the output maps
Local<Object> map1Wrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *map1Matrix = ObjectWrap::Unwrap<Matrix>(map1Wrap);
map1Matrix->mat = map1;
Local<Object> map2Wrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *map2Matrix = ObjectWrap::Unwrap<Matrix>(map2Wrap);
map2Matrix->mat = map2;
// Make a return object with the two maps
Local<Object> ret = NanNew<Object>();
ret->Set(NanNew<String>("map1"), map1Wrap);
ret->Set(NanNew<String>("map2"), map2Wrap);
// Return the maps
NanReturnValue(ret);
} catch (cv::Exception &e) {
const char *err_msg = e.what();
NanThrowError(err_msg);
NanReturnUndefined();
}
}
// cv::remap
NAN_METHOD(ImgProc::Remap)
{
NanEscapableScope();
try {
// Get the arguments
// Arg 0 is the image
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
cv::Mat inputImage = m0->mat;
// Arg 1 is the first map
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
cv::Mat map1 = m1->mat;
// Arg 2 is the second map
Matrix* m2 = ObjectWrap::Unwrap<Matrix>(args[2]->ToObject());
cv::Mat map2 = m2->mat;
// Arg 3 is the interpolation mode
int interpolation = args[3]->IntegerValue();
// Args 4, 5 border settings, skipping for now
// Output image
cv::Mat outputImage;
// Remap
cv::remap(inputImage, outputImage, map1, map2, interpolation);
// Wrap the output image
Local<Object> outMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *outMatrix = ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = outputImage;
// Return the image
NanReturnValue(outMatrixWrap);
} catch (cv::Exception &e) {
const char *err_msg = e.what();
NanThrowError(err_msg);
NanReturnUndefined();
}
}

19
src/ImgProc.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NODE_IMGPROC_H
#define __NODE_IMGPROC_H
#include "OpenCV.h"
// Implementation of imgproc.hpp functions
class ImgProc: public node::ObjectWrap {
public:
static void Init(Handle<Object> target);
static NAN_METHOD(Undistort);
static NAN_METHOD(InitUndistortRectifyMap);
static NAN_METHOD(Remap);
};
#endif

View File

@ -10,6 +10,7 @@
#include "FaceRecognizer.h"
#include "Constants.h"
#include "Calib3D.h"
#include "ImgProc.h"
extern "C" void
init(Handle<Object> target) {
@ -25,6 +26,7 @@ init(Handle<Object> target) {
NamedWindow::Init(target);
Constants::Init(target);
Calib3D::Init(target);
ImgProc::Init(target);
#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4