Added tests for image-size calculation

pull/44/head
Christoph Oberhofer 10 years ago
parent 89da5cc4e7
commit 7bb34b546b

@ -0,0 +1,65 @@
define(['barcode_locator', 'config', 'html_utils'],
function(BarcodeLocator, Config, HtmlUtils){
describe('checkImageConstraints', function() {
var config,
inputStream,
imageSize;
beforeEach(function() {
imageSize = {
x: 640, y: 480
};
config = HtmlUtils.mergeObjects({}, Config);
inputStream = {
getWidth: function() {
return imageSize.x;
},
getHeight: function() {
return imageSize.y;
},
setWidth: function() {},
setHeight: function() {}
};
sinon.stub(inputStream, "setWidth", function(width) {
imageSize.x = width;
});
sinon.stub(inputStream, "setHeight", function(height) {
imageSize.y = height;
});
});
afterEach(function() {
inputStream.setWidth.restore();
inputStream.setHeight.restore();
});
it('should not adjust the image-size if not needed', function() {
var expected = {x: imageSize.x, y: imageSize.y};
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
expect(inputStream.getWidth()).to.be.equal(expected.x);
expect(inputStream.getHeight()).to.be.equal(expected.y);
});
it('should adjust the image-size', function() {
var expected = {x: imageSize.x, y: imageSize.y};
config.locator.halfSample = true;
imageSize.y += 1;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
expect(inputStream.getWidth()).to.be.equal(expected.x);
expect(inputStream.getHeight()).to.be.equal(expected.y);
});
it('should adjust the image-size', function() {
var expected = {x: imageSize.x, y: imageSize.y};
imageSize.y += 1;
config.locator.halfSample = false;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
expect(inputStream.getHeight()).to.be.equal(expected.y);
expect(inputStream.getWidth()).to.be.equal(expected.x);
});
});
});

@ -8,4 +8,20 @@ define(['cv_utils'], function(CVUtils){
expect(res.toVec2()[0]).to.equal(1);
});
});
describe('calculatePatchSize', function() {
it('should not throw an error in case of valid image size', function() {
var expected = {x: 32, y: 32},
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
expect(patchSize).to.be.deep.equal(expected);
});
it('should thow an error if image size it not valid', function() {
var expected = {x: 32, y: 32},
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
expect(patchSize).to.be.deep.equal(expected);
});
});
});

@ -486,6 +486,7 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
initBuffers();
initCanvas();
},
locate : function() {
var patchesFound,
topLabels = [],
@ -516,6 +517,30 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
boxes = findBoxes(topLabels, maxLabel);
return boxes;
},
checkImageConstraints: function(inputStream, config) {
var patchSize,
width = inputStream.getWidth(),
height = inputStream.getHeight(),
halfSample = config.halfSample ? 0.5 : 1,
size = {
x: Math.floor(width * halfSample),
y: Math.floor(height * halfSample)
};
patchSize = CVUtils.calculatePatchSize(config.patchSize, size);
inputStream.setWidth(Math.floor(Math.floor(size.x/patchSize.x)*(1/halfSample)*patchSize.x));
inputStream.setHeight(Math.floor(Math.floor(size.y/patchSize.y)*(1/halfSample)*patchSize.y));
console.log("Patch-Size: " + JSON.stringify(patchSize));
if ((inputStream.getWidth() % patchSize.x) === 0 && (inputStream.getHeight() % patchSize.y) === 0) {
return true;
}
throw new Error("Image dimensions do not comply with the current settings: Width (" +
width + " )and height (" + height +
") must a multiple of " + patchSize.x);
}
};
});

@ -647,7 +647,6 @@ define(['cluster', 'glMatrixAddon', "array_helper"], function(Cluster2, glMatrix
optimalPatchSize = findPatchSizeForDivisors(common);
if (!optimalPatchSize) {
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
throw new AdjustToSizeError("", optimalPatchSize);
}
return optimalPatchSize;
};

@ -14,8 +14,7 @@ define([
"config",
"events",
"camera_access",
"image_debug",
"cv_utils"],
"image_debug"],
function(Code128Reader,
EANReader,
InputStream,
@ -27,8 +26,7 @@ function(Code128Reader,
_config,
Events,
CameraAccess,
ImageDebug,
CVUtils) {
ImageDebug) {
"use strict";
var _inputStream,
@ -107,39 +105,10 @@ function(Code128Reader,
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
}
function checkImageConstraints() {
var patchSize,
width = _inputStream.getWidth(),
height = _inputStream.getHeight(),
halfSample = _config.locator.halfSample ? 0.5 : 1,
size = {
x: Math.floor(width * halfSample),
y: Math.floor(height * halfSample)
};
function canRecord(cb) {
if (_config.locate) {
try {
console.log(size);
patchSize = CVUtils.calculatePatchSize(_config.locator.patchSize, size);
} catch (error) {
if (error instanceof CVUtils.AdjustToSizeError) {
_inputStream.setWidth(Math.floor(Math.floor(size.x/error.patchSize.x)*(1/halfSample)*error.patchSize.x));
_inputStream.setHeight(Math.floor(Math.floor(size.y/error.patchSize.y)*(1/halfSample)*error.patchSize.y));
patchSize = error.patchSize;
}
}
console.log("Patch-Size: " + JSON.stringify(patchSize));
if ((_inputStream.getWidth() % patchSize.x) === 0 && (_inputStream.getHeight() % patchSize.y) === 0) {
return true;
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
}
}
throw new Error("Image dimensions do not comply with the current settings: Width (" +
width + " )and height (" + height +
") must a multiple of " + patchSize.x);
}
function canRecord(cb) {
checkImageConstraints();
initCanvas();
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
initConfig();

Loading…
Cancel
Save