From 0cb436e930b8d46f6fb2c68944be8d8801307750 Mon Sep 17 00:00:00 2001 From: Peter Braden Date: Tue, 26 Feb 2013 15:26:54 -0800 Subject: [PATCH] Changing ImageStream -> ImageDataStream and creating ImageStream. To pipe data into an image matrix, you now use ImageDataStream. ImageStream is now a stream that transforms image buffers into matrices. --- README.md | 25 ++++++++++++++++++++----- lib/opencv.js | 26 +++++++++++++++++++++----- test/unit.js | 4 ++-- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 02c39dc..8a24227 100755 --- a/README.md +++ b/README.md @@ -65,15 +65,30 @@ Or you can use opencv to read in image files. Supported formats are in the OpenC ... }) -If you need to pipe data into an image, you can use an imagestream: +If you need to pipe data into an image, you can use an ImageDataStream: + + var s = new cv.ImageDataStream() + + s.on('load', function(matrix){ + ... + }) + + fs.createReadStream('./examples/test.jpg').pipe(s); + +If however, you have a series of images, and you wish to stream them into a +stream of Matrices, you can use an ImageStream. Thus: var s = new cv.ImageStream() - s.on('load', function(matrix){ - ... - }) + s.on('data', function(matrix){ + ... + }) + + ardrone.createPngStream().pipe(s); + +Note: Each 'data' event into the ImageStream should be a complete image buffer. + - fs.createReadStream('./examples/test.jpg').pipe(s); #### Accessing Data diff --git a/lib/opencv.js b/lib/opencv.js index 0e635f6..5a155e6 100755 --- a/lib/opencv.js +++ b/lib/opencv.js @@ -40,21 +40,21 @@ matrix.inspect = function(){ } -cv.ImageStream = function(){ +cv.ImageDataStream = function(){ this.data = Buffers([]) this.writable = true } -util.inherits(cv.ImageStream, Stream); -var imagestream = cv.ImageStream.prototype; +util.inherits(cv.ImageDataStream, Stream); +var imagedatastream = cv.ImageDataStream.prototype; -imagestream.write = function(buf){ +imagedatastream.write = function(buf){ this.data.push(buf) return true; } -imagestream.end = function(b){ +imagedatastream.end = function(b){ var self = this; if (b) @@ -67,3 +67,19 @@ imagestream.end = function(b){ }); } + + +cv.ImageStream = function(){ + this.writable = true +} + +util.inherits(cv.ImageStream, Stream); +var imagestream = cv.ImageStream.prototype; + +imagestream.write = function(buf){ + cv.readImage(buf, function(err, matrix){ + self.emit('data', matrix); + }); +} + + diff --git a/test/unit.js b/test/unit.js index e02f6bf..b10ff9f 100755 --- a/test/unit.js +++ b/test/unit.js @@ -250,12 +250,12 @@ vows.describe('Smoke Tests OpenCV').addBatch({ } - , "ImageStream" : { + , "ImageDataStream" : { topic : require('../lib/opencv') , "pipe" : { topic : function(cv){ - var s = new cv.ImageStream() + var s = new cv.ImageDataStream() , self = this s.on('load', function(im){ assert.ok(im)