mirror of
https://github.com/peterbraden/node-opencv.git
synced 2026-02-01 16:06:49 +00:00
new interface
This commit is contained in:
parent
2f3d3cea6e
commit
bf3b39beb0
@ -3,7 +3,7 @@ var cv = require('./lib/opencv')
|
||||
|
||||
console.log(cv.version)
|
||||
|
||||
//console.log(cv.Image.prototype);
|
||||
//console.log(cv.readImage("./examples/t2.jpg").toBuffer());
|
||||
|
||||
|
||||
var im = new cv.Image("./examples/t2.jpg")
|
||||
|
||||
@ -28,6 +28,8 @@ Matrix::Init(Handle<Object> target) {
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "height", Height);
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "toBuffer", ToBuffer);
|
||||
|
||||
target->Set(String::NewSymbol("Matrix"), m->GetFunction());
|
||||
};
|
||||
|
||||
@ -122,3 +124,29 @@ Matrix::Height(const Arguments& args){
|
||||
return scope.Close(Number::New(self->mat.size().height));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Handle<Value>
|
||||
Matrix::ToBuffer(const v8::Arguments& args){
|
||||
HandleScope scope;
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
|
||||
std::vector<uchar> vec(0);
|
||||
std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90
|
||||
|
||||
cv::imencode(".jpg", self->mat, vec, params);
|
||||
|
||||
node::Buffer *buf = node::Buffer::New(vec.size());
|
||||
uchar* data = (uchar*) Buffer::Data(buf);
|
||||
memcpy(data, &vec[0], vec.size());
|
||||
|
||||
v8::Local<v8::Object> globalObj = v8::Context::GetCurrent()->Global();
|
||||
v8::Local<v8::Function> bufferConstructor = v8::Local<v8::Function>::Cast(globalObj->Get(v8::String::New("Buffer")));
|
||||
v8::Handle<v8::Value> constructorArgs[3] = {buf->handle_, v8::Integer::New(vec.size()), v8::Integer::New(0)};
|
||||
v8::Local<v8::Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs);
|
||||
|
||||
return scope.Close(actualBuffer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -59,6 +59,8 @@ class Matrix: public node::ObjectWrap {
|
||||
static Handle<Value> Width(const Arguments& args);
|
||||
static Handle<Value> Height(const Arguments& args);
|
||||
|
||||
static Handle<Value> ToBuffer(const Arguments& args);
|
||||
|
||||
|
||||
static Handle<Value> Empty(const Arguments& args);
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "OpenCV.h"
|
||||
#include "Matrix.h"
|
||||
|
||||
void
|
||||
OpenCV::Init(Handle<Object> target) {
|
||||
@ -9,6 +10,48 @@ OpenCV::Init(Handle<Object> target) {
|
||||
char out [21];
|
||||
int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION);
|
||||
target->Set(String::NewSymbol("version"), String::New(out, n));
|
||||
|
||||
NODE_SET_METHOD(target, "readImage", ReadImage);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Handle<Value>
|
||||
OpenCV::ReadImage(const Arguments &args) {
|
||||
HandleScope scope;
|
||||
|
||||
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
|
||||
Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
|
||||
cv::Mat mat;
|
||||
|
||||
|
||||
if (args[0]->IsNumber() && args[1]->IsNumber()){
|
||||
int width, height;
|
||||
|
||||
width = args[0]->Uint32Value();
|
||||
height = args[1]->Uint32Value();
|
||||
mat = *(new cv::Mat(width, height, CV_8UC1));
|
||||
|
||||
} else if (args[0]->IsString()) {
|
||||
|
||||
std::string filename = std::string(*v8::String::AsciiValue(args[0]->ToString()));
|
||||
mat = cv::imread(filename, -1);
|
||||
|
||||
} else if (Buffer::HasInstance(args[0])){
|
||||
uint8_t *buf = (uint8_t *) Buffer::Data(args[0]->ToObject());
|
||||
unsigned len = Buffer::Length(args[0]->ToObject());
|
||||
|
||||
cv::Mat *mbuf = new cv::Mat(len, 1, CV_8UC1, buf);
|
||||
mat = cv::imdecode(*mbuf, -1);
|
||||
|
||||
if (mat.empty()){
|
||||
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Error loading file")));
|
||||
}
|
||||
}
|
||||
|
||||
img->mat = mat;
|
||||
return scope.Close(im_h);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -24,6 +24,9 @@ using namespace node;
|
||||
class OpenCV: public node::ObjectWrap{
|
||||
public:
|
||||
static void Init(Handle<Object> target);
|
||||
|
||||
static Handle<Value> ReadImage(const v8::Arguments&);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -93,6 +93,15 @@ vows.describe('Smoke Tests OpenCV').addBatch({
|
||||
assert.equal(new cv.Matrix().empty(), true);
|
||||
}
|
||||
|
||||
, "toBuffer": function(cv){
|
||||
var buf = fs.readFileSync('./examples/mona.jpg')
|
||||
, im = cv.readImage(buf.slice(0))
|
||||
, buf0 = im.toBuffer()
|
||||
|
||||
assert.ok(buf0);
|
||||
//assert.equal(buf.toString('base64'), buf0.toString('base64'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -100,42 +109,25 @@ vows.describe('Smoke Tests OpenCV').addBatch({
|
||||
, "Image" : {
|
||||
topic : require('../lib/opencv')
|
||||
|
||||
, "constructor(filename)": function(cv){
|
||||
assert.ok(new cv.Image("./examples/mona.jpg"))
|
||||
, ".readImage from file": function(cv){
|
||||
var im = cv.readImage("./examples/mona.jpg")
|
||||
assert.ok(im)
|
||||
assert.equal(im.width(), 500);
|
||||
assert.equal(im.height(), 756)
|
||||
assert.equal(im.empty(), false)
|
||||
}
|
||||
|
||||
, "constructor(buffer)" : function(cv){
|
||||
var im = new cv.Image(fs.readFileSync('./examples/mona.jpg'))
|
||||
, ".readImage from buffer" : function(cv){
|
||||
var im = cv.readImage(fs.readFileSync('./examples/mona.jpg'))
|
||||
assert.ok(im);
|
||||
assert.equal(im.width(), 500);
|
||||
assert.equal(im.height(), 756)
|
||||
assert.equal(im.empty(), false)
|
||||
}
|
||||
|
||||
|
||||
, "inherits from matrix": function(cv){
|
||||
assert.equal(new cv.Image("./examples/mona.jpg").empty(), false)
|
||||
}
|
||||
|
||||
, ".width / .height" : function(cv){
|
||||
assert.equal(new cv.Image("./examples/mona.jpg").width(), 500)
|
||||
assert.equal(new cv.Image("./examples/mona.jpg").height(), 756)
|
||||
}
|
||||
|
||||
, ".ellipse": function(cv){
|
||||
assert.equal(new cv.Image("./examples/mona.jpg").ellipse(10, 10, 10, 10), undefined)
|
||||
}
|
||||
|
||||
, "toBuffer": function(cv){
|
||||
var buf = fs.readFileSync('./examples/mona.jpg')
|
||||
, im = new cv.Image(buf.slice(0))
|
||||
, buf0 = im.toBuffer()
|
||||
assert.ok(buf0);
|
||||
|
||||
//assert.equal(buf.toString('base64'), buf0.toString('base64'));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
, "CascadeClassifier": {
|
||||
topic : require('../lib/opencv')
|
||||
|
||||
@ -161,5 +153,5 @@ vows.describe('Smoke Tests OpenCV').addBatch({
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
}).run();
|
||||
Loading…
x
Reference in New Issue
Block a user