diff --git a/lib/frame_grabber.js b/lib/frame_grabber.js index 3d7d132..9607389 100644 --- a/lib/frame_grabber.js +++ b/lib/frame_grabber.js @@ -15,17 +15,23 @@ define(["cv_utils"], function(CVUtils) { _video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()), _canvasSize = inputStream.getCanvasSize(), _size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()), - topRight = inputStream.getTopRight(), + _topRight = inputStream.getTopRight(), _data = new Uint8Array(_size.x * _size.y), _grayData = new Uint8Array(_video_size.x * _video_size.y), - _canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y); + _canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y), + _grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0), + _canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0), + _targetImageArray = _canvasImageArray.hi(_topRight.x + _size.x, _topRight.y + _size.y).lo(_topRight.x, _topRight.y), + _stepSizeX = _video_size.x/_canvasSize.x, + _stepSizeY = _video_size.y/_canvasSize.y; console.log("FrameGrabber", JSON.stringify({ - size: _size, - topRight: topRight, - videoSize: _video_size, - canvasSize: _canvasSize - })); + videoSize: _grayImageArray.shape, + canvasSize: _canvasImageArray.shape, + stepSize: [_stepSizeX, _stepSizeY], + size: _targetImageArray.shape, + topRight: _topRight + }, null, 2)); /** * Uses the given array as frame-buffer @@ -46,51 +52,40 @@ define(["cv_utils"], function(CVUtils) { * The image-data is converted to gray-scale and then half-sampled if configured. */ _that.grab = function() { - var doHalfSample = _streamConfig.halfSample, - frame = inputStream.getFrame(); + var frame = inputStream.getFrame(); if (frame) { - this.scaleAndCrop(frame, _video_size, _canvasSize, topRight, _size); + this.scaleAndCrop(frame); return true; } else { return false; } }; - _that.scaleAndCrop = function(frame, frameSize, canvasSize, topRight, targetSize) { - var grayImageArray = Ndarray(_grayData, [frameSize.y, frameSize.x]).transpose(1, 0), - canvasImageArray = Ndarray(_canvasData, [canvasSize.y, canvasSize.x]).transpose(1, 0), - targetImageArray = canvasImageArray.hi(topRight.x + targetSize.x, topRight.y + targetSize.y).lo(topRight.x, topRight.y), - stepSizeX = frameSize.x/canvasSize.x, - stepSizeY = frameSize.y/canvasSize.y, - x, + _that.scaleAndCrop = function(frame) { + var x, y; - console.log("Input-size: ", grayImageArray.shape); - console.log("Canvas-size: ", canvasImageArray.shape); - console.log("Target-size: ", targetImageArray.shape); - - console.log("Step-size: ", [stepSizeX, stepSizeY]); - // 1. compute full-sized gray image CVUtils.computeGray(frame.data, _grayData); // 2. interpolate - for (y = 0; y < canvasSize.y; y++) { - for (x = 0; x < canvasSize.x; x++) { - canvasImageArray.set(x, y, (Interp2D(grayImageArray, x*stepSizeX, y*stepSizeY)) | 0); + for (y = 0; y < _canvasSize.y; y++) { + for (x = 0; x < _canvasSize.x; x++) { + _canvasImageArray.set(x, y, (Interp2D(_grayImageArray, x * _stepSizeX, y * _stepSizeY)) | 0); } } // targetImageArray must be equal to targetSize - if (targetImageArray.shape[0] !== targetSize.x || - targetImageArray.shape[1] !== targetSize.y) { + if (_targetImageArray.shape[0] !== _size.x || + _targetImageArray.shape[1] !== _size.y) { throw new Error("Shapes do not match!"); } + // 3. crop - for (y = 0; y < targetSize.y; y++) { - for (x = 0; x < targetSize.x; x++) { - _data[y*targetSize.x + x] = targetImageArray.get(x, y); + for (y = 0; y < _size.y; y++) { + for (x = 0; x < _size.x; x++) { + _data[y * _size.x + x] = _targetImageArray.get(x, y); } } },