Skip to content

Commit

Permalink
Track external memory usage for matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
dstarke committed Dec 22, 2017
1 parent 48a4fc3 commit dbf2df0
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 127 deletions.
18 changes: 4 additions & 14 deletions src/BackgroundSubtractor.cc
Expand Up @@ -255,10 +255,6 @@ NAN_METHOD(BackgroundSubtractorWrap::ApplyMOG) {


try {
Local<Object> fgMask =
Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(fgMask);

cv::Mat mat;
if (Buffer::HasInstance(info[0])) {
uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject());
Expand Down Expand Up @@ -287,7 +283,7 @@ NAN_METHOD(BackgroundSubtractorWrap::ApplyMOG) {
#endif
}

img->mat = _fgMask;
Local<Object> fgMask = Matrix::CreateWrappedFromMat(_fgMask);
mat.release();

argv[0] = Nan::Null();
Expand Down Expand Up @@ -344,9 +340,7 @@ class AsyncBackgroundSubtractorWorker: public Nan::AsyncWorker {
void HandleOKCallback() {
Nan::HandleScope scope;

Local<Object> im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *imgout = Nan::ObjectWrap::Unwrap<Matrix>(im_to_return);
imgout->mat = _fgMask;
Local<Object> im_to_return = Matrix::CreateWrappedFromMat(_fgMask);

Local<Value> argv[] = {
Nan::Null()
Expand All @@ -372,7 +366,6 @@ NAN_METHOD(BackgroundSubtractorWrap::Apply) {
SETUP_FUNCTION(BackgroundSubtractorWrap);
int callback_arg = -1;
int numargs = info.Length();
int success = 1;

Local<Function> cb;

Expand Down Expand Up @@ -407,10 +400,7 @@ NAN_METHOD(BackgroundSubtractorWrap::Apply) {
} else { //synchronous - return the image

try {
Local<Object> fgMask =
Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(fgMask);

Local<Object> fgMask;
cv::Mat mat;
if (Buffer::HasInstance(info[0])) {
uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject());
Expand All @@ -436,7 +426,7 @@ NAN_METHOD(BackgroundSubtractorWrap::Apply) {
#else
self->subtractor->operator()(mat, _fgMask);
#endif
img->mat = _fgMask;
fgMask = Matrix::CreateWrappedFromMat(_fgMask);
}

mat.release();
Expand Down
4 changes: 1 addition & 3 deletions src/FaceRecognizer.cc
Expand Up @@ -448,9 +448,7 @@ NAN_METHOD(FaceRecognizerWrap::GetMat) {
m = self->rec->getMat(key);
#endif

Local<Object> im = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
img->mat = m;
Local<Object> im = Matrix::CreateWrappedFromMat(m);

info.GetReturnValue().Set(im);
}
Expand Down
25 changes: 6 additions & 19 deletions src/ImgProc.cc
Expand Up @@ -36,9 +36,7 @@ NAN_METHOD(ImgProc::DistanceTransform) {
cv::distanceTransform(inputImage, outputImage, distType, 0);

// Wrap the output image
Local<Object> outMatrixWrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *outMatrix = Nan::ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = outputImage;
Local<Object> outMatrixWrap = Matrix::CreateWrappedFromMat(outputImage);

// Return the output image
info.GetReturnValue().Set(outMatrixWrap);
Expand Down Expand Up @@ -75,9 +73,7 @@ NAN_METHOD(ImgProc::Undistort) {
cv::undistort(inputImage, outputImage, K, dist);

// Wrap the output image
Local<Object> outMatrixWrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *outMatrix = Nan::ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = outputImage;
Local<Object> outMatrixWrap = Matrix::CreateWrappedFromMat(outputImage);

// Return the output image
info.GetReturnValue().Set(outMatrixWrap);
Expand Down Expand Up @@ -128,13 +124,8 @@ NAN_METHOD(ImgProc::InitUndistortRectifyMap) {
cv::initUndistortRectifyMap(K, dist, R, newK, imageSize, m1type, map1, map2);

// Wrap the output maps
Local<Object> map1Wrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *map1Matrix = Nan::ObjectWrap::Unwrap<Matrix>(map1Wrap);
map1Matrix->mat = map1;

Local<Object> map2Wrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *map2Matrix = Nan::ObjectWrap::Unwrap<Matrix>(map2Wrap);
map2Matrix->mat = map2;
Local<Object> map1Wrap = Matrix::CreateWrappedFromMat(map1);
Local<Object> map2Wrap = Matrix::CreateWrappedFromMat(map2);

// Make a return object with the two maps
Local<Object> ret = Nan::New<Object>();
Expand Down Expand Up @@ -181,9 +172,7 @@ NAN_METHOD(ImgProc::Remap) {
cv::remap(inputImage, outputImage, map1, map2, interpolation);

// Wrap the output image
Local<Object> outMatrixWrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *outMatrix = Nan::ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = outputImage;
Local<Object> outMatrixWrap = Matrix::CreateWrappedFromMat(outputImage);

// Return the image
info.GetReturnValue().Set(outMatrixWrap);
Expand Down Expand Up @@ -223,9 +212,7 @@ NAN_METHOD(ImgProc::GetStructuringElement) {
cv::Mat mat = cv::getStructuringElement(shape, ksize);

// Wrap the output image
Local<Object> outMatrixWrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *outMatrix = ObjectWrap::Unwrap<Matrix>(outMatrixWrap);
outMatrix->mat = mat;
Local<Object> outMatrixWrap = Matrix::CreateWrappedFromMat(mat);

// Return the image
info.GetReturnValue().Set(outMatrixWrap);
Expand Down
8 changes: 2 additions & 6 deletions src/LDAWrap.cc
Expand Up @@ -66,9 +66,7 @@ NAN_METHOD(LDAWrap::SubspaceProject) {

cv::Mat m = cv::subspaceProject(w->mat, mean->mat, src->mat);

Local<Object> im = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
img->mat = m;
Local<Object> im = Matrix::CreateWrappedFromMat(m);

info.GetReturnValue().Set(im);
}
Expand All @@ -92,9 +90,7 @@ NAN_METHOD(LDAWrap::SubspaceReconstruct) {

cv::Mat m = cv::subspaceReconstruct(w->mat, mean->mat, src->mat);

Local<Object> im = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
img->mat = m;
Local<Object> im = Matrix::CreateWrappedFromMat(m);

info.GetReturnValue().Set(im);
}
Expand Down

0 comments on commit dbf2df0

Please sign in to comment.