From 49bc593d17a1e4b5d41ed70628cb72923e301683 Mon Sep 17 00:00:00 2001 From: Anshul Jain Date: Thu, 19 Nov 2015 11:51:35 -0800 Subject: [PATCH] Add matrix reshape function --- src/Matrix.cc | 27 +++++++++++++++++++++++++++ src/Matrix.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index fd9e82d..8880386 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -104,6 +104,7 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "setWithMask", SetWithMask); Nan::SetPrototypeMethod(ctor, "meanWithMask", MeanWithMask); Nan::SetPrototypeMethod(ctor, "shift", Shift); + Nan::SetPrototypeMethod(ctor, "reshape", Reshape); Nan::SetPrototypeMethod(ctor, "release", Release); target->Set(Nan::New("Matrix").ToLocalChecked(), ctor->GetFunction()); @@ -2379,6 +2380,32 @@ NAN_METHOD(Matrix::Shift) { return; } +/** + * Changes the shape and/or the number of channels of a 2D matrix without + * copying the data. + * Reference:http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-reshape + */ +NAN_METHOD(Matrix::Reshape) { + SETUP_FUNCTION(Matrix) + + int cn = 0; + int rows = 0; + if (info.Length() == 2) { + INT_FROM_ARGS(cn, 0); + INT_FROM_ARGS(rows, 1); + } else if (info.Length() == 1) { + INT_FROM_ARGS(cn, 0); + } else { + JSTHROW("Invalid number of arguments"); + } + + cv::Mat res = self->mat.reshape(cn, rows); + ~self->mat; + self->mat = res; + + return; +} + NAN_METHOD(Matrix::Release) { Nan::HandleScope scope; diff --git a/src/Matrix.h b/src/Matrix.h index c9b77b8..835139b 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -119,6 +119,7 @@ public: JSFUNC(SetWithMask) JSFUNC(MeanWithMask) JSFUNC(Shift) + JSFUNC(Reshape) JSFUNC(Release) /*