diff --git a/examples/files/alpha-test.png b/examples/files/alpha-test.png new file mode 100644 index 0000000..f5a64a7 Binary files /dev/null and b/examples/files/alpha-test.png differ diff --git a/src/Matrix.cc b/src/Matrix.cc index 170c913..06dbce4 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1606,13 +1606,27 @@ NAN_METHOD(Matrix::HoughCircles) { } cv::Scalar setColor(Local objColor) { - Local valB = objColor->Get(0); - Local valG = objColor->Get(1); - Local valR = objColor->Get(2); + int64_t channels[4] = { 0, 0, 0, 0 }; - cv::Scalar color = cv::Scalar(valB->IntegerValue(), valG->IntegerValue(), - valR->IntegerValue()); - return color; + // We'll accomodate a channel count up to 4 and fall back to the old + // "assume it's always 3" in the default case + if (!objColor->HasRealIndexedProperty(1)) { + channels[0] = objColor->Get(0)->IntegerValue(); + } else if (!objColor->HasRealIndexedProperty(2)) { + channels[0] = objColor->Get(0)->IntegerValue(); + channels[1] = objColor->Get(1)->IntegerValue(); + } else if (!objColor->HasRealIndexedProperty(4)) { + channels[0] = objColor->Get(0)->IntegerValue(); + channels[1] = objColor->Get(1)->IntegerValue(); + channels[2] = objColor->Get(2)->IntegerValue(); + channels[3] = objColor->Get(3)->IntegerValue(); + } else { + channels[0] = objColor->Get(0)->IntegerValue(); + channels[1] = objColor->Get(1)->IntegerValue(); + channels[2] = objColor->Get(2)->IntegerValue(); + } + + return cv::Scalar(channels[0], channels[1], channels[2], channels[3]); } cv::Point setPoint(Local objPoint) { diff --git a/test/unit.js b/test/unit.js index a003ea2..c28ad04 100755 --- a/test/unit.js +++ b/test/unit.js @@ -391,5 +391,31 @@ test('MatchTemplateByMatrix', function(assert) { }) }); +test('setColor works will alpha channels', function(assert) { + var cv = require('../lib/opencv'); + var mat = new cv.Matrix(100, 100, cv.Constants.CV_8UC4); + + var SQUARE = [ 50, 50 ]; + mat.rectangle([ 0, 0 ], SQUARE, [ 0, 187, 255, 255 ], -1); + mat.rectangle([ 0, 50 ], SQUARE, [ 0, 187, 124, 200 ], -1); + mat.rectangle([ 50, 0 ], SQUARE, [ 241, 161, 0, 128 ], -1); + mat.rectangle([ 50, 50 ], SQUARE, [ 20, 83, 246, 70 ], -1); + + cv.readImage('./examples/files/alpha-test.png', function(err, imgMat) { + if (!err) { + var diff = new cv.Matrix(); + diff.absDiff(mat, imgMat); + // We'll verify that each channel is 0 + var channels = diff.split(); + for (var i = 0; i < 4; i++) { + assert.equal(channels[i].countNonZero(), 0); + } + } else { + throw err; + } + assert.end(); + }); +}); + // Test the examples folder. require('./examples')()