|
| 1 | +/* jshint undef: true, unused: true, browser:true, devel: true */ |
| 2 | +/* global define */ |
| 3 | + |
| 4 | +define(["cv_utils"], function(CVUtils) { |
| 5 | + "use strict"; |
| 6 | + |
| 7 | + var Ndarray = require("ndarray"), |
| 8 | + Interp2D = require("ndarray-linear-interpolate").d2; |
| 9 | + |
| 10 | + var FrameGrabber = {}; |
| 11 | + |
| 12 | + FrameGrabber.create = function(inputStream) { |
| 13 | + var _that = {}, |
| 14 | + _streamConfig = inputStream.getConfig(), |
| 15 | + _video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()), |
| 16 | + _canvasSize = inputStream.getCanvasSize(), |
| 17 | + _size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()), |
| 18 | + _topRight = inputStream.getTopRight(), |
| 19 | + _data = new Uint8Array(_size.x * _size.y), |
| 20 | + _grayData = new Uint8Array(_video_size.x * _video_size.y), |
| 21 | + _canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y), |
| 22 | + _grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0), |
| 23 | + _canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0), |
| 24 | + _targetImageArray = _canvasImageArray.hi(_topRight.x + _size.x, _topRight.y + _size.y).lo(_topRight.x, _topRight.y), |
| 25 | + _stepSizeX = _video_size.x/_canvasSize.x, |
| 26 | + _stepSizeY = _video_size.y/_canvasSize.y; |
| 27 | + |
| 28 | + console.log("FrameGrabber", JSON.stringify({ |
| 29 | + videoSize: _grayImageArray.shape, |
| 30 | + canvasSize: _canvasImageArray.shape, |
| 31 | + stepSize: [_stepSizeX, _stepSizeY], |
| 32 | + size: _targetImageArray.shape, |
| 33 | + topRight: _topRight |
| 34 | + })); |
| 35 | + |
| 36 | + /** |
| 37 | + * Uses the given array as frame-buffer |
| 38 | + */ |
| 39 | + _that.attachData = function(data) { |
| 40 | + _data = data; |
| 41 | + }; |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the used frame-buffer |
| 45 | + */ |
| 46 | + _that.getData = function() { |
| 47 | + return _data; |
| 48 | + }; |
| 49 | + |
| 50 | + /** |
| 51 | + * Fetches a frame from the input-stream and puts into the frame-buffer. |
| 52 | + * The image-data is converted to gray-scale and then half-sampled if configured. |
| 53 | + */ |
| 54 | + _that.grab = function() { |
| 55 | + var frame = inputStream.getFrame(); |
| 56 | + |
| 57 | + if (frame) { |
| 58 | + this.scaleAndCrop(frame); |
| 59 | + return true; |
| 60 | + } else { |
| 61 | + return false; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + _that.scaleAndCrop = function(frame) { |
| 66 | + var x, |
| 67 | + y; |
| 68 | + |
| 69 | + // 1. compute full-sized gray image |
| 70 | + CVUtils.computeGray(frame.data, _grayData); |
| 71 | + |
| 72 | + // 2. interpolate |
| 73 | + for (y = 0; y < _canvasSize.y; y++) { |
| 74 | + for (x = 0; x < _canvasSize.x; x++) { |
| 75 | + _canvasImageArray.set(x, y, (Interp2D(_grayImageArray, x * _stepSizeX, y * _stepSizeY)) | 0); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // targetImageArray must be equal to targetSize |
| 80 | + if (_targetImageArray.shape[0] !== _size.x || |
| 81 | + _targetImageArray.shape[1] !== _size.y) { |
| 82 | + throw new Error("Shapes do not match!"); |
| 83 | + } |
| 84 | + |
| 85 | + // 3. crop |
| 86 | + for (y = 0; y < _size.y; y++) { |
| 87 | + for (x = 0; x < _size.x; x++) { |
| 88 | + _data[y * _size.x + x] = _targetImageArray.get(x, y); |
| 89 | + } |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + _that.getSize = function() { |
| 94 | + return _size; |
| 95 | + }; |
| 96 | + |
| 97 | + return _that; |
| 98 | + }; |
| 99 | + |
| 100 | + return (FrameGrabber); |
| 101 | +}); |
0 commit comments