From 8c1b5e401b9e09f6751dca685346869a97f38929 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Thu, 6 Oct 2016 10:06:20 -0700 Subject: [PATCH 1/3] handles colors with less-/more-than three channels --- examples/files/alpha-test.png | Bin 0 -> 815 bytes src/Matrix.cc | 37 +++++++++++++++++++++++++++++----- test/unit.js | 26 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 examples/files/alpha-test.png diff --git a/examples/files/alpha-test.png b/examples/files/alpha-test.png new file mode 100644 index 0000000000000000000000000000000000000000..f5a64a7dad56a03369e8045d08cf35b623f6a6d1 GIT binary patch literal 815 zcmeAS@N?(olHy`uVBq!ia0vp^DIm objColor) { - Local valB = objColor->Get(0); - Local valG = objColor->Get(1); - Local valR = objColor->Get(2); + cv::Scalar color; + Local val1; + Local val2; + Local val3; + Local val4; + + // 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)) { + val1 = objColor->Get(0); + color = cv::Scalar(val1->IntegerValue()); + } else if (!objColor->HasRealIndexedProperty(2)) { + val1 = objColor->Get(0); + val2 = objColor->Get(1); + color = cv::Scalar(val1->IntegerValue(), val2->IntegerValue()); + } else if (!objColor->HasRealIndexedProperty(4)) { + val1 = objColor->Get(0); + val2 = objColor->Get(1); + val3 = objColor->Get(2); + val4 = objColor->Get(3); + color = cv::Scalar(val1->IntegerValue(), + val2->IntegerValue(), + val3->IntegerValue(), + val4->IntegerValue()); + } else { + val1 = objColor->Get(0); + val2 = objColor->Get(1); + val3 = objColor->Get(2); + color = cv::Scalar(val1->IntegerValue(), + val2->IntegerValue(), + val3->IntegerValue()); + } - cv::Scalar color = cv::Scalar(valB->IntegerValue(), valG->IntegerValue(), - valR->IntegerValue()); return color; } diff --git a/test/unit.js b/test/unit.js index 412eef3..09117aa 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')() From bd9e313297a780498457b0cbdad9eb6c176eae52 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Sat, 29 Oct 2016 11:21:17 -0700 Subject: [PATCH 2/3] cleaned up setColor clean up --- src/Matrix.cc | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 419e0be..2e09975 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1598,39 +1598,27 @@ NAN_METHOD(Matrix::HoughCircles) { cv::Scalar setColor(Local objColor) { cv::Scalar color; - Local val1; - Local val2; - Local val3; - Local val4; + int64_t channels[4] = { 0, 0, 0, 0 }; // 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)) { - val1 = objColor->Get(0); - color = cv::Scalar(val1->IntegerValue()); + channels[0] = objColor->Get(0)->IntegerValue(); } else if (!objColor->HasRealIndexedProperty(2)) { - val1 = objColor->Get(0); - val2 = objColor->Get(1); - color = cv::Scalar(val1->IntegerValue(), val2->IntegerValue()); + channels[0] = objColor->Get(0)->IntegerValue(); + channels[1] = objColor->Get(1)->IntegerValue(); } else if (!objColor->HasRealIndexedProperty(4)) { - val1 = objColor->Get(0); - val2 = objColor->Get(1); - val3 = objColor->Get(2); - val4 = objColor->Get(3); - color = cv::Scalar(val1->IntegerValue(), - val2->IntegerValue(), - val3->IntegerValue(), - val4->IntegerValue()); + 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 { - val1 = objColor->Get(0); - val2 = objColor->Get(1); - val3 = objColor->Get(2); - color = cv::Scalar(val1->IntegerValue(), - val2->IntegerValue(), - val3->IntegerValue()); + channels[0] = objColor->Get(0)->IntegerValue(); + channels[1] = objColor->Get(1)->IntegerValue(); + channels[2] = objColor->Get(2)->IntegerValue(); } - return color; + return cv::Scalar(channels[0], channels[1], channels[2], channels[3]); } cv::Point setPoint(Local objPoint) { From 64774c23723e19c4a4b806fc80973264a356aa63 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Sat, 29 Oct 2016 11:43:08 -0700 Subject: [PATCH 3/3] removed unneeded variable from setColor --- src/Matrix.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 2e09975..313a322 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1597,7 +1597,6 @@ NAN_METHOD(Matrix::HoughCircles) { } cv::Scalar setColor(Local objColor) { - cv::Scalar color; int64_t channels[4] = { 0, 0, 0, 0 }; // We'll accomodate a channel count up to 4 and fall back to the old