mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
try catch, macros etc
This commit is contained in:
parent
cbd8a1cdb1
commit
406eef45a2
14
TODO
14
TODO
@ -1,5 +1,3 @@
|
||||
Get rid of Image
|
||||
- Replace with operations on Matrix and cv helper js functions
|
||||
|
||||
|
||||
- Matrix doesn't need to be eventemitter:
|
||||
@ -10,17 +8,9 @@ Get rid of Image
|
||||
|
||||
############ Ideas #####################
|
||||
|
||||
var mat = cv.readImage('foo.jpg');
|
||||
|
||||
//shortcut for cascade classifier
|
||||
mat.detectFaces('data.xml', function(err, faces){
|
||||
for (var i=0;i<faces.length; i++){
|
||||
var x = faces[i]
|
||||
mat.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
|
||||
}
|
||||
});
|
||||
|
||||
var mat = cv.readImageSync('foo.jpg');
|
||||
|
||||
cv.readImage('foo.jpg', function(err, mat){});
|
||||
|
||||
|
||||
var s = new opencv.ImageStream()
|
||||
|
||||
@ -12,6 +12,7 @@ The matrix is one of opencv's most core datatypes.
|
||||
|
||||
*/
|
||||
var matrix = opencv.Matrix.prototype;
|
||||
|
||||
matrix.__proto__ = EventEmitter.prototype;
|
||||
|
||||
matrix.faceDetect = function(classifier, opts, cb){
|
||||
|
||||
@ -27,7 +27,6 @@ Matrix::Init(Handle<Object> target) {
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "width", Width);
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "height", Height);
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "toBuffer", ToBuffer);
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "ellipse", Ellipse);
|
||||
NODE_SET_PROTOTYPE_METHOD(constructor, "save", Save);
|
||||
@ -64,17 +63,15 @@ Matrix::Matrix(int w, int h): ObjectWrap() {
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Empty(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
return scope.Close(Boolean::New(self->mat.empty()));
|
||||
}
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Get(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
int i = args[0]->IntegerValue();
|
||||
int j = args[1]->IntegerValue();
|
||||
|
||||
@ -84,9 +81,8 @@ Matrix::Get(const Arguments& args){
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Set(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
int i = args[0]->IntegerValue();
|
||||
int j = args[1]->IntegerValue();
|
||||
double val = args[2]->NumberValue();
|
||||
@ -99,9 +95,8 @@ Matrix::Set(const Arguments& args){
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Size(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
v8::Local<v8::Array> arr = v8::Array::New(2);
|
||||
arr->Set(0, Number::New(self->mat.size().height));
|
||||
arr->Set(1, Number::New(self->mat.size().width));
|
||||
@ -112,17 +107,15 @@ Matrix::Size(const Arguments& args){
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Width(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
return scope.Close(Number::New(self->mat.size().width));
|
||||
}
|
||||
|
||||
Handle<Value>
|
||||
Matrix::Height(const Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
return scope.Close(Number::New(self->mat.size().height));
|
||||
}
|
||||
|
||||
@ -130,9 +123,7 @@ Matrix::Height(const Arguments& args){
|
||||
|
||||
Handle<Value>
|
||||
Matrix::ToBuffer(const v8::Arguments& args){
|
||||
HandleScope scope;
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
std::vector<uchar> vec(0);
|
||||
std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90
|
||||
@ -155,9 +146,8 @@ Matrix::ToBuffer(const v8::Arguments& args){
|
||||
// ellipse(x, y, wid, height, angle, startangle, endangle, color, thickness, linetype, shift)
|
||||
Handle<Value>
|
||||
Matrix::Ellipse(const v8::Arguments& args){
|
||||
HandleScope scope;
|
||||
SETUP_FUNCTION(Matrix)
|
||||
|
||||
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
|
||||
int x = args[0]->Uint32Value();
|
||||
int y = args[1]->Uint32Value();
|
||||
int width = args[2]->Uint32Value();
|
||||
|
||||
22
src/Matrix.h
22
src/Matrix.h
@ -52,19 +52,15 @@ class Matrix: public node::ObjectWrap {
|
||||
*/
|
||||
|
||||
|
||||
static Handle<Value> Get(const Arguments& args); // at
|
||||
static Handle<Value> Set(const Arguments& args);
|
||||
|
||||
static Handle<Value> Size(const Arguments& args);
|
||||
static Handle<Value> Width(const Arguments& args);
|
||||
static Handle<Value> Height(const Arguments& args);
|
||||
|
||||
static Handle<Value> ToBuffer(const Arguments& args);
|
||||
|
||||
static Handle<Value> Ellipse(const Arguments& args);
|
||||
static Handle<Value> Empty(const Arguments& args);
|
||||
static Handle<Value> Save(const Arguments& args);
|
||||
|
||||
JSFUNC(Get) // at
|
||||
JSFUNC(Set)
|
||||
JSFUNC(Size)
|
||||
JSFUNC(Width)
|
||||
JSFUNC(Height)
|
||||
JSFUNC(ToBuffer)
|
||||
JSFUNC(Ellipse)
|
||||
JSFUNC(Empty)
|
||||
JSFUNC(Save)
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -20,38 +20,45 @@ OpenCV::Init(Handle<Object> target) {
|
||||
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()) {
|
||||
try{
|
||||
|
||||
std::string filename = std::string(*v8::String::AsciiValue(args[0]->ToString()));
|
||||
mat = cv::imread(filename, -1);
|
||||
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
|
||||
Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
|
||||
cv::Mat mat;
|
||||
|
||||
} 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")));
|
||||
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);
|
||||
img->mat = mat;
|
||||
return scope.Close(im_h);
|
||||
|
||||
} catch( cv::Exception& e ){
|
||||
const char* err_msg = e.what();
|
||||
return v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg)));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -20,6 +20,14 @@ using namespace node;
|
||||
Local<Function> VAR = Local<Function>::Cast(args[I]);
|
||||
|
||||
|
||||
#define SETUP_FUNCTION(TYP) \
|
||||
HandleScope scope; \
|
||||
TYP *self = ObjectWrap::Unwrap<TYP>(args.This());
|
||||
|
||||
#define JSFUNC(NAME) \
|
||||
static Handle<Value> NAME(const Arguments& args);
|
||||
|
||||
|
||||
|
||||
class OpenCV: public node::ObjectWrap{
|
||||
public:
|
||||
|
||||
@ -51,19 +51,15 @@ VideoCaptureWrap::VideoCaptureWrap(int device){
|
||||
|
||||
Handle<Value>
|
||||
VideoCaptureWrap::GetFrame(const Arguments &args) {
|
||||
HandleScope scope;
|
||||
|
||||
VideoCaptureWrap *v = ObjectWrap::Unwrap<VideoCaptureWrap>(args.This());
|
||||
SETUP_FUNCTION(VideoCaptureWrap)
|
||||
|
||||
cv::Mat frame;
|
||||
v->cap.retrieve(frame);
|
||||
self->cap.retrieve(frame);
|
||||
|
||||
|
||||
|
||||
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
|
||||
Matrix *im = ObjectWrap::Unwrap<Matrix>(im_h);
|
||||
im->mat = frame;
|
||||
return scope.Close(im_h);
|
||||
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
|
||||
Matrix *im = ObjectWrap::Unwrap<Matrix>(im_h);
|
||||
im->mat = frame;
|
||||
return scope.Close(im_h);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user