Experimenting with detecting the whole area

feature/#70
Christoph Oberhofer 9 years ago
parent a8b2d3ed9a
commit 5491b5b808

@ -1,3 +1,6 @@
const vec2 = {
clone: require('gl-vec2/clone')
};
export function getCenterLineFromBox(box) { export function getCenterLineFromBox(box) {
return [{ return [{
@ -41,3 +44,10 @@ export function getExtendedLine(inputImageWrapper, line, angle, ext) {
} }
return line; return line;
} }
export function getPointOnLine(line, distance, angle) {
const x = distance * Math.cos(angle) + line[0].x,
y = distance * Math.sin(angle) + line[0].y;
return vec2.clone([x, y]);
}

@ -2,7 +2,8 @@ import {
getCenterLineFromBox as getLine, getCenterLineFromBox as getLine,
getLineLength, getLineLength,
getLineAngle, getLineAngle,
getExtendedLine getExtendedLine,
getPointOnLine
} from '../common/geometric'; } from '../common/geometric';
import ImageDebug from '../common/image_debug'; import ImageDebug from '../common/image_debug';
import Bresenham from '../decoder/bresenham'; import Bresenham from '../decoder/bresenham';
@ -11,29 +12,52 @@ import {findPattern} from '../reader/common';
const vec2 = { const vec2 = {
clone: require('gl-vec2/clone'), clone: require('gl-vec2/clone'),
dot: require('gl-vec2/dot') dot: require('gl-vec2/dot')
} };
const START_PATTERN = [8, 1, 1, 1, 1, 1, 1, 3], const START_PATTERN = [8, 1, 1, 1, 1, 1, 1, 3],
STOP_PATTERN = [7, 1, 1, 3, 1, 1, 1, 2, 1], STOP_PATTERN = [7, 1, 1, 3, 1, 1, 1, 2, 1],
MODULO = START_PATTERN.reduce((sum, i) => (sum + i),0); MODULO = START_PATTERN.reduce((sum, i) => (sum + i), 0);
function findTopLeft(line, angle, inputImageWrapper, ctx) {
let barcodeLine,
match,
pos = [
vec2.clone([0, 0]),
vec2.clone([0, 0])
],
xdir = Math.sin(angle),
ydir = Math.cos(angle);
do {
barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]);
if (ENV.development && ctx) {
ImageDebug.drawPath(line, {x: 'x', y: 'y'}, ctx, {color: 'red', lineWidth: 1});
}
Bresenham.toBinaryLine(barcodeLine);
match = findPattern(barcodeLine.line, START_PATTERN, {modulo: MODULO});
if (match) {
pos[0] = getPointOnLine(line, match.start, angle);
pos[1] = getPointOnLine(line, match.end, angle);
line[0].x += xdir*10;
line[0].y -= ydir*10;
line[1].x += xdir*10;
line[1].y -= ydir*10;
}
ImageDebug.drawVertices(pos, {x: 0, y: 1}, ctx, {color: 'blue', lineWidth: 1});
} while (match);
return pos;
}
export default function detect(inputImageWrapper, box, ctx) { export default function detect(inputImageWrapper, box, ctx) {
let line = getLine(box), let line = getLine(box),
lineLength = getLineLength(line), lineLength = getLineLength(line),
lineAngle = getLineAngle(line), lineAngle = getLineAngle(line),
extendedLine = getExtendedLine(inputImageWrapper, line, lineAngle, Math.floor(lineLength * 0.15)), extendedLine = getExtendedLine(inputImageWrapper, line, lineAngle, Math.floor(lineLength * 0.15));
barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, extendedLine[0], extendedLine[1]);
if (ENV.development) {
if (ctx) {
ImageDebug.drawPath(extendedLine, {x: 'x', y: 'y'}, ctx, {color: 'red', lineWidth: 1});
}
}
Bresenham.toBinaryLine(barcodeLine);
const match = findPattern(barcodeLine.line, START_PATTERN, {modulo: MODULO}); var topLeft = findTopLeft(extendedLine, lineAngle, inputImageWrapper, ctx);
console.log(match); console.log(topLeft);
return [ return [
vec2.clone([199, 84]), vec2.clone([199, 84]),
@ -44,5 +68,5 @@ export default function detect(inputImageWrapper, box, ctx) {
vec2.clone([310, 295]), vec2.clone([310, 295]),
vec2.clone([554, 83]), vec2.clone([554, 83]),
vec2.clone([580, 277]) vec2.clone([580, 277])
] ];
}; };

@ -60,8 +60,8 @@ export function findPattern(row, pattern, {
offset, offset,
isWhite=false, isWhite=false,
tryHarder=true, tryHarder=true,
epsilon=0.5, epsilon=0.8,
maxSingleError=0.5, maxSingleError=0.8,
modulo}) { modulo}) {
var counter = [], var counter = [],
i, i,

Loading…
Cancel
Save