Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added videocapture working (libuv), some examples, more methods to Ma…

…trix and implement of libuv for detectobjects
  • Loading branch information...
commit b73174c00fbce67bd801d6707590d42c7cec9535 1 parent bc91847
@raichuk raichuk authored
View
0  .gitignore 100644 → 100755
File mode changed
View
0  .travis.yml 100644 → 100755
File mode changed
View
0  README.md 100644 → 100755
File mode changed
View
2  TODO 100644 → 100755
@@ -42,4 +42,4 @@ cv.loadImage('test.jpg', function(err, im){
http://www.athile.net/library/wiki/index.php?title=Library/V8/Tutorial#Wrapping_a_Javascript_function_as_a_std::function.3C.3E
-https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/
+https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/
View
0  examples/face-proxy.js 100644 → 100755
File mode changed
View
0  examples/haarcascade_frontalface_alt.xml 100644 → 100755
File mode changed
View
BIN  examples/mona.jpg
Deleted file not rendered
View
0  index.html 100644 → 100755
File mode changed
View
0  lib/bindings.js 100644 → 100755
File mode changed
View
2  lib/opencv.js 100644 → 100755
@@ -1,5 +1,5 @@
var Stream = require('stream').Stream
- , Buffers = require('buffers')
+ , Buffers = require('buffer')
, util = require('util');
var bindings = require('./bindings')
View
0  package.json 100644 → 100755
File mode changed
View
2  smoketest.js
@@ -14,4 +14,4 @@ var cv = require('./lib/opencv')
im.save('./out.jpg');
});
-})
+})
View
39 src/CascadeClassifierWrap.cc 100644 → 100755
@@ -3,6 +3,8 @@
#include "Matrix.h"
+void AsyncDetectMultiScale(uv_work_t *req);
+void AfterAsyncDetectMultiScale(uv_work_t *req);
Persistent<FunctionTemplate> CascadeClassifierWrap::constructor;
@@ -61,6 +63,8 @@ struct classifier_baton_t {
int minh;
int sleep_for;
std::vector<cv::Rect> res;
+
+ uv_work_t request;
};
@@ -102,42 +106,47 @@ CascadeClassifierWrap::DetectMultiScale(const v8::Arguments& args){
baton->minw = minw;
baton->minh = minh;
baton->sleep_for = 1;
- self->Ref();
+ baton->request.data = baton;
+// self->Ref();
+
+// eio_custom(EIO_DetectMultiScale, EIO_PRI_DEFAULT, EIO_AfterDetectMultiScale, baton);
+// ev_ref(EV_DEFAULT_UC);
- eio_custom(EIO_DetectMultiScale, EIO_PRI_DEFAULT, EIO_AfterDetectMultiScale, baton);
- ev_ref(EV_DEFAULT_UC);
+ uv_queue_work(uv_default_loop(), &baton->request, AsyncDetectMultiScale, AfterAsyncDetectMultiScale);
return Undefined();
}
-
-void
-CascadeClassifierWrap::EIO_DetectMultiScale(eio_req *req){
- classifier_baton_t *baton = static_cast<classifier_baton_t *>(req->data);
- sleep(baton->sleep_for);
+void AsyncDetectMultiScale(uv_work_t *req) {
+ classifier_baton_t *baton = static_cast<classifier_baton_t *>(req->data);
+// sleep(baton->sleep_for);
std::vector<cv::Rect> objects;
cv::Mat gray;
- cvtColor( baton->im->mat, gray, CV_BGR2GRAY );
- equalizeHist( gray, gray);
+ if(baton->im->mat.channels() != 1)
+ cvtColor(baton->im->mat, gray, CV_BGR2GRAY);
+
+ equalizeHist( gray, gray);
baton->cc->cc.detectMultiScale(gray, objects, baton->scale, baton->neighbors, 0, cv::Size(baton->minw, baton->minh));
baton->res = objects;
+
+
}
-int
-CascadeClassifierWrap::EIO_AfterDetectMultiScale(eio_req *req){
+void AfterAsyncDetectMultiScale(uv_work_t *req) {
+
HandleScope scope;
classifier_baton_t *baton = static_cast<classifier_baton_t *>(req->data);
- ev_unref(EV_DEFAULT_UC);
- baton->cc->Unref();
+// ev_unref(EV_DEFAULT_UC);
+// baton->cc->Unref();
Local<Value> argv[2];
@@ -169,6 +178,6 @@ CascadeClassifierWrap::EIO_AfterDetectMultiScale(eio_req *req){
delete baton;
- return 0;
+// return 0;
}
View
2  src/CascadeClassifierWrap.h 100644 → 100755
@@ -16,4 +16,4 @@ class CascadeClassifierWrap: public node::ObjectWrap {
static void EIO_DetectMultiScale(eio_req *req);
static int EIO_AfterDetectMultiScale(eio_req *req);
-};
+};
View
480 src/Matrix.cc 100644 → 100755
@@ -1,221 +1,473 @@
+#include "Contours.h"
#include "Matrix.h"
#include "OpenCV.h"
v8::Persistent<FunctionTemplate> Matrix::constructor;
+cv::Scalar setColor(Local<Object> objColor);
+
+//
void
Matrix::Init(Handle<Object> target) {
- HandleScope scope;
+ HandleScope scope;
+
+ //Class
+ v8::Local<v8::FunctionTemplate> m = v8::FunctionTemplate::New(New);
+ m->SetClassName(v8::String::NewSymbol("Matrix"));
+
+ // Constructor
+ constructor = Persistent<FunctionTemplate>::New(m);
+ constructor->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor->SetClassName(String::NewSymbol("Matrix"));
- //Class
- v8::Local<v8::FunctionTemplate> m = v8::FunctionTemplate::New(New);
- m->SetClassName(v8::String::NewSymbol("Matrix"));
+ // Prototype
+ Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
- // Constructor
- constructor = Persistent<FunctionTemplate>::New(m);
- constructor->InstanceTemplate()->SetInternalFieldCount(1);
- constructor->SetClassName(String::NewSymbol("Matrix"));
- // Prototype
- Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
+ NODE_SET_PROTOTYPE_METHOD(constructor, "row", Row);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "col", Col);
- NODE_SET_PROTOTYPE_METHOD(constructor, "row", Row);
- NODE_SET_PROTOTYPE_METHOD(constructor, "col", Col);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "get", Get);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "set", Set);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "width", Width);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "height", Height);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "channels", Channels);
+ 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);
- NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty);
- NODE_SET_PROTOTYPE_METHOD(constructor, "get", Get);
- NODE_SET_PROTOTYPE_METHOD(constructor, "set", Set);
- 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);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "convertGrayscale", ConvertGrayscale);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "convertHSVscale", ConvertHSVscale);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "copy", Copy);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "ptr", Ptr);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "addWeighted", AddWeighted);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "split", Split);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "bla", Bla);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "canny", Canny);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "dilate", Dilate);
- NODE_SET_METHOD(constructor, "Eye", Eye);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "findContours", FindContours);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "drawContour", DrawContour);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "drawAllContours", DrawAllContours);
+ NODE_SET_METHOD(constructor, "Eye", Eye);
- target->Set(String::NewSymbol("Matrix"), m->GetFunction());
-};
+
+ target->Set(String::NewSymbol("Matrix"), m->GetFunction());
+};
Handle<Value>
Matrix::New(const Arguments &args) {
- HandleScope scope;
+ HandleScope scope;
+
+ if (args.This()->InternalFieldCount() == 0)
+ return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new")));
- if (args.This()->InternalFieldCount() == 0)
- return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new")));
+ Matrix *mat;
- Matrix *mat;
+ if (args.Length() == 0){
+ mat = new Matrix;
+ } else if (args.Length() == 2 && args[0]->IsInt32() && args[1]->IsInt32()){
+ mat = new Matrix(args[0]->IntegerValue(), args[1]->IntegerValue());
+ }
- if (args.Length() == 0){
- mat = new Matrix;
- } else if (args.Length() == 2 && args[0]->IsInt32() && args[1]->IsInt32()){
- mat = new Matrix(args[0]->IntegerValue(), args[1]->IntegerValue());
- }
- mat->Wrap(args.Holder());
- return scope.Close(args.Holder());
+ mat->Wrap(args.Holder());
+ return scope.Close(args.Holder());
}
Matrix::Matrix(): ObjectWrap() {
- mat = cv::Mat();
+ mat = cv::Mat();
}
Matrix::Matrix(int w, int h): ObjectWrap() {
- mat = cv::Mat(w, h, CV_64FC1);
+
+ //TODO:Parametrizar esto
+ mat = cv::Mat(h, w, CV_8UC3);
+
}
-Handle<Value>
+
+Handle<Value>
Matrix::Empty(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- return scope.Close(Boolean::New(self->mat.empty()));
+ return scope.Close(Boolean::New(self->mat.empty()));
}
-Handle<Value>
+Handle<Value>
Matrix::Get(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- int i = args[0]->IntegerValue();
- int j = args[1]->IntegerValue();
+ int i = args[0]->IntegerValue();
+ int j = args[1]->IntegerValue();
- return scope.Close(Number::New(self->mat.at<double>(i,j)));
+ return scope.Close(Number::New(self->mat.at<double>(i,j)));
}
Handle<Value>
Matrix::Set(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
+
+ int i = args[0]->IntegerValue();
+ int j = args[1]->IntegerValue();
+ double val = args[2]->NumberValue();
- int i = args[0]->IntegerValue();
- int j = args[1]->IntegerValue();
- double val = args[2]->NumberValue();
+ if(args.Length() == 4) {
+ self->mat.at<cv::Vec3b>(i,j)[args[3]->NumberValue()] = val;
- self->mat.at<double>(i,j) = val;
+ } else if(args.Length() == 3) {
+ self->mat.at<cv::Vec3b>(i,j)[0] = val;
+ self->mat.at<cv::Vec3b>(i,j)[1] = val;
+ self->mat.at<cv::Vec3b>(i,j)[2] = val;
- return scope.Close(Undefined());
+ } else {
+ return ThrowException(Exception::TypeError(String::New("Invalid number of arguments")));
+ }
+
+ return scope.Close(Undefined());
}
Handle<Value>
Matrix::Size(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- 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));
+ 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));
- return scope.Close(arr);
+ return scope.Close(arr);
}
Handle<Value>
Matrix::Row(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- int width = self->mat.size().width;
- int j = args[0]->IntegerValue();
- v8::Local<v8::Array> arr = v8::Array::New(width);
+ int width = self->mat.size().width;
+ int j = args[0]->IntegerValue();
+ v8::Local<v8::Array> arr = v8::Array::New(width);
+
+ for (int i=0; i<width; i++)
+ arr->Set(i, Number::New(self->mat.at<double>(i, j)));
- for (int i=0; i<width; i++){
- arr->Set(i, Number::New(self->mat.at<double>(i, j)));
- }
return scope.Close(arr);
}
Handle<Value>
Matrix::Col(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- int width = self->mat.size().width;
- int j = args[0]->IntegerValue();
- v8::Local<v8::Array> arr = v8::Array::New(width);
+ int width = self->mat.size().width;
+ int j = args[0]->IntegerValue();
+ v8::Local<v8::Array> arr = v8::Array::New(width);
- for (int i=0; i<width; i++){
- arr->Set(i, Number::New(self->mat.at<double>(j, i)));
- }
- return scope.Close(arr);
+ for (int i=0; i<width; i++)
+ arr->Set(i, Number::New(self->mat.at<double>(j, i)));
+
+ return scope.Close(arr);
}
Handle<Value>
Matrix::Width(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- return scope.Close(Number::New(self->mat.size().width));
+ return scope.Close(Number::New(self->mat.size().width));
}
Handle<Value>
Matrix::Height(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- return scope.Close(Number::New(self->mat.size().height));
-}
+ return scope.Close(Number::New(self->mat.size().height));
+}
+
+Handle<Value>
+Matrix::Channels(const Arguments& args){
+ SETUP_FUNCTION(Matrix)
+ return scope.Close(Number::New(self->mat.channels()));
+}
Handle<Value>
Matrix::ToBuffer(const v8::Arguments& args){
- SETUP_FUNCTION(Matrix)
-
- std::vector<uchar> vec(0);
- std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90
+ SETUP_FUNCTION(Matrix)
- cv::imencode(".jpg", self->mat, vec, params);
+ std::vector<uchar> vec(0);
+ std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90
- node::Buffer *buf = node::Buffer::New(vec.size());
- uchar* data = (uchar*) Buffer::Data(buf);
- memcpy(data, &vec[0], vec.size());
+ cv::imencode(".jpg", self->mat, vec, params);
- 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);
+ node::Buffer *buf = node::Buffer::New(vec.size());
+ uchar* data = (uchar*) Buffer::Data(buf);
+ memcpy(data, &vec[0], vec.size());
- return scope.Close(actualBuffer);
-}
+ 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);
+}
- // ellipse(x, y, wid, height, angle, startangle, endangle, color, thickness, linetype, shift)
Handle<Value>
Matrix::Ellipse(const v8::Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(Matrix)
- int x = args[0]->Uint32Value();
- int y = args[1]->Uint32Value();
- int width = args[2]->Uint32Value();
- int height = args[3]->Uint32Value();
+ int x = args[0]->Uint32Value();
+ int y = args[1]->Uint32Value();
+ int width = args[2]->Uint32Value();
+ int height = args[3]->Uint32Value();
- cv::ellipse(self->mat, cv::Point(x, y), cv::Size(width, height), 0, 0, 360, cv::Scalar( 255, 0, 255 ), 4, 8, 0);
- return scope.Close(v8::Null());
+ cv::ellipse(self->mat, cv::Point(x, y), cv::Size(width, height), 0, 0, 360, cv::Scalar( 255, 0, 255 ), 4, 8, 0);
+ return scope.Close(v8::Null());
}
Handle<Value>
Matrix::Save(const v8::Arguments& args){
- HandleScope scope;
+ HandleScope scope;
- if (!args[0]->IsString())
- return ThrowException(Exception::TypeError(String::New("filename required")));
-
- Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
- String::AsciiValue filename(args[0]);
- int res = cv::imwrite(*filename, self->mat);
- return scope.Close(Number::New(res));
+ if (!args[0]->IsString())
+ return ThrowException(Exception::TypeError(String::New("filename required")));
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ String::AsciiValue filename(args[0]);
+ int res = cv::imwrite(*filename, self->mat);
+ return scope.Close(Number::New(res));
}
+
Handle<Value>
Matrix::Eye(const v8::Arguments& args){
- HandleScope scope;
+ HandleScope scope;
+
+ int w = args[0]->Uint32Value();
+ int h = args[1]->Uint32Value();
+
+ Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
+ Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
+ cv::Mat mat = cv::Mat::eye(w, h, CV_64FC1);
+
+ img->mat = mat;
+ return scope.Close(im_h);
+}
+
+
+Handle<Value>
+Matrix::ConvertGrayscale(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ if(self->mat.channels() != 3)
+ return ThrowException(String::New("Image is no 3-channel"));
+
+ cv::Mat gray;
+
+ cv::cvtColor(self->mat, gray, CV_BGR2GRAY);
+ gray.copyTo(self->mat);
+
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::ConvertHSVscale(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ if(self->mat.channels() != 3)
+ return ThrowException(String::New("Image is no 3-channel"));
+
+ cv::Mat hsv;
+
+ cv::cvtColor(self->mat, hsv, CV_BGR2HSV);
+ hsv.copyTo(self->mat);
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::Copy(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+
+ Local<Object> img_to_return = Matrix::constructor->GetFunction()->NewInstance();
+ Matrix *img = ObjectWrap::Unwrap<Matrix>(img_to_return);
+ self->mat.copyTo(img->mat);
+
+ return scope.Close(img_to_return);
+}
+
+
+Handle<Value>
+Matrix::Ptr(const v8::Arguments& args) {
+ HandleScope scope;
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ int line = args[0]->Uint32Value();
+
+
+ char* data = self->mat.ptr<char>(line);
+ //uchar* data = self->mat.data;
+
+/*
+ char *mydata = "Random raw data\0";
+*/
+ node::Buffer *return_buffer = Buffer::New((char *)data, self->mat.step);
+ return scope.Close( return_buffer->handle_ );
+
+// return Undefined();
+}
+
+Handle<Value>
+Matrix::Bla(const v8::Arguments& args) {
+ HandleScope scope;
+ int i = args[1]->Uint32Value();
+
+ int div = 64;
+
+ if (Buffer::HasInstance(args[0])){
+
+ char *buf = (char *) Buffer::Data(args[0]->ToObject());
+ unsigned len = Buffer::Length(args[0]->ToObject());
+
+ }
+ return Undefined();
+
+}
+
+
+Handle<Value>
+Matrix::AddWeighted(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+
+ Matrix *src1 = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
+ Matrix *src2 = ObjectWrap::Unwrap<Matrix>(args[2]->ToObject());
+
+ float alpha = args[1]->NumberValue();
+ float beta = args[3]->NumberValue();
+ int gamma = 0;
+
+ cv::addWeighted(src1->mat, alpha, src2->mat, beta, gamma, self->mat);
+
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::Split(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::Canny(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ int lowThresh = args[0]->NumberValue();
+ int highThresh = args[1]->NumberValue();
+
+ cv::Canny(self->mat, self->mat, lowThresh, highThresh);
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::Dilate(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ int niters = args[0]->NumberValue();
+
+ cv::dilate(self->mat, self->mat, cv::Mat(), cv::Point(-1, -1), niters);
+
+ return scope.Close(v8::Null());
+}
+
+
+Handle<Value>
+Matrix::FindContours(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+
+ Local<Object> conts_to_return= Contour::constructor->GetFunction()->NewInstance();
+ Contour *contours = ObjectWrap::Unwrap<Contour>(conts_to_return);
+
+ cv::findContours(self->mat, contours->contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
+
+ return scope.Close(conts_to_return);
+
+}
+
+
+Handle<Value>
+Matrix::DrawContour(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ Contour *cont = ObjectWrap::Unwrap<Contour>(args[0]->ToObject());
+ int pos = args[1]->NumberValue();
+
+ cv::Scalar color(0, 0, 255);
+
+ if(args[2]->IsArray()) {
+ Local<Object> objColor = args[2]->ToObject();
+ color = setColor(objColor);
+ }
+
+ cv::drawContours(self->mat, cont->contours, pos, color, 1);
+
+ return Undefined();
+}
+
+
+Handle<Value>
+Matrix::DrawAllContours(const v8::Arguments& args) {
+ HandleScope scope;
+
+ Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
+ Contour *cont = ObjectWrap::Unwrap<Contour>(args[0]->ToObject());
+
+ cv::Scalar color(0, 0, 255);
+
+ if(args[1]->IsArray()) {
+ Local<Object> objColor = args[1]->ToObject();
+ color = setColor(objColor);
+ }
+
+ cv::drawContours(self->mat, cont->contours, -1, color, 1);
+
+ return Undefined();
+}
+
+cv::Scalar setColor(Local<Object> objColor) {
- int w = args[0]->Uint32Value();
- int h = args[1]->Uint32Value();
+ Local<Value> valB = objColor->Get(0);
+ Local<Value> valG = objColor->Get(1);
+ Local<Value> valR = objColor->Get(2);
- Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
- Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
- cv::Mat mat = cv::Mat::eye(w, h, CV_64FC1);
+ cv::Scalar color = cv::Scalar(valB->IntegerValue(), valG->IntegerValue(), valR->IntegerValue());
+ return color;
- img->mat = mat;
- return scope.Close(im_h);
}
View
15 src/Matrix.h 100644 → 100755
@@ -60,10 +60,25 @@ class Matrix: public node::ObjectWrap {
JSFUNC(Size)
JSFUNC(Width)
JSFUNC(Height)
+ JSFUNC(Channels)
JSFUNC(ToBuffer)
JSFUNC(Ellipse)
JSFUNC(Empty)
JSFUNC(Save)
+ JSFUNC(ConvertGrayscale)
+ JSFUNC(ConvertHSVscale)
+ JSFUNC(Copy)
+ JSFUNC(Ptr)
+ JSFUNC(Bla)
+ JSFUNC(AddWeighted)
+ JSFUNC(Split)
+ JSFUNC(Canny)
+ JSFUNC(Dilate)
+
+ JSFUNC(FindContours)
+ JSFUNC(DrawContour)
+ JSFUNC(DrawAllContours)
+
};
View
0  src/OpenCV.cc 100644 → 100755
File mode changed
View
1  src/OpenCV.h 100644 → 100755
@@ -10,6 +10,7 @@
#include <highgui.h>
#include <string.h>
+
using namespace v8;
using namespace node;
View
0  src/Point.cc 100644 → 100755
File mode changed
View
0  src/Point.h 100644 → 100755
File mode changed
View
114 src/VideoCaptureWrap.cc 100644 → 100755
@@ -2,64 +2,114 @@
#include "Matrix.h"
#include "OpenCV.h"
+
+void AsyncRead(uv_work_t *req);
+void AfterAsyncRead(uv_work_t *req);
+
v8::Persistent<FunctionTemplate> VideoCaptureWrap::constructor;
+struct videocapture_baton {
+
+ Persistent<Function> cb;
+ VideoCaptureWrap *vc;
+ Matrix *im;
+
+ uv_work_t request;
+};
+
+
void
VideoCaptureWrap::Init(Handle<Object> target) {
HandleScope scope;
- // Constructor
- constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(VideoCaptureWrap::New));
- constructor->InstanceTemplate()->SetInternalFieldCount(1);
- constructor->SetClassName(String::NewSymbol("VideoCapture"));
+ // Constructor
+ constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(VideoCaptureWrap::New));
+ constructor->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor->SetClassName(String::NewSymbol("VideoCapture"));
+
+ // Prototype
+ Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
- // Prototype
- Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
-
- NODE_SET_PROTOTYPE_METHOD(constructor, "getFrame", GetFrame);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "read", Read);
- target->Set(String::NewSymbol("VideoCapture"), constructor->GetFunction());
+ target->Set(String::NewSymbol("VideoCapture"), constructor->GetFunction());
};
Handle<Value>
VideoCaptureWrap::New(const Arguments &args) {
- HandleScope scope;
+ HandleScope scope;
if (args.This()->InternalFieldCount() == 0)
- return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new")));
+ return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new")));
- VideoCaptureWrap *v;
+ VideoCaptureWrap *v;
+
+ if (args[0]->IsNumber()){
+ v = new VideoCaptureWrap(args[0]->NumberValue());
+ } else {}
- if (args[0]->IsNumber()){
- v = new VideoCaptureWrap(args[0]->NumberValue());
- } else {}
- v->Wrap(args.This());
- return args.This();
-}
+ v->Wrap(args.This());
+
+ return args.This();
+}
VideoCaptureWrap::VideoCaptureWrap(int device){
- HandleScope scope;
- cv::VideoCapture cap(device);
+ HandleScope scope;
+ cap.open(device);
- if(!cap.isOpened()){
- ThrowException(Exception::Error(String::New("Camera could not be opened")));
- }
+ if(!cap.isOpened()){
+ ThrowException(Exception::Error(String::New("Camera could not be opened")));
+ }
}
Handle<Value>
-VideoCaptureWrap::GetFrame(const Arguments &args) {
- SETUP_FUNCTION(VideoCaptureWrap)
+VideoCaptureWrap::Read(const Arguments &args) {
+
+ HandleScope scope;
+ VideoCaptureWrap *v = ObjectWrap::Unwrap<VideoCaptureWrap>(args.This());
+
+ REQ_FUN_ARG(0, cb);
+
+ videocapture_baton *baton = new videocapture_baton();
+ baton->vc = v;
+ baton->cb = Persistent<Function>::New(cb);
+ baton->im = new Matrix();
+ baton->request.data = baton;
- cv::Mat frame;
- self->cap.retrieve(frame);
+ uv_queue_work(uv_default_loop(), &baton->request, AsyncRead, AfterAsyncRead);
+ return Undefined();
- Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
- Matrix *im = ObjectWrap::Unwrap<Matrix>(im_h);
- im->mat = frame;
- return scope.Close(im_h);
}
-
+void AsyncRead(uv_work_t *req) {
+ videocapture_baton *baton = static_cast<videocapture_baton *>(req->data);
+
+ baton->vc->cap.read(baton->im->mat);
+}
+
+
+void AfterAsyncRead(uv_work_t *req) {
+
+ HandleScope scope;
+
+ videocapture_baton *baton = static_cast<videocapture_baton *>(req->data);
+
+ Local<Object> im_to_return= Matrix::constructor->GetFunction()->NewInstance();
+ Matrix *img = ObjectWrap::Unwrap<Matrix>(im_to_return);
+ cv::Mat mat;
+ mat = baton->im->mat;
+
+ img->mat = mat;
+ Local<Value> argv[1];
+
+ argv[0] = im_to_return;
+
+ baton->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+ baton->cb.Dispose();
+
+ delete baton;
+
+}
View
4 src/VideoCaptureWrap.h 100644 → 100755
@@ -11,7 +11,9 @@ class VideoCaptureWrap: public node::ObjectWrap {
VideoCaptureWrap(const std::string& filename);
VideoCaptureWrap(int device);
- static Handle<Value> GetFrame(const v8::Arguments&);
+ static Handle<Value> Read(const v8::Arguments&);
+
+
static Handle<Value> GetFrameAt(const v8::Arguments&);
};
View
2  src/init.cc 100644 → 100755
@@ -3,6 +3,7 @@
#include "Matrix.h"
#include "CascadeClassifierWrap.h"
#include "VideoCaptureWrap.h"
+#include "Contours.h"
extern "C" void
@@ -13,4 +14,5 @@ init(Handle<Object> target) {
Matrix::Init(target);
CascadeClassifierWrap::Init(target);
VideoCaptureWrap::Init(target);
+ Contour::Init(target);
};
View
0  test/smoke.js 100644 → 100755
File mode changed
View
0  wscript 100644 → 100755
File mode changed
Please sign in to comment.
Something went wrong with that request. Please try again.