mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Merge remote-tracking branch 'origin/stereo'
Conflicts: binding.gyp
This commit is contained in:
commit
24a23b803b
@ -15,13 +15,13 @@
|
|||||||
"src/BackgroundSubtractor.cc",
|
"src/BackgroundSubtractor.cc",
|
||||||
"src/Constants.cc",
|
"src/Constants.cc",
|
||||||
"src/Calib3D.cc",
|
"src/Calib3D.cc",
|
||||||
"src/ImgProc.cc"
|
"src/ImgProc.cc",
|
||||||
|
"src/Stereo.cc"
|
||||||
],
|
],
|
||||||
|
|
||||||
"libraries": [
|
"libraries": [
|
||||||
"<!@(pkg-config --libs opencv)"
|
"<!@(pkg-config --libs opencv)"
|
||||||
],
|
],
|
||||||
|
|
||||||
# For windows
|
# For windows
|
||||||
|
|
||||||
"include_dirs": [
|
"include_dirs": [
|
||||||
|
|||||||
526
src/Calib3D.cc
526
src/Calib3D.cc
@ -1,6 +1,123 @@
|
|||||||
#include "Calib3D.h"
|
#include "Calib3D.h"
|
||||||
#include "Matrix.h"
|
#include "Matrix.h"
|
||||||
|
|
||||||
|
inline Local<Object> matrixFromMat(cv::Mat &input)
|
||||||
|
{
|
||||||
|
Local<Object> matrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
||||||
|
Matrix *matrix = ObjectWrap::Unwrap<Matrix>(matrixWrap);
|
||||||
|
matrix->mat = input;
|
||||||
|
|
||||||
|
return matrixWrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline cv::Mat matFromMatrix(Handle<Value> matrix)
|
||||||
|
{
|
||||||
|
Matrix* m = ObjectWrap::Unwrap<Matrix>(matrix->ToObject());
|
||||||
|
return m->mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline cv::Size sizeFromArray(Handle<Value> jsArray)
|
||||||
|
{
|
||||||
|
cv::Size patternSize;
|
||||||
|
|
||||||
|
if (jsArray->IsArray())
|
||||||
|
{
|
||||||
|
Local<Object> v8sz = jsArray->ToObject();
|
||||||
|
|
||||||
|
patternSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JSTHROW_TYPE("Size is not a valid array");
|
||||||
|
}
|
||||||
|
|
||||||
|
return patternSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::vector<cv::Point2f> points2fFromArray(Handle<Value> array)
|
||||||
|
{
|
||||||
|
std::vector<cv::Point2f> points;
|
||||||
|
if(array->IsArray())
|
||||||
|
{
|
||||||
|
Local<Array> pointsArray = Local<Array>::Cast(array->ToObject());
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < pointsArray->Length(); i++)
|
||||||
|
{
|
||||||
|
Local<Object> pt = pointsArray->Get(i)->ToObject();
|
||||||
|
points.push_back(cv::Point2f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
||||||
|
pt->Get(NanNew<String>("y"))->ToNumber()->Value()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JSTHROW_TYPE("Points not a valid array");
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::vector<cv::Point3f> points3fFromArray(Handle<Value> array)
|
||||||
|
{
|
||||||
|
std::vector<cv::Point3f> points;
|
||||||
|
if(array->IsArray()) {
|
||||||
|
Local<Array> pointsArray = Local<Array>::Cast(array->ToObject());
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < pointsArray->Length(); i++)
|
||||||
|
{
|
||||||
|
Local<Object> pt = pointsArray->Get(i)->ToObject();
|
||||||
|
points.push_back(cv::Point3f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
||||||
|
pt->Get(NanNew<String>("y"))->ToNumber()->Value(),
|
||||||
|
pt->Get(NanNew<String>("z"))->ToNumber()->Value()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JSTHROW_TYPE("Must pass array of object points for each frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::vector<std::vector<cv::Point2f> > points2fFromArrayOfArrays(Handle<Value> array)
|
||||||
|
{
|
||||||
|
std::vector<std::vector<cv::Point2f> > points;
|
||||||
|
if(array->IsArray())
|
||||||
|
{
|
||||||
|
Local<Array> pointsArray = Local<Array>::Cast(array->ToObject());
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < pointsArray->Length(); i++)
|
||||||
|
{
|
||||||
|
points.push_back(points2fFromArray(pointsArray->Get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JSTHROW_TYPE("Must pass array of object points for each frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::vector<std::vector<cv::Point3f> > points3fFromArrayOfArrays(Handle<Value> array)
|
||||||
|
{
|
||||||
|
std::vector<std::vector<cv::Point3f> > points;
|
||||||
|
if(array->IsArray())
|
||||||
|
{
|
||||||
|
Local<Array> pointsArray = Local<Array>::Cast(array->ToObject());
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < pointsArray->Length(); i++)
|
||||||
|
{
|
||||||
|
points.push_back(points3fFromArray(pointsArray->Get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JSTHROW_TYPE("Must pass array of object points for each frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
void Calib3D::Init(Handle<Object> target)
|
void Calib3D::Init(Handle<Object> target)
|
||||||
{
|
{
|
||||||
Persistent<Object> inner;
|
Persistent<Object> inner;
|
||||||
@ -12,6 +129,10 @@ void Calib3D::Init(Handle<Object> target)
|
|||||||
NODE_SET_METHOD(obj, "calibrateCamera", CalibrateCamera);
|
NODE_SET_METHOD(obj, "calibrateCamera", CalibrateCamera);
|
||||||
NODE_SET_METHOD(obj, "solvePnP", SolvePnP);
|
NODE_SET_METHOD(obj, "solvePnP", SolvePnP);
|
||||||
NODE_SET_METHOD(obj, "getOptimalNewCameraMatrix", GetOptimalNewCameraMatrix);
|
NODE_SET_METHOD(obj, "getOptimalNewCameraMatrix", GetOptimalNewCameraMatrix);
|
||||||
|
NODE_SET_METHOD(obj, "stereoCalibrate", StereoCalibrate);
|
||||||
|
NODE_SET_METHOD(obj, "stereoRectify", StereoRectify);
|
||||||
|
NODE_SET_METHOD(obj, "computeCorrespondEpilines", ComputeCorrespondEpilines);
|
||||||
|
NODE_SET_METHOD(obj, "reprojectImageTo3d", ReprojectImageTo3D);
|
||||||
|
|
||||||
target->Set(NanNew("calib3d"), obj);
|
target->Set(NanNew("calib3d"), obj);
|
||||||
}
|
}
|
||||||
@ -25,18 +146,10 @@ NAN_METHOD(Calib3D::FindChessboardCorners)
|
|||||||
// Get the arguments from javascript
|
// Get the arguments from javascript
|
||||||
|
|
||||||
// Arg 0 is the image
|
// Arg 0 is the image
|
||||||
Matrix* m = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
cv::Mat mat = matFromMatrix(args[0]);
|
||||||
cv::Mat mat = m->mat;
|
|
||||||
|
|
||||||
// Arg 1 is the pattern size
|
// Arg 1 is the pattern size
|
||||||
cv::Size patternSize;
|
cv::Size patternSize = sizeFromArray(args[1]);
|
||||||
if (args[1]->IsArray()) {
|
|
||||||
Local<Object> v8sz = args[1]->ToObject();
|
|
||||||
|
|
||||||
patternSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue());
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass pattern size");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 2 would normally be the flags, ignoring this for now and using the default flags
|
// Arg 2 would normally be the flags, ignoring this for now and using the default flags
|
||||||
|
|
||||||
@ -80,33 +193,13 @@ NAN_METHOD(Calib3D::DrawChessboardCorners)
|
|||||||
// Get the arguments
|
// Get the arguments
|
||||||
|
|
||||||
// Arg 0 is the image
|
// Arg 0 is the image
|
||||||
Matrix* m = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
cv::Mat mat = matFromMatrix(args[0]);
|
||||||
cv::Mat mat = m->mat;
|
|
||||||
|
|
||||||
// Arg 1 is the pattern size
|
// Arg 1 is the pattern size
|
||||||
cv::Size patternSize;
|
cv::Size patternSize = sizeFromArray(args[1]);
|
||||||
if (args[1]->IsArray()) {
|
|
||||||
Local<Object> v8sz = args[1]->ToObject();
|
|
||||||
|
|
||||||
patternSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue());
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass pattern size");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 2 is the corners array
|
// Arg 2 is the corners array
|
||||||
std::vector<cv::Point2f> corners;
|
std::vector<cv::Point2f> corners = points2fFromArray(args[2]);
|
||||||
if(args[2]->IsArray()) {
|
|
||||||
Local<Array> cornersArray = Local<Array>::Cast(args[2]);
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < cornersArray->Length(); i++)
|
|
||||||
{
|
|
||||||
Local<Object> pt = cornersArray->Get(i)->ToObject();
|
|
||||||
corners.push_back(cv::Point2f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("y"))->ToNumber()->Value()));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass corners array");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 3, pattern found boolean
|
// Arg 3, pattern found boolean
|
||||||
bool patternWasFound = args[3]->ToBoolean()->Value();
|
bool patternWasFound = args[3]->ToBoolean()->Value();
|
||||||
@ -133,61 +226,13 @@ NAN_METHOD(Calib3D::CalibrateCamera)
|
|||||||
// Get the arguments
|
// Get the arguments
|
||||||
|
|
||||||
// Arg 0, the array of object points, an array of arrays
|
// Arg 0, the array of object points, an array of arrays
|
||||||
std::vector<std::vector<cv::Point3f> > objectPoints;
|
std::vector<std::vector<cv::Point3f> > objectPoints = points3fFromArrayOfArrays(args[0]);
|
||||||
if(args[0]->IsArray()) {
|
|
||||||
Local<Array> objectPointsArray = Local<Array>::Cast(args[0]);
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < objectPointsArray->Length(); i++)
|
// Arg 1, the image points, another array of arrays
|
||||||
{
|
std::vector<std::vector<cv::Point2f> > imagePoints = points2fFromArrayOfArrays(args[1]);
|
||||||
std::vector<cv::Point3f> points;
|
|
||||||
|
|
||||||
Local<Array> pointsArray = Local<Array>::Cast(objectPointsArray->Get(i));
|
|
||||||
for(unsigned int j = 0; j < pointsArray->Length(); j++)
|
|
||||||
{
|
|
||||||
Local<Object> pt = pointsArray->Get(j)->ToObject();
|
|
||||||
points.push_back(cv::Point3f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("y"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("z"))->ToNumber()->Value()));
|
|
||||||
}
|
|
||||||
|
|
||||||
objectPoints.push_back(points);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass array of object points for each frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 1, the image points, another array of arrays =(
|
|
||||||
std::vector<std::vector<cv::Point2f> > imagePoints;
|
|
||||||
if(args[1]->IsArray()) {
|
|
||||||
Local<Array> imagePointsArray = Local<Array>::Cast(args[1]);
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < imagePointsArray->Length(); i++)
|
|
||||||
{
|
|
||||||
std::vector<cv::Point2f> points;
|
|
||||||
|
|
||||||
Local<Array> pointsArray = Local<Array>::Cast(imagePointsArray->Get(i));
|
|
||||||
for(unsigned int j = 0; j < pointsArray->Length(); j++)
|
|
||||||
{
|
|
||||||
Local<Object> pt = pointsArray->Get(j)->ToObject();
|
|
||||||
points.push_back(cv::Point2f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("y"))->ToNumber()->Value()));
|
|
||||||
}
|
|
||||||
|
|
||||||
imagePoints.push_back(points);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass array of object points for each frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 2, the image size
|
// Arg 2, the image size
|
||||||
cv::Size imageSize;
|
cv::Size imageSize = sizeFromArray(args[2]);
|
||||||
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 pattern size");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 3, 4, input guesses for the camrea matrix and distortion coefficients, skipping for now
|
// Arg 3, 4, input guesses for the camrea matrix and distortion coefficients, skipping for now
|
||||||
cv::Mat K, dist;
|
cv::Mat K, dist;
|
||||||
@ -206,17 +251,11 @@ NAN_METHOD(Calib3D::CalibrateCamera)
|
|||||||
ret->Set(NanNew<String>("reprojectionError"), NanNew<Number>(error));
|
ret->Set(NanNew<String>("reprojectionError"), NanNew<Number>(error));
|
||||||
|
|
||||||
// K
|
// K
|
||||||
Local<Object> KMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
Local<Object> KMatrixWrap = matrixFromMat(K);
|
||||||
Matrix *KMatrix = ObjectWrap::Unwrap<Matrix>(KMatrixWrap);
|
|
||||||
KMatrix->mat = K;
|
|
||||||
|
|
||||||
ret->Set(NanNew<String>("K"), KMatrixWrap);
|
ret->Set(NanNew<String>("K"), KMatrixWrap);
|
||||||
|
|
||||||
// dist
|
// dist
|
||||||
Local<Object> distMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
Local<Object> distMatrixWrap = matrixFromMat(dist);
|
||||||
Matrix *distMatrix = ObjectWrap::Unwrap<Matrix>(distMatrixWrap);
|
|
||||||
distMatrix->mat = dist;
|
|
||||||
|
|
||||||
ret->Set(NanNew<String>("distortion"), distMatrixWrap);
|
ret->Set(NanNew<String>("distortion"), distMatrixWrap);
|
||||||
|
|
||||||
// Per frame R and t, skiping for now
|
// Per frame R and t, skiping for now
|
||||||
@ -240,43 +279,16 @@ NAN_METHOD(Calib3D::SolvePnP)
|
|||||||
// Get the arguments
|
// Get the arguments
|
||||||
|
|
||||||
// Arg 0, the array of object points
|
// Arg 0, the array of object points
|
||||||
std::vector<cv::Point3f> objectPoints;
|
std::vector<cv::Point3f> objectPoints = points3fFromArray(args[0]);
|
||||||
if(args[0]->IsArray()) {
|
|
||||||
Local<Array> objectPointsArray = Local<Array>::Cast(args[0]);
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < objectPointsArray->Length(); i++)
|
|
||||||
{
|
|
||||||
Local<Object> pt = objectPointsArray->Get(i)->ToObject();
|
|
||||||
objectPoints.push_back(cv::Point3f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("y"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("z"))->ToNumber()->Value()));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass array of object points for each frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 1, the image points
|
// Arg 1, the image points
|
||||||
std::vector<cv::Point2f> imagePoints;
|
std::vector<cv::Point2f> imagePoints = points2fFromArray(args[1]);
|
||||||
if(args[1]->IsArray()) {
|
|
||||||
Local<Array> imagePointsArray = Local<Array>::Cast(args[1]);
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < imagePointsArray->Length(); i++)
|
|
||||||
{
|
|
||||||
Local<Object> pt = imagePointsArray->Get(i)->ToObject();
|
|
||||||
imagePoints.push_back(cv::Point2f(pt->Get(NanNew<String>("x"))->ToNumber()->Value(),
|
|
||||||
pt->Get(NanNew<String>("y"))->ToNumber()->Value()));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSTHROW_TYPE("Must pass array of object points for each frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arg 2, the camera matrix
|
// Arg 2, the camera matrix
|
||||||
Matrix* kWrap = ObjectWrap::Unwrap<Matrix>(args[2]->ToObject());
|
cv::Mat K = matFromMatrix(args[2]);
|
||||||
cv::Mat K = kWrap->mat;
|
|
||||||
|
|
||||||
// Arg 3, the distortion coefficients
|
// Arg 3, the distortion coefficients
|
||||||
Matrix* distWrap = ObjectWrap::Unwrap<Matrix>(args[3]->ToObject());
|
cv::Mat dist = matFromMatrix(args[3]);
|
||||||
cv::Mat dist = distWrap->mat;
|
|
||||||
|
|
||||||
// Arg 4, use extrinsic guess, skipped for now
|
// Arg 4, use extrinsic guess, skipped for now
|
||||||
|
|
||||||
@ -291,17 +303,11 @@ NAN_METHOD(Calib3D::SolvePnP)
|
|||||||
Local<Object> ret = NanNew<Object>();
|
Local<Object> ret = NanNew<Object>();
|
||||||
|
|
||||||
// rvec
|
// rvec
|
||||||
Local<Object> rMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
Local<Object> rMatrixWrap = matrixFromMat(rvec);
|
||||||
Matrix *rMatrix = ObjectWrap::Unwrap<Matrix>(rMatrixWrap);
|
|
||||||
rMatrix->mat = rvec;
|
|
||||||
|
|
||||||
ret->Set(NanNew<String>("rvec"), rMatrixWrap);
|
ret->Set(NanNew<String>("rvec"), rMatrixWrap);
|
||||||
|
|
||||||
// tvec
|
// tvec
|
||||||
Local<Object> tMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
Local<Object> tMatrixWrap = matrixFromMat(tvec);
|
||||||
Matrix *tMatrix = ObjectWrap::Unwrap<Matrix>(tMatrixWrap);
|
|
||||||
tMatrix->mat = tvec;
|
|
||||||
|
|
||||||
ret->Set(NanNew<String>("tvec"), tMatrixWrap);
|
ret->Set(NanNew<String>("tvec"), tMatrixWrap);
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
@ -314,7 +320,7 @@ NAN_METHOD(Calib3D::SolvePnP)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cv::solvePnP
|
// cv::getOptimalNewCameraMAtrix
|
||||||
NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
|
NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
|
||||||
{
|
{
|
||||||
NanEscapableScope();
|
NanEscapableScope();
|
||||||
@ -323,35 +329,19 @@ NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
|
|||||||
// Get the arguments
|
// Get the arguments
|
||||||
|
|
||||||
// Arg 0 is the original camera matrix
|
// Arg 0 is the original camera matrix
|
||||||
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
cv::Mat Kin = matFromMatrix(args[0]);
|
||||||
cv::Mat Kin = m0->mat;
|
|
||||||
|
|
||||||
// Arg 1 is the distortion coefficients
|
// Arg 1 is the distortion coefficients
|
||||||
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
|
cv::Mat dist = matFromMatrix(args[1]);
|
||||||
cv::Mat dist = m1->mat;
|
|
||||||
|
|
||||||
// Arg 2, the image size
|
// Arg 2, the image size
|
||||||
cv::Size imageSize;
|
cv::Size imageSize = sizeFromArray(args[2]);
|
||||||
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
|
// Arg 3 is the alpha free scaling parameter
|
||||||
double alpha = args[3]->ToNumber()->Value();
|
double alpha = args[3]->ToNumber()->Value();
|
||||||
|
|
||||||
// Arg 4, the new image size
|
// Arg 4, the new image size
|
||||||
cv::Size newImageSize;
|
cv::Size newImageSize = sizeFromArray(args[4]);
|
||||||
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 5, valid ROI, skip for now
|
||||||
// Arg 6, center principal point, skip for now
|
// Arg 6, center principal point, skip for now
|
||||||
@ -360,9 +350,7 @@ NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
|
|||||||
cv::Mat Kout = cv::getOptimalNewCameraMatrix(Kin, dist, imageSize, alpha, newImageSize);
|
cv::Mat Kout = cv::getOptimalNewCameraMatrix(Kin, dist, imageSize, alpha, newImageSize);
|
||||||
|
|
||||||
// Wrap the output K
|
// Wrap the output K
|
||||||
Local<Object> KMatrixWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
Local<Object> KMatrixWrap = matrixFromMat(Kout);
|
||||||
Matrix *KMatrix = ObjectWrap::Unwrap<Matrix>(KMatrixWrap);
|
|
||||||
KMatrix->mat = Kout;
|
|
||||||
|
|
||||||
// Return the new K matrix
|
// Return the new K matrix
|
||||||
NanReturnValue(KMatrixWrap);
|
NanReturnValue(KMatrixWrap);
|
||||||
@ -373,3 +361,229 @@ NAN_METHOD(Calib3D::GetOptimalNewCameraMatrix)
|
|||||||
NanReturnUndefined();
|
NanReturnUndefined();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cv::stereoCalibrate
|
||||||
|
NAN_METHOD(Calib3D::StereoCalibrate)
|
||||||
|
{
|
||||||
|
NanEscapableScope();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg 0, the array of object points, an array of arrays
|
||||||
|
std::vector<std::vector<cv::Point3f> > objectPoints = points3fFromArrayOfArrays(args[0]);
|
||||||
|
|
||||||
|
// Arg 1, the image points1, another array of arrays
|
||||||
|
std::vector<std::vector<cv::Point2f> > imagePoints1 = points2fFromArrayOfArrays(args[1]);
|
||||||
|
|
||||||
|
// Arg 2, the image points2, another array of arrays =(
|
||||||
|
std::vector<std::vector<cv::Point2f> > imagePoints2 = points2fFromArrayOfArrays(args[2]);
|
||||||
|
|
||||||
|
// Arg 3 is the image size (follows the PYTHON api not the C++ api since all following arguments are optional or outputs)
|
||||||
|
cv::Size imageSize = sizeFromArray(args[3]);
|
||||||
|
|
||||||
|
// Arg 4,5,6,7 is the camera matrix and distortion coefficients (optional but must pass all 4 or none)
|
||||||
|
cv::Mat k1, d1, k2, d2;
|
||||||
|
if(args.Length() >= 8)
|
||||||
|
{
|
||||||
|
k1 = matFromMatrix(args[4]);
|
||||||
|
d1 = matFromMatrix(args[5]);
|
||||||
|
|
||||||
|
k2 = matFromMatrix(args[6]);
|
||||||
|
d2 = matFromMatrix(args[7]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last argument is flags, skipping for now
|
||||||
|
|
||||||
|
// Output mats
|
||||||
|
cv::Mat R, t, E, F;
|
||||||
|
|
||||||
|
// Do the stereo calibration
|
||||||
|
cv::stereoCalibrate(objectPoints, imagePoints1, imagePoints2, k1, d1, k2, d2, imageSize, R, t, E, F);
|
||||||
|
|
||||||
|
// make the return value
|
||||||
|
Local<Object> ret = NanNew<Object>();
|
||||||
|
|
||||||
|
// Make the output arguments
|
||||||
|
|
||||||
|
// k1
|
||||||
|
Local<Object> K1MatrixWrap = matrixFromMat(k1);
|
||||||
|
|
||||||
|
// d1
|
||||||
|
Local<Object> d1MatrixWrap = matrixFromMat(d1);
|
||||||
|
|
||||||
|
// k2
|
||||||
|
Local<Object> K2MatrixWrap = matrixFromMat(k2);
|
||||||
|
|
||||||
|
// d2
|
||||||
|
Local<Object> d2MatrixWrap = matrixFromMat(d2);
|
||||||
|
|
||||||
|
// R
|
||||||
|
Local<Object> RMatrixWrap = matrixFromMat(R);
|
||||||
|
|
||||||
|
// t
|
||||||
|
Local<Object> tMatrixWrap = matrixFromMat(t);
|
||||||
|
|
||||||
|
// E
|
||||||
|
Local<Object> EMatrixWrap = matrixFromMat(E);
|
||||||
|
|
||||||
|
// F
|
||||||
|
Local<Object> FMatrixWrap = matrixFromMat(F);
|
||||||
|
|
||||||
|
// Add to return object
|
||||||
|
ret->Set(NanNew<String>("K1"), K1MatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("distortion1"), d1MatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("K2"), K2MatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("distortion2"), d2MatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("R"), RMatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("t"), tMatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("E"), EMatrixWrap);
|
||||||
|
ret->Set(NanNew<String>("F"), FMatrixWrap);
|
||||||
|
|
||||||
|
// Return
|
||||||
|
NanReturnValue(ret);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cv::stereoRectify
|
||||||
|
NAN_METHOD(Calib3D::StereoRectify)
|
||||||
|
{
|
||||||
|
NanEscapableScope();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg0, the first camera matrix
|
||||||
|
cv::Mat K1 = matFromMatrix(args[0]);
|
||||||
|
|
||||||
|
// Arg1, the first distortion coefficients
|
||||||
|
cv::Mat d1 = matFromMatrix(args[1]);
|
||||||
|
|
||||||
|
// Arg2, the second camera matrix
|
||||||
|
cv::Mat K2 = matFromMatrix(args[2]);
|
||||||
|
|
||||||
|
// Arg3, the second distortion coefficients
|
||||||
|
cv::Mat d2 = matFromMatrix(args[3]);
|
||||||
|
|
||||||
|
// Arg4, the image size
|
||||||
|
cv::Size imageSize = sizeFromArray(args[4]);
|
||||||
|
|
||||||
|
// arg5, the intercamera rotation matrix
|
||||||
|
cv::Mat R = matFromMatrix(args[5]);
|
||||||
|
|
||||||
|
// Arg6, the intercamera translation vector
|
||||||
|
cv::Mat t = matFromMatrix(args[6]);
|
||||||
|
|
||||||
|
// Arg8, flags, skipping for now
|
||||||
|
|
||||||
|
// Arg9, freescaling paremeter, skipping for now
|
||||||
|
|
||||||
|
// Arg10, new image size, skipping for now to fix at original image size
|
||||||
|
|
||||||
|
// Make output matrics
|
||||||
|
cv::Mat R1, R2, P1, P2, Q;
|
||||||
|
|
||||||
|
// Do the stereo rectification
|
||||||
|
cv::stereoRectify(K1, d1, K2, d2, imageSize, R, t, R1, R2, P1, P2, Q);
|
||||||
|
|
||||||
|
// Make the return object
|
||||||
|
Local<Object> ret = NanNew<Object>();
|
||||||
|
|
||||||
|
ret->Set(NanNew<String>("R1"), matrixFromMat(R1));
|
||||||
|
ret->Set(NanNew<String>("R2"), matrixFromMat(R2));
|
||||||
|
ret->Set(NanNew<String>("P1"), matrixFromMat(P1));
|
||||||
|
ret->Set(NanNew<String>("P2"), matrixFromMat(P2));
|
||||||
|
ret->Set(NanNew<String>("Q"), matrixFromMat(Q));
|
||||||
|
|
||||||
|
// Return the recification parameters
|
||||||
|
NanReturnValue(ret);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cv::computeCorrespondEpilines
|
||||||
|
NAN_METHOD(Calib3D::ComputeCorrespondEpilines)
|
||||||
|
{
|
||||||
|
NanEscapableScope();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg0, the image points
|
||||||
|
std::vector<cv::Point2f> points = points2fFromArray(args[0]);
|
||||||
|
|
||||||
|
// Arg1, the image index (1 or 2)
|
||||||
|
int whichImage = int(args[1]->ToNumber()->Value());
|
||||||
|
|
||||||
|
// Arg2, the fundamental matrix
|
||||||
|
cv::Mat F = matFromMatrix(args[2]);
|
||||||
|
|
||||||
|
// compute the lines
|
||||||
|
std::vector<cv::Vec3f> lines;
|
||||||
|
cv::computeCorrespondEpilines(points, whichImage, F, lines);
|
||||||
|
|
||||||
|
// Convert the lines to an array of objects (ax + by + c = 0)
|
||||||
|
Local<Array> linesArray = NanNew<Array>(lines.size());
|
||||||
|
for(unsigned int i = 0; i < lines.size(); i++)
|
||||||
|
{
|
||||||
|
Local<Object> line_data = NanNew<Object>();
|
||||||
|
line_data->Set(NanNew<String>("a"), NanNew<Number>(lines[i][0]));
|
||||||
|
line_data->Set(NanNew<String>("b"), NanNew<Number>(lines[i][1]));
|
||||||
|
line_data->Set(NanNew<String>("c"), NanNew<Number>(lines[i][2]));
|
||||||
|
|
||||||
|
linesArray->Set(NanNew<Number>(i), line_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the lines
|
||||||
|
NanReturnValue(linesArray);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cv::reprojectImageTo3D
|
||||||
|
NAN_METHOD(Calib3D::ReprojectImageTo3D)
|
||||||
|
{
|
||||||
|
NanEscapableScope();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg0, the disparity image
|
||||||
|
cv::Mat disparity = matFromMatrix(args[0]);
|
||||||
|
|
||||||
|
// Arg1, the depth-to-disparity transformation Q
|
||||||
|
cv::Mat Q = matFromMatrix(args[1]);
|
||||||
|
|
||||||
|
// Arg 2, handle missing values, skipped for now
|
||||||
|
|
||||||
|
// Arg3, output bit depth, skipped for now
|
||||||
|
|
||||||
|
// Compute the depth image
|
||||||
|
cv::Mat depthImage;
|
||||||
|
cv::reprojectImageTo3D(disparity, depthImage, Q);
|
||||||
|
|
||||||
|
// Wrap the depth image
|
||||||
|
Local<Object> depthImageMatrix = matrixFromMat(depthImage);
|
||||||
|
|
||||||
|
NanReturnValue(depthImageMatrix);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -18,6 +18,14 @@ public:
|
|||||||
static NAN_METHOD(SolvePnP);
|
static NAN_METHOD(SolvePnP);
|
||||||
|
|
||||||
static NAN_METHOD(GetOptimalNewCameraMatrix);
|
static NAN_METHOD(GetOptimalNewCameraMatrix);
|
||||||
|
|
||||||
|
static NAN_METHOD(StereoCalibrate);
|
||||||
|
|
||||||
|
static NAN_METHOD(StereoRectify);
|
||||||
|
|
||||||
|
static NAN_METHOD(ComputeCorrespondEpilines);
|
||||||
|
|
||||||
|
static NAN_METHOD(ReprojectImageTo3D);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
323
src/Stereo.cc
Normal file
323
src/Stereo.cc
Normal file
@ -0,0 +1,323 @@
|
|||||||
|
#include "Stereo.h"
|
||||||
|
#include "Matrix.h"
|
||||||
|
#include <opencv2/legacy/legacy.hpp>
|
||||||
|
|
||||||
|
// Block matching
|
||||||
|
|
||||||
|
v8::Persistent<FunctionTemplate> StereoBM::constructor;
|
||||||
|
|
||||||
|
void
|
||||||
|
StereoBM::Init(Handle<Object> target) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(StereoBM::New);
|
||||||
|
NanAssignPersistent(constructor, ctor);
|
||||||
|
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
ctor->SetClassName(NanNew("StereoBM"));
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(ctor, "compute", Compute);
|
||||||
|
|
||||||
|
ctor->Set(NanNew<String>("BASIC_PRESET"), NanNew<Integer>((int)cv::StereoBM::BASIC_PRESET));
|
||||||
|
ctor->Set(NanNew<String>("FISH_EYE_PRESET"), NanNew<Integer>((int)cv::StereoBM::FISH_EYE_PRESET));
|
||||||
|
ctor->Set(NanNew<String>("NARROW_PRESET"), NanNew<Integer>((int)cv::StereoBM::NARROW_PRESET));
|
||||||
|
|
||||||
|
target->Set(NanNew("StereoBM"), ctor->GetFunction());
|
||||||
|
}
|
||||||
|
|
||||||
|
NAN_METHOD(StereoBM::New) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
if (args.This()->InternalFieldCount() == 0)
|
||||||
|
NanThrowTypeError("Cannot instantiate without new");
|
||||||
|
|
||||||
|
StereoBM *stereo;
|
||||||
|
|
||||||
|
if (args.Length() == 0)
|
||||||
|
{
|
||||||
|
stereo = new StereoBM();
|
||||||
|
}
|
||||||
|
else if (args.Length() == 1)
|
||||||
|
{
|
||||||
|
stereo = new StereoBM(args[0]->IntegerValue()); // preset
|
||||||
|
}
|
||||||
|
else if (args.Length() == 2)
|
||||||
|
{
|
||||||
|
stereo = new StereoBM(args[0]->IntegerValue(), args[1]->IntegerValue()); // preset, disparity search range
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stereo = new StereoBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue()); // preset, disparity search range, sum of absolute differences window size
|
||||||
|
}
|
||||||
|
|
||||||
|
stereo->Wrap(args.Holder());
|
||||||
|
NanReturnValue(args.Holder());
|
||||||
|
}
|
||||||
|
|
||||||
|
StereoBM::StereoBM(int preset, int ndisparities, int SADWindowSize)
|
||||||
|
: ObjectWrap(), stereo(preset, ndisparities, SADWindowSize)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO make this async
|
||||||
|
NAN_METHOD(StereoBM::Compute)
|
||||||
|
{
|
||||||
|
SETUP_FUNCTION(StereoBM)
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg 0, the 'left' image
|
||||||
|
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
||||||
|
cv::Mat left = m0->mat;
|
||||||
|
|
||||||
|
// Arg 1, the 'right' image
|
||||||
|
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
|
||||||
|
cv::Mat right = m1->mat;
|
||||||
|
|
||||||
|
// Optional 3rd arg, the disparty depth
|
||||||
|
int type = CV_16S;
|
||||||
|
if(args.Length() > 2)
|
||||||
|
{
|
||||||
|
type = args[2]->IntegerValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute stereo using the block matching algorithm
|
||||||
|
cv::Mat disparity;
|
||||||
|
self->stereo(left, right, disparity, type);
|
||||||
|
|
||||||
|
// Wrap the returned disparity map
|
||||||
|
Local<Object> disparityWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
||||||
|
Matrix *disp = ObjectWrap::Unwrap<Matrix>(disparityWrap);
|
||||||
|
disp->mat = disparity;
|
||||||
|
|
||||||
|
NanReturnValue(disparityWrap);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// Semi-Global Block matching
|
||||||
|
|
||||||
|
v8::Persistent<FunctionTemplate> StereoSGBM::constructor;
|
||||||
|
|
||||||
|
void
|
||||||
|
StereoSGBM::Init(Handle<Object> target) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(StereoSGBM::New);
|
||||||
|
NanAssignPersistent(constructor, ctor);
|
||||||
|
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
ctor->SetClassName(NanNew("StereoSGBM"));
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(ctor, "compute", Compute);
|
||||||
|
|
||||||
|
target->Set(NanNew("StereoSGBM"), ctor->GetFunction());
|
||||||
|
}
|
||||||
|
|
||||||
|
NAN_METHOD(StereoSGBM::New) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
if (args.This()->InternalFieldCount() == 0)
|
||||||
|
NanThrowTypeError("Cannot instantiate without new");
|
||||||
|
|
||||||
|
StereoSGBM *stereo;
|
||||||
|
|
||||||
|
if (args.Length() == 0)
|
||||||
|
{
|
||||||
|
stereo = new StereoSGBM();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If passing arguments, must pass the first 3 at least
|
||||||
|
if (args.Length() >= 3)
|
||||||
|
{
|
||||||
|
switch (args.Length())
|
||||||
|
{
|
||||||
|
case 3:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 7:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue(), args[6]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 8:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue(), args[6]->IntegerValue(), args[7]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 9:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue(), args[6]->IntegerValue(), args[7]->IntegerValue(), args[8]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 10:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue(), args[6]->IntegerValue(), args[7]->IntegerValue(), args[8]->IntegerValue(), args[9]->IntegerValue());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
stereo = new StereoSGBM(args[0]->IntegerValue(), args[1]->IntegerValue(), args[2]->IntegerValue(), args[3]->IntegerValue(), args[4]->IntegerValue(), args[5]->IntegerValue(), args[6]->IntegerValue(), args[7]->IntegerValue(), args[8]->IntegerValue(), args[9]->IntegerValue(), args[10]->ToBoolean()->Value());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NanThrowError("If overriding default settings, must pass minDisparity, numDisparities, and SADWindowSize");
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stereo->Wrap(args.Holder());
|
||||||
|
NanReturnValue(args.Holder());
|
||||||
|
}
|
||||||
|
|
||||||
|
StereoSGBM::StereoSGBM()
|
||||||
|
: ObjectWrap(), stereo()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
StereoSGBM::StereoSGBM(int minDisparity, int ndisparities, int SADWindowSize, int p1, int p2, int disp12MaxDiff, int preFilterCap, int uniquenessRatio, int speckleWindowSize, int speckleRange, bool fullDP)
|
||||||
|
: ObjectWrap(), stereo(minDisparity, ndisparities, SADWindowSize, p1, p2, disp12MaxDiff, preFilterCap, uniquenessRatio, speckleWindowSize, speckleRange, fullDP)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO make this async
|
||||||
|
NAN_METHOD(StereoSGBM::Compute)
|
||||||
|
{
|
||||||
|
SETUP_FUNCTION(StereoSGBM)
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg 0, the 'left' image
|
||||||
|
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
||||||
|
cv::Mat left = m0->mat;
|
||||||
|
|
||||||
|
// Arg 1, the 'right' image
|
||||||
|
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
|
||||||
|
cv::Mat right = m1->mat;
|
||||||
|
|
||||||
|
// Compute stereo using the block matching algorithm
|
||||||
|
cv::Mat disparity;
|
||||||
|
self->stereo(left, right, disparity);
|
||||||
|
|
||||||
|
// Wrap the returned disparity map
|
||||||
|
Local<Object> disparityWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
||||||
|
Matrix *disp = ObjectWrap::Unwrap<Matrix>(disparityWrap);
|
||||||
|
disp->mat = disparity;
|
||||||
|
|
||||||
|
NanReturnValue(disparityWrap);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// Graph cut
|
||||||
|
|
||||||
|
v8::Persistent<FunctionTemplate> StereoGC::constructor;
|
||||||
|
|
||||||
|
void
|
||||||
|
StereoGC::Init(Handle<Object> target) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(StereoGC::New);
|
||||||
|
NanAssignPersistent(constructor, ctor);
|
||||||
|
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
ctor->SetClassName(NanNew("StereoGC"));
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(ctor, "compute", Compute);
|
||||||
|
|
||||||
|
target->Set(NanNew("StereoGC"), ctor->GetFunction());
|
||||||
|
}
|
||||||
|
|
||||||
|
NAN_METHOD(StereoGC::New) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
if (args.This()->InternalFieldCount() == 0)
|
||||||
|
NanThrowTypeError("Cannot instantiate without new");
|
||||||
|
|
||||||
|
StereoGC *stereo;
|
||||||
|
|
||||||
|
if (args.Length() == 0)
|
||||||
|
{
|
||||||
|
stereo = new StereoGC();
|
||||||
|
}
|
||||||
|
else if (args.Length() == 1)
|
||||||
|
{
|
||||||
|
stereo = new StereoGC(args[0]->IntegerValue()); // numberOfDisparities
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stereo = new StereoGC(args[0]->IntegerValue(), args[1]->IntegerValue()); // max iterations
|
||||||
|
}
|
||||||
|
|
||||||
|
stereo->Wrap(args.Holder());
|
||||||
|
NanReturnValue(args.Holder());
|
||||||
|
}
|
||||||
|
|
||||||
|
StereoGC::StereoGC(int numberOfDisparities, int maxIters)
|
||||||
|
: ObjectWrap()
|
||||||
|
{
|
||||||
|
stereo = cvCreateStereoGCState(numberOfDisparities, maxIters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO make this async
|
||||||
|
NAN_METHOD(StereoGC::Compute)
|
||||||
|
{
|
||||||
|
SETUP_FUNCTION(StereoGC)
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the arguments
|
||||||
|
|
||||||
|
// Arg 0, the 'left' image
|
||||||
|
Matrix* m0 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
|
||||||
|
cv::Mat left = m0->mat;
|
||||||
|
|
||||||
|
// Arg 1, the 'right' image
|
||||||
|
Matrix* m1 = ObjectWrap::Unwrap<Matrix>(args[1]->ToObject());
|
||||||
|
cv::Mat right = m1->mat;
|
||||||
|
|
||||||
|
// Compute stereo using the block matching algorithm
|
||||||
|
CvMat left_leg = left, right_leg = right;
|
||||||
|
CvMat *disp_left = cvCreateMat(left.rows, left.cols, CV_16S), *disp_right = cvCreateMat(right.rows, right.cols, CV_16S);
|
||||||
|
cvFindStereoCorrespondenceGC(&left_leg, &right_leg, disp_left, disp_right, self->stereo, 0);
|
||||||
|
|
||||||
|
cv::Mat disp16 = disp_left;
|
||||||
|
cv::Mat disparity(disp16.rows, disp16.cols, CV_8U);
|
||||||
|
disp16.convertTo(disparity, CV_8U, -16);
|
||||||
|
|
||||||
|
// Wrap the returned disparity map
|
||||||
|
Local<Object> disparityWrap = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
|
||||||
|
Matrix *disp = ObjectWrap::Unwrap<Matrix>(disparityWrap);
|
||||||
|
disp->mat = disparity;
|
||||||
|
|
||||||
|
NanReturnValue(disparityWrap);
|
||||||
|
|
||||||
|
} catch (cv::Exception &e) {
|
||||||
|
const char *err_msg = e.what();
|
||||||
|
NanThrowError(err_msg);
|
||||||
|
NanReturnUndefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
58
src/Stereo.h
Normal file
58
src/Stereo.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#ifndef __NODE_STEREO_H
|
||||||
|
#define __NODE_STEREO_H
|
||||||
|
|
||||||
|
#include "OpenCV.h"
|
||||||
|
|
||||||
|
class StereoBM: public node::ObjectWrap {
|
||||||
|
public:
|
||||||
|
cv::StereoBM stereo;
|
||||||
|
|
||||||
|
static Persistent<FunctionTemplate> constructor;
|
||||||
|
static void Init(Handle<Object> target);
|
||||||
|
static NAN_METHOD(New);
|
||||||
|
|
||||||
|
StereoBM(int preset = cv::StereoBM::BASIC_PRESET, int ndisparities = 0, int SADWindowSize=21);
|
||||||
|
|
||||||
|
JSFUNC(Compute);
|
||||||
|
};
|
||||||
|
|
||||||
|
class StereoSGBM: public node::ObjectWrap {
|
||||||
|
public:
|
||||||
|
cv::StereoSGBM stereo;
|
||||||
|
|
||||||
|
static Persistent<FunctionTemplate> constructor;
|
||||||
|
static void Init(Handle<Object> target);
|
||||||
|
static NAN_METHOD(New);
|
||||||
|
|
||||||
|
StereoSGBM();
|
||||||
|
StereoSGBM(int minDisparity,
|
||||||
|
int ndisparities,
|
||||||
|
int SADWindowSize,
|
||||||
|
int p1 = 0,
|
||||||
|
int p2 = 0,
|
||||||
|
int disp12MaxDiff = 0,
|
||||||
|
int preFilterCap = 0,
|
||||||
|
int uniquenessRatio = 0,
|
||||||
|
int speckleWindowSize = 0,
|
||||||
|
int speckleRange = 0,
|
||||||
|
bool fullDP = false);
|
||||||
|
|
||||||
|
JSFUNC(Compute);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CvStereoGCState;
|
||||||
|
|
||||||
|
class StereoGC: public node::ObjectWrap {
|
||||||
|
public:
|
||||||
|
CvStereoGCState *stereo;
|
||||||
|
|
||||||
|
static Persistent<FunctionTemplate> constructor;
|
||||||
|
static void Init(Handle<Object> target);
|
||||||
|
static NAN_METHOD(New);
|
||||||
|
|
||||||
|
StereoGC(int numberOfDisparities = 16, int maxIterations = 2);
|
||||||
|
|
||||||
|
JSFUNC(Compute);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -11,6 +11,7 @@
|
|||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
#include "Calib3D.h"
|
#include "Calib3D.h"
|
||||||
#include "ImgProc.h"
|
#include "ImgProc.h"
|
||||||
|
#include "Stereo.h"
|
||||||
|
|
||||||
extern "C" void
|
extern "C" void
|
||||||
init(Handle<Object> target) {
|
init(Handle<Object> target) {
|
||||||
@ -27,6 +28,9 @@ init(Handle<Object> target) {
|
|||||||
Constants::Init(target);
|
Constants::Init(target);
|
||||||
Calib3D::Init(target);
|
Calib3D::Init(target);
|
||||||
ImgProc::Init(target);
|
ImgProc::Init(target);
|
||||||
|
StereoBM::Init(target);
|
||||||
|
StereoSGBM::Init(target);
|
||||||
|
StereoGC::Init(target);
|
||||||
|
|
||||||
|
|
||||||
#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4
|
#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user