mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Added graph-cut stereo algorithm
This commit is contained in:
parent
487ac8a137
commit
d0400ec5ae
@ -1,5 +1,6 @@
|
||||
#include "Stereo.h"
|
||||
#include "Matrix.h"
|
||||
#include <opencv2/legacy/legacy.hpp>
|
||||
|
||||
// Block matching
|
||||
|
||||
@ -231,3 +232,92 @@ NAN_METHOD(StereoSGBM::Compute)
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
15
src/Stereo.h
15
src/Stereo.h
@ -40,4 +40,19 @@ public:
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user