Merge pull request #129 from jhludwig/warptransform

Warp Perspective Transform
This commit is contained in:
Peter Braden 2014-05-10 21:00:32 +02:00
commit fee6a05f9e
3 changed files with 81 additions and 1 deletions

23
examples/warp_image.js Normal file
View File

@ -0,0 +1,23 @@
var cv = require('../lib/opencv');
cv.readImage("./mona.png", function(err, im) {
var srcArray = [
0,0,
im.width(),0,
im.width(),im.height(),
0,im.height()];
var dstArray = [
0,0,
im.width()*.9,im.height()*.1,
im.width(),im.height(),
im.width()*.2,im.height()*.8];
var xfrmMat = im.getPerspectiveTransform(srcArray,dstArray);
img_warp = im.copy();
img_warp.warpPerspective(xfrmMat,im.width,im.height,[255,255,255]);
img_warp.save("/tmp/mona_warp.png");
});

View File

@ -100,6 +100,9 @@ Matrix::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor, "pushBack", PushBack); NODE_SET_PROTOTYPE_METHOD(constructor, "pushBack", PushBack);
NODE_SET_PROTOTYPE_METHOD(constructor, "putText", PutText); NODE_SET_PROTOTYPE_METHOD(constructor, "putText", PutText);
NODE_SET_PROTOTYPE_METHOD(constructor, "getPerspectiveTransform", GetPerspectiveTransform);
NODE_SET_PROTOTYPE_METHOD(constructor, "warpPerspective", WarpPerspective);
NODE_SET_METHOD(constructor, "Eye", Eye); NODE_SET_METHOD(constructor, "Eye", Eye);
@ -1756,3 +1759,54 @@ Matrix::PutText(const v8::Arguments& args) {
return scope.Close(Undefined()); return scope.Close(Undefined());
} }
Handle<Value>
Matrix::GetPerspectiveTransform(const v8::Arguments& args) {
HandleScope scope;
// extract quad args
Local<Object> srcArray = args[0]->ToObject();
Local<Object> tgtArray = args[1]->ToObject();
std::vector<cv::Point2f> src_corners(4);
std::vector<cv::Point2f> tgt_corners(4);
for (unsigned int i = 0; i < 4; i++) {
src_corners[i] = cvPoint(srcArray->Get(i*2)->IntegerValue(),srcArray->Get(i*2+1)->IntegerValue());
tgt_corners[i] = cvPoint(tgtArray->Get(i*2)->IntegerValue(),tgtArray->Get(i*2+1)->IntegerValue());
}
Local<Object> xfrm = Matrix::constructor->GetFunction()->NewInstance();
Matrix *xfrmmat = ObjectWrap::Unwrap<Matrix>(xfrm);
xfrmmat->mat = cv::getPerspectiveTransform(src_corners, tgt_corners);
return scope.Close(xfrm);
}
Handle<Value>
Matrix::WarpPerspective(const v8::Arguments& args) {
SETUP_FUNCTION(Matrix)
Matrix *xfrm = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
int width = args[1]->IntegerValue();
int height = args[2]->IntegerValue();
int flags = cv::INTER_LINEAR;
int borderMode = cv::BORDER_REPLICATE;
cv::Scalar borderColor(0, 0, 255);
if(args[3]->IsArray()) {
Local<Object> objColor = args[3]->ToObject();
borderColor = setColor(objColor);
}
cv::Mat res = cv::Mat(width, height, CV_32FC3);
cv::warpPerspective(self->mat, res, xfrm->mat, cv::Size(width, height), flags, borderMode, borderColor);
~self->mat;
self->mat = res;
return scope.Close(v8::Null());
}

View File

@ -94,6 +94,8 @@ class Matrix: public node::ObjectWrap {
JSFUNC(PushBack) JSFUNC(PushBack)
JSFUNC(PutText) JSFUNC(PutText)
JSFUNC(GetPerspectiveTransform)
JSFUNC(WarpPerspective)
/* /*
static Handle<Value> Val(const Arguments& args); static Handle<Value> Val(const Arguments& args);
static Handle<Value> RowRange(const Arguments& args); static Handle<Value> RowRange(const Arguments& args);
@ -121,7 +123,8 @@ class Matrix: public node::ObjectWrap {
static Handle<Value> Depth(const Arguments& args); static Handle<Value> Depth(const Arguments& args);
static Handle<Value> Channels(const Arguments& args); static Handle<Value> Channels(const Arguments& args);
static Handle<Value> StepOne(const Arguments& args); static Handle<Value> StepOne(const Arguments& args);
static Handle<Value> GetPerspectiveTransform(const Arguments& args);
static Handle<Value> WarpPerspective(const Arguments& args);
*/ */