diff --git a/examples/files/alpha-small.png b/examples/files/alpha-small.png new file mode 100644 index 0000000..19d7a82 Binary files /dev/null and b/examples/files/alpha-small.png differ diff --git a/src/Matrix.cc b/src/Matrix.cc index 3bf2c47..e8ccfe3 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -26,6 +26,7 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "pixelCol", PixelCol); Nan::SetPrototypeMethod(ctor, "empty", Empty); Nan::SetPrototypeMethod(ctor, "get", Get); + Nan::SetPrototypeMethod(ctor, "getPixel", GetPixel); Nan::SetPrototypeMethod(ctor, "set", Set); Nan::SetPrototypeMethod(ctor, "put", Put); Nan::SetPrototypeMethod(ctor, "brightness", Brightness); @@ -276,6 +277,35 @@ NAN_METHOD(Matrix::Get) { info.GetReturnValue().Set(Nan::New(val)); } +NAN_METHOD(Matrix::GetPixel) { + SETUP_FUNCTION(Matrix) + + int y = info[0]->IntegerValue(); + int x = info[1]->IntegerValue(); + + if (self->mat.channels() == 4) { + v8::Local < v8::Array > arr = Nan::New(4); + cv::Vec4b pixel = self->mat.at(y, x); + arr->Set(0, Nan::New((double) pixel.val[0])); + arr->Set(1, Nan::New((double) pixel.val[1])); + arr->Set(2, Nan::New((double) pixel.val[2])); + arr->Set(3, Nan::New((double) pixel.val[3])); + info.GetReturnValue().Set(arr); + } else if (self->mat.channels() == 3) { + v8::Local < v8::Array > arr = Nan::New(3); + cv::Vec3b pixel = self->mat.at(y, x); + arr->Set(0, Nan::New((double) pixel.val[0])); + arr->Set(1, Nan::New((double) pixel.val[1])); + arr->Set(2, Nan::New((double) pixel.val[2])); + info.GetReturnValue().Set(arr); + } else if(self->mat.channels() == 1) { + int pixel = (int)self->mat.at(y, x); + info.GetReturnValue().Set(pixel); + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); + } +} + NAN_METHOD(Matrix::Set) { SETUP_FUNCTION(Matrix) @@ -569,7 +599,17 @@ NAN_METHOD(Matrix::PixelRow) { int y = info[0]->IntegerValue(); v8::Local < v8::Array > arr; - if (self->mat.channels() == 3) { + if (self->mat.channels() == 4) { + arr = Nan::New(width * 4); + for (int x = 0; x < width; x++) { + cv::Vec4b pixel = self->mat.at(y, x); + int offset = x * 4; + 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])); + arr->Set(offset + 3, Nan::New((double) pixel.val[3])); + } + } else 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); @@ -578,12 +618,14 @@ NAN_METHOD(Matrix::PixelRow) { arr->Set(offset + 1, Nan::New((double) pixel.val[1])); arr->Set(offset + 2, Nan::New((double) pixel.val[2])); } - } else { + } else if(self->mat.channels() == 1){ 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)); } + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); } info.GetReturnValue().Set(arr); @@ -609,8 +651,18 @@ NAN_METHOD(Matrix::PixelCol) { int height = self->mat.size().height; int x = info[0]->IntegerValue(); v8::Local < v8::Array > arr; - - if (self->mat.channels() == 3) { + + if (self->mat.channels() == 4) { + arr = Nan::New(height * 4); + for (int y = 0; y < height; y++) { + cv::Vec4b pixel = self->mat.at(y, x); + int offset = y * 4; + 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])); + arr->Set(offset + 3, Nan::New((double) pixel.val[3])); + } + } else 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); @@ -619,13 +671,16 @@ NAN_METHOD(Matrix::PixelCol) { arr->Set(offset + 1, Nan::New((double) pixel.val[1])); arr->Set(offset + 2, Nan::New((double) pixel.val[2])); } - } else { + } else if(self->mat.channels() == 1) { 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)); } + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); } + info.GetReturnValue().Set(arr); } diff --git a/src/Matrix.h b/src/Matrix.h index 81ee0ff..15799dd 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -20,6 +20,7 @@ public: JSFUNC(Eye) // factory JSFUNC(Get) // at + JSFUNC(GetPixel) JSFUNC(Set) JSFUNC(Put) diff --git a/test/unit.js b/test/unit.js index eee0e22..8a07086 100755 --- a/test/unit.js +++ b/test/unit.js @@ -464,6 +464,75 @@ test('setColor works will alpha channels', function(assert) { }); }); +test('getPixel',function(assert){ + cv.readImage('./examples/files/alpha-test.png', function(err, imgMat) { + if (!err) { + assert.equal(imgMat.channels(),4); + assert.deepEqual(imgMat.getPixel(10,10),[0,187,255,255]); + assert.deepEqual(imgMat.getPixel(30,80),[241,161,0,128]); + assert.deepEqual(imgMat.getPixel(80,30),[0,187,124,200]); + assert.deepEqual(imgMat.getPixel(80,80),[20,83,246,70]); + + var channels = imgMat.split(); + imgMat.merge([channels[0],channels[1],channels[2]]); + assert.equal(imgMat.channels(),3); + assert.deepEqual(imgMat.getPixel(10,10),[0,187,255]); + assert.deepEqual(imgMat.getPixel(30,80),[241,161,0]); + assert.deepEqual(imgMat.getPixel(80,30),[0,187,124]); + assert.deepEqual(imgMat.getPixel(80,80),[20,83,246]); + + imgMat.merge([channels[3]]); + assert.equal(imgMat.channels(),1); + assert.deepEqual(imgMat.getPixel(0,0),255); + assert.deepEqual(imgMat.getPixel(30,80),128); + assert.deepEqual(imgMat.getPixel(80,30),200); + assert.deepEqual(imgMat.getPixel(80,80),70); + + } else { + throw err; + } + assert.end(); + }); +}); + +test('pixelRow pixelCol 4 channel image ',function(assert){ + cv.readImage('./examples/files/alpha-small.png', function(err, imgMat) { + if (!err) { + assert.equal(imgMat.channels(),4); + + assert.deepEqual(imgMat.pixelCol(0), + [ 0, 0, 0, 255, + 0, 255, 0, 88, + 0, 0, 0, 88 ]); + + assert.deepEqual(imgMat.pixelCol(1), + [ 255, 255, 255, 255, + 0, 0, 255, 88, + 255, 255, 255, 88 ]); + + assert.deepEqual(imgMat.pixelRow(0), + [ 0, 0, 0, 255, + 255, 255, 255, 255, + 0, 255, 0, 255, + 255, 0, 0, 255, + 0, 0, 255, 255, + 229, 194, 201, 155 ]); + + assert.deepEqual(imgMat.pixelRow(1), + [ 0, 255, 0, 88, + 0, 0, 255, 88, + 255, 0, 0, 88, + 47, 40, 42, 155, + 145, 66, 125, 227, + 47, 100, 163, 72 ]); + } else { + throw err; + } + assert.end(); + }); +}); + + test('toArray/fromArray working in both ways', function(assert) { var cv = require('../lib/opencv');