Merge pull request #518 from DynamicSTOP/master

extended pixelCol and pixelRow (1,3,4 channels), added getPixel
This commit is contained in:
Peter Braden 2017-09-13 11:54:17 +02:00 committed by GitHub
commit 884e0dcc41
4 changed files with 130 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

View File

@ -26,6 +26,7 @@ void Matrix::Init(Local<Object> 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<Number>(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<Array>(4);
cv::Vec4b pixel = self->mat.at<cv::Vec4b>(y, x);
arr->Set(0, Nan::New<Number>((double) pixel.val[0]));
arr->Set(1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(2, Nan::New<Number>((double) pixel.val[2]));
arr->Set(3, Nan::New<Number>((double) pixel.val[3]));
info.GetReturnValue().Set(arr);
} else if (self->mat.channels() == 3) {
v8::Local < v8::Array > arr = Nan::New<Array>(3);
cv::Vec3b pixel = self->mat.at<cv::Vec3b>(y, x);
arr->Set(0, Nan::New<Number>((double) pixel.val[0]));
arr->Set(1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(2, Nan::New<Number>((double) pixel.val[2]));
info.GetReturnValue().Set(arr);
} else if(self->mat.channels() == 1) {
int pixel = (int)self->mat.at<unsigned char>(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<Array>(width * 4);
for (int x = 0; x < width; x++) {
cv::Vec4b pixel = self->mat.at<cv::Vec4b>(y, x);
int offset = x * 4;
arr->Set(offset, Nan::New<Number>((double) pixel.val[0]));
arr->Set(offset + 1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(offset + 2, Nan::New<Number>((double) pixel.val[2]));
arr->Set(offset + 3, Nan::New<Number>((double) pixel.val[3]));
}
} else if(self->mat.channels() == 3){
arr = Nan::New<Array>(width * 3);
for (int x = 0; x < width; x++) {
cv::Vec3b pixel = self->mat.at<cv::Vec3b>(y, x);
@ -578,12 +618,14 @@ NAN_METHOD(Matrix::PixelRow) {
arr->Set(offset + 1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(offset + 2, Nan::New<Number>((double) pixel.val[2]));
}
} else {
} else if(self->mat.channels() == 1){
arr = Nan::New<Array>(width);
for (int x = 0; x < width; x++) {
int pixel = (int)self->mat.at<unsigned char>(y, x);
arr->Set(x, Nan::New<Number>(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<Array>(height * 4);
for (int y = 0; y < height; y++) {
cv::Vec4b pixel = self->mat.at<cv::Vec4b>(y, x);
int offset = y * 4;
arr->Set(offset, Nan::New<Number>((double) pixel.val[0]));
arr->Set(offset + 1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(offset + 2, Nan::New<Number>((double) pixel.val[2]));
arr->Set(offset + 3, Nan::New<Number>((double) pixel.val[3]));
}
} else if (self->mat.channels() == 3) {
arr = Nan::New<Array>(height * 3);
for (int y = 0; y < height; y++) {
cv::Vec3b pixel = self->mat.at<cv::Vec3b>(y, x);
@ -619,13 +671,16 @@ NAN_METHOD(Matrix::PixelCol) {
arr->Set(offset + 1, Nan::New<Number>((double) pixel.val[1]));
arr->Set(offset + 2, Nan::New<Number>((double) pixel.val[2]));
}
} else {
} else if(self->mat.channels() == 1) {
arr = Nan::New<Array>(height);
for (int y = 0; y < height; y++) {
int pixel = (int)self->mat.at<unsigned char>(y, x);
arr->Set(y, Nan::New<Number>(pixel));
}
} else {
Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported");
}
info.GetReturnValue().Set(arr);
}

View File

@ -20,6 +20,7 @@ public:
JSFUNC(Eye) // factory
JSFUNC(Get) // at
JSFUNC(GetPixel)
JSFUNC(Set)
JSFUNC(Put)

View File

@ -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');