Skip to content

Commit

Permalink
Unit tests for Brief
Browse files Browse the repository at this point in the history
  • Loading branch information
mairatma authored and eduardolundgren committed Jul 20, 2014
1 parent b058fe2 commit b83336c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/tracking-min.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions build/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,9 @@
* to describe the corner, e.g. [0,0,0,0, 0,0,0,0, ...].
*/
tracking.Brief.getDescriptors = function(pixels, width, keypoints) {
// Optimizing divide by four operation using binary shift
// (this.N >> 5) === this.N/4.
var descriptors = new Int32Array(keypoints.length * (this.N >> 5));
// Optimizing divide by 32 operation using binary shift
// (this.N >> 5) === this.N/32.
var descriptors = new Int32Array((keypoints.length >> 1) * (this.N >> 5));
var descriptorWord = 0;
var offsets = this.getRandomOffsets_(width);
var position = 0;
Expand All @@ -1226,9 +1226,15 @@
var offsetsPosition = 0;
for (var j = 0, n = this.N; j < n; j++) {
if (pixels[offsets[offsetsPosition++] + w] < pixels[offsets[offsetsPosition++] + w]) {
// The bit in the position `j % 32` of descriptorWord should be set to 1. We do
// this by making an OR operation with a binary number that only has the bit
// in that position set to 1. That binary number is obtained by shifting 1 left by
// `j % 32` (which is the same as `j & 31` left) positions.
descriptorWord |= 1 << (j & 31);
}

// If the next j is a multiple of 32, we will need to use a new descriptor word to hold
// the next results.
if (!((j + 1) & 31)) {
descriptors[position++] = descriptorWord;
descriptorWord = 0;
Expand Down Expand Up @@ -1272,8 +1278,8 @@
var minj = 0;
for (var j = 0; j < len2; j++) {
var dist = 0;
// Optimizing divide by four operation using binary shift
// (this.N >> 5) === this.N/4.
// Optimizing divide by 32 operation using binary shift
// (this.N >> 5) === this.N/32.
for (var k = 0, n = this.N >> 5; k < n; k++) {
dist += tracking.Math.hammingWeight(descriptors1[i * n + k] ^ descriptors2[j * n + k]);
}
Expand Down
16 changes: 11 additions & 5 deletions src/features/Brief.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
* to describe the corner, e.g. [0,0,0,0, 0,0,0,0, ...].
*/
tracking.Brief.getDescriptors = function(pixels, width, keypoints) {
// Optimizing divide by four operation using binary shift
// (this.N >> 5) === this.N/4.
var descriptors = new Int32Array(keypoints.length * (this.N >> 5));
// Optimizing divide by 32 operation using binary shift
// (this.N >> 5) === this.N/32.
var descriptors = new Int32Array((keypoints.length >> 1) * (this.N >> 5));
var descriptorWord = 0;
var offsets = this.getRandomOffsets_(width);
var position = 0;
Expand All @@ -59,9 +59,15 @@
var offsetsPosition = 0;
for (var j = 0, n = this.N; j < n; j++) {
if (pixels[offsets[offsetsPosition++] + w] < pixels[offsets[offsetsPosition++] + w]) {
// The bit in the position `j % 32` of descriptorWord should be set to 1. We do
// this by making an OR operation with a binary number that only has the bit
// in that position set to 1. That binary number is obtained by shifting 1 left by
// `j % 32` (which is the same as `j & 31` left) positions.
descriptorWord |= 1 << (j & 31);
}

// If the next j is a multiple of 32, we will need to use a new descriptor word to hold
// the next results.
if (!((j + 1) & 31)) {
descriptors[position++] = descriptorWord;
descriptorWord = 0;
Expand Down Expand Up @@ -105,8 +111,8 @@
var minj = 0;
for (var j = 0; j < len2; j++) {
var dist = 0;
// Optimizing divide by four operation using binary shift
// (this.N >> 5) === this.N/4.
// Optimizing divide by 32 operation using binary shift
// (this.N >> 5) === this.N/32.
for (var k = 0, n = this.N >> 5; k < n; k++) {
dist += tracking.Math.hammingWeight(descriptors1[i * n + k] ^ descriptors2[j * n + k]);
}
Expand Down
74 changes: 74 additions & 0 deletions test/Brief.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

var tracking = require('./utils/sandbox.js');

module.exports = {
setUp: function(done) {
done();
},

tearDown: function(done) {
done();
},

testGetDescriptors: function(test) {
var descriptors;
var grayScale = [
0, 0, 1, 0, 0, 0,
1, 9, 0, 9, 1, 0,
0, 1, 1, 1, 0, 0
];
var repeat = [-7, 7, -6, 6, -5, 5, -1, 1];
var width = 6;

// Write the offsets manually, as we can't verify results that are obtained randomly.
tracking.Brief.randomOffsets_[width] = [];
for (var i = 0; i < tracking.Brief.N; i++) {
var position = i % 4;
tracking.Brief.randomOffsets_[width].push(repeat[position * 2], repeat[position * 2 + 1]);
}

descriptors = tracking.Brief.getDescriptors(grayScale, width, [1, 1, 3, 1]);

test.equal(8, descriptors.length, 'There should be 8 descriptor words');

for (var i = 0; i < 4; i++) {
test.equal(858993459, descriptors[i], 'Descriptor should be 858993459');
}
for (var i = 5; i < 8; i++) {
test.equal(-286331154, descriptors[i], 'Descriptor should be -286331154');
}

test.done();
},

testGetMatchings: function(test) {
var descriptors1;
var descriptors2;
var grayScale1 = [
0, 0, 1, 0, 0, 0,
1, 9, 0, 9, 1, 0,
0, 1, 1, 1, 0, 0
];
var grayScale2 = [
0, 0, 0, 1, 0, 0,
0, 1, 9, 0, 9, 1,
0, 0, 1, 1, 1, 0
];
var keypoints1 = [1, 1, 3, 1];
var keypoints2 = [4, 1, 2, 1];
var matchings;
var width = 6;

descriptors1 = tracking.Brief.getDescriptors(grayScale1, width, keypoints1);
descriptors2 = tracking.Brief.getDescriptors(grayScale2, width, keypoints2);

matchings = tracking.Brief.match(keypoints1, descriptors1, keypoints2, descriptors2);

test.equal(2, matchings.length, 'There should be 2 matchings');
test.equal(1, matchings[0], 'Keypoint 0 from 1st array should match keypoint 1 from the 2nd');
test.equal(0, matchings[1], 'Keypoint 1 from 1st array should match keypoint 0 from the 2nd');

test.done();
}
};

0 comments on commit b83336c

Please sign in to comment.