From 48c07c7e3ba27ec958dd3e8e59774eb5051c8959 Mon Sep 17 00:00:00 2001 From: Michael Thomas Schmidt Date: Mon, 17 Apr 2017 14:53:16 -0500 Subject: [PATCH] Added support for single depth pixelRow and pixelColumn --- src/Matrix.cc | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index cda5221..d6e17d5 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -565,14 +565,23 @@ NAN_METHOD(Matrix::PixelRow) { int width = self->mat.size().width; int y = info[0]->IntegerValue(); - v8::Local < v8::Array > arr = Nan::New(width * 3); + v8::Local < v8::Array > arr; - for (int x = 0; x < width; x++) { - cv::Vec3b pixel = self->mat.at(y, x); - int offset = x * 3; - arr->Set(offset, Nan::New((double) pixel.val[0])); - arr->Set(offset + 1, Nan::New((double) pixel.val[1])); - arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + if (self->mat.channels() == 3) { + arr = Nan::New(width * 3); + for (int x = 0; x < width; x++) { + cv::Vec3b pixel = self->mat.at(y, x); + int offset = x * 3; + arr->Set(offset, Nan::New((double) pixel.val[0])); + arr->Set(offset + 1, Nan::New((double) pixel.val[1])); + arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + } + } else { + arr = Nan::New(width); + for (int x = 0; x < width; x++) { + int pixel = (int)self->mat.at(y, x); + arr->Set(x, Nan::New(pixel)); + } } info.GetReturnValue().Set(arr); @@ -597,14 +606,23 @@ NAN_METHOD(Matrix::PixelCol) { int height = self->mat.size().height; int x = info[0]->IntegerValue(); - v8::Local < v8::Array > arr = Nan::New(height * 3); - - for (int y = 0; y < height; y++) { - cv::Vec3b pixel = self->mat.at(y, x); - int offset = y * 3; - arr->Set(offset, Nan::New((double) pixel.val[0])); - arr->Set(offset + 1, Nan::New((double) pixel.val[1])); - arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + v8::Local < v8::Array > arr; + + if (self->mat.channels() == 3) { + arr = Nan::New(height * 3); + for (int y = 0; y < height; y++) { + cv::Vec3b pixel = self->mat.at(y, x); + int offset = y * 3; + arr->Set(offset, Nan::New((double) pixel.val[0])); + arr->Set(offset + 1, Nan::New((double) pixel.val[1])); + arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + } + } else { + arr = Nan::New(height); + for (int y = 0; y < height; y++) { + int pixel = (int)self->mat.at(y, x); + arr->Set(y, Nan::New(pixel)); + } } info.GetReturnValue().Set(arr); }