├── bea ├── compile.sh ├── cascade.bea ├── opencv.bea ├── videocap.bea ├── types.bea ├── matnd.bea ├── sparsemat.bea ├── mat.bea ├── constants.bea └── cv.bea ├── index.js ├── .gitignore ├── src ├── addon.cpp ├── cvcheck.h ├── customTypes.h ├── opencv_manual.cpp ├── bea.h └── opencvjs.h ├── package.json ├── scripts ├── objdetect.coffee ├── capture.coffee ├── opencv.coffee ├── calcHist.coffee ├── tests.coffee └── effects.coffee ├── README.md └── binding.gyp /bea/compile.sh: -------------------------------------------------------------------------------- 1 | bea opencv.bea -o ../src -f -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('coffee-script') 2 | module.exports = require('./scripts/opencv') -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | node-test/ 3 | node_modules 4 | .lock-wscript 5 | scripts/data 6 | scripts/lena.jpg -------------------------------------------------------------------------------- /bea/cascade.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | @class CascadeClassifier 3 | CascadeClassifier() 4 | bool empty() 5 | bool load(const std::string& filename) 6 | std::vector detectMultiScale(const Mat& image, double scaleFactor=1.1, int minNeighbours=3, int flags = 0, Size minSize=Size()) 7 | @call 8 | std::vector fnRetVal; 9 | _this->detectMultiScale(*image, fnRetVal, scaleFactor, minNeighbours, flags, minSize); -------------------------------------------------------------------------------- /src/addon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "bea.h" 4 | #include "opencvjs.h" 5 | 6 | using namespace v8; 7 | 8 | 9 | void reportError(TryCatch& try_catch){ 10 | //if (m_logger) 11 | //m_logger(*v8::String::Utf8Value(try_catch.StackTrace())); 12 | } 13 | 14 | bea::reportExceptionCb bea::Global::reportException = reportError; 15 | 16 | extern "C" { 17 | static void init (Handle target) { 18 | HandleScope scope; 19 | opencvjs::Project::expose(target); 20 | } 21 | NODE_MODULE(addon,init); 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "opencv-node", 3 | "description": "Opencv for node", 4 | "version": "0.2.6", 5 | "homepage": "https://github.com/codeboost/opencv-node", 6 | "author": "Florin Braghis ", 7 | "keywords" : ["opencv"], 8 | "scripts": { 9 | "install": "node-gyp configure build", 10 | "test": "coffee script/tests" 11 | }, 12 | "main": "index.js", 13 | "engines": { 14 | "node": ">=0.8.11" 15 | }, 16 | "dependencies": { 17 | "coffee-script": ">=1.3.3", 18 | "underscore": ">=1.3.3", 19 | "keypress": "*" 20 | } 21 | } -------------------------------------------------------------------------------- /bea/opencv.bea: -------------------------------------------------------------------------------- 1 | @project opencvjs 2 | 3 | @targetNamespace opencvjs 4 | 5 | #C++ code added to the header file 6 | @header 7 | \#include 8 | \#include "cvcheck.h" 9 | \#include 10 | \#include 11 | \#include "bea.h" 12 | \#include "customTypes.h" 13 | 14 | #C++ code added to the cpp file 15 | @cpp 16 | \#include "opencvjs.h" 17 | 18 | using namespace cv; 19 | 20 | 21 | @include "constants.bea" 22 | @include "types.bea" 23 | @include "mat.bea" 24 | @include "videocap.bea" 25 | @include "sparsemat.bea" 26 | @include "cv.bea" 27 | @include "cascade.bea" 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /bea/videocap.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | @class VideoCapture "VideoCapture" 3 | VideoCapture() 4 | VideoCapture(const std::string& filename) 5 | VideoCapture(int device) 6 | bool open(const std::string& filename) 7 | bool open(int device) 8 | bool isOpened() 9 | void release() 10 | bool grab() 11 | bool retrieve(Mat& image, int channel=0) 12 | bool read(Mat& image) 13 | double get(int propId) 14 | bool set(int propId, double value) 15 | 16 | @class VideoWriter 17 | VideoWriter() 18 | VideoWriter(const std::string& filename, int fourcc, double fps, Size frameSize, bool isColor=true) 19 | 20 | bool open(const std::string& filename, int fourcc, double fps, Size frameSize, bool isColor=true) 21 | bool isOpened() 22 | void write(const Mat& image) -------------------------------------------------------------------------------- /src/cvcheck.h: -------------------------------------------------------------------------------- 1 | #ifndef __CVCHECK_H__ 2 | #define __CVCHECK_H__ 3 | #define THROW_EXCEPTION(msg) return v8::ThrowException(v8::Exception::TypeError(v8::String::New(msg))) 4 | 5 | //Throw if images have different size 6 | #define REQUIRE_SAME_SIZE(src, dst) if (src->size() != dst->size()) THROW_EXCEPTION("Images must have the same size") 7 | 8 | //Throws if images have different type 9 | #define REQUIRE_SAME_TYPE(src, dst) if (src->type() != dst->type()) THROW_EXCEPTION("Images must have the same type") 10 | 11 | //Throw if images have different size/type 12 | #define REQUIRE_SAME_SIZE_TYPE(src, dst) if (src->size() != dst->size() || src->type() != dst->type()) THROW_EXCEPTION("Images must have same size and type") 13 | 14 | //Throws if expression is false 15 | #define THROW_IF_NOT(expr, msg) if (!(expr)) {return v8::ThrowException(v8::Exception::RangeError(v8::String::New((msg))));} 16 | #endif //__CVCHECK_H__ 17 | -------------------------------------------------------------------------------- /scripts/objdetect.coffee: -------------------------------------------------------------------------------- 1 | cv = require './opencv' 2 | keypress = require 'keypress' 3 | 4 | src = cv.imread "lena.jpg", 1 5 | return console.log 'Error opening' if src.empty 6 | 7 | 8 | 9 | gray = new cv.Mat src.size, src.type 10 | eqImg = new cv.Mat src.size, src.type 11 | 12 | cv.cvtColor src, gray, cv.CV_RGB2GRAY 13 | cv.equalizeHist gray, eqImg 14 | result = src.clone() 15 | 16 | 17 | cascade = new cv.CascadeClassifier 18 | 19 | #The 'data' folder from opencv considered to be in current directory 20 | if not cascade.load 'data/haarcascades/haarcascade_eye.xml' 21 | console.log 'Cascade load failed' 22 | return 23 | 24 | 25 | faces = cascade.detectMultiScale eqImg, 1.1, 3, 0, {width: 20, height: 20} 26 | 27 | 28 | for rect in faces 29 | cv.rectangle result, {x: rect.x, y: rect.y}, {x: rect.x + rect.width, y:rect.y + rect.height}, [255, 0, 0], 2 30 | 31 | cv.imshow "result_img", result 32 | 33 | cv.closeOnEsc() 34 | console.log 'Press ESC to stop.' 35 | 36 | -------------------------------------------------------------------------------- /scripts/capture.coffee: -------------------------------------------------------------------------------- 1 | #Shows how to capture an image from the camera 2 | cv = require './opencv' 3 | VideoCapture = cv.VideoCapture 4 | Mat = cv.Mat 5 | 6 | testCapture = -> 7 | cap = new VideoCapture 0 8 | 9 | if not cap.isOpened() 10 | console.log 'Could not open video device' 11 | return 12 | 13 | edges = new Mat 14 | 15 | cv.namedWindow "camera", 1 16 | cv.namedWindow "output", 1 17 | 18 | running = true 19 | #note that in a real script you would want to use process.nextTick() to do the capture, 20 | #otherwise the script engine will be forever busy running this while 21 | #See effects.coffee 22 | while running 23 | 24 | frame = new Mat 25 | 26 | cap.read frame 27 | 28 | cv.imshow "camera", frame 29 | 30 | cv.cvtColor frame, edges, cv.CV_BGR2GRAY 31 | #cv.GaussianBlur edges, edges, {width: 7, height: 7}, 1.5, 1.5 32 | #cv.Canny edges, edges, 0, 30, 3 33 | 34 | cv.imshow "output", edges 35 | 36 | if cv.waitKey(27) >=0 37 | running = false 38 | 39 | 40 | testCapture() -------------------------------------------------------------------------------- /bea/types.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | 3 | @type TermCriteria 4 | int type 5 | int maxCount 6 | double epsilon 7 | 8 | @type Point 9 | int x 10 | int y 11 | 12 | @type Point2f 13 | float x 14 | float y 15 | 16 | @type Size 17 | int width 18 | int height 19 | 20 | @type Size2f 21 | float width 22 | float height 23 | 24 | @type Rect 25 | int x 26 | int y 27 | int width 28 | int height 29 | 30 | @type Range 31 | int start 32 | int end 33 | 34 | @type RotatedRect 35 | Point2f center 36 | Size2f size 37 | float angle 38 | 39 | @type minMaxLocRet 40 | double minVal 41 | double maxVal 42 | int minIdx 43 | int maxIdx 44 | 45 | @type Scalar 46 | @type std::string 47 | #@type size_t castfrom int 48 | @type std::vector 49 | @type std::vector 50 | @type std::vector 51 | @type std::vector 52 | @type Vec3f 53 | @type std::vector 54 | @type Vec2f 55 | @type std::vector 56 | @type std::vector 57 | -------------------------------------------------------------------------------- /scripts/opencv.coffee: -------------------------------------------------------------------------------- 1 | #This should be done in the C++ addon ? 2 | opencvjs = require '../build/Release/addon.node' 3 | 4 | _ = require 'underscore' 5 | cv = opencvjs.OpenCV 6 | Mat = opencvjs.Mat 7 | 8 | _.extend module.exports, opencvjs 9 | delete module.exports.OpenCV 10 | 11 | _.extend module.exports, opencvjs.OpenCV 12 | 13 | #Run the message loop on windows. This is required, otherwise the window painting will not work in Windows. 14 | module.exports.runMessageLoop = -> 15 | #I doubt this is the most efficient way to do it, but it seems to do the job for the moment 16 | doTick = -> 17 | cv.doTick(); #calls GetMessage(), TranslateMessage() and DispatchMessage() 18 | process.nextTick doTick 19 | 20 | if require('os').type().toLowerCase().indexOf("windows") is 0 21 | doTick() 22 | 23 | 24 | #This is used 25 | module.exports.closeOnEsc = -> 26 | require('keypress') process.stdin 27 | process.stdin.on 'keypress', (char, key) -> 28 | if key.name == 'escape' 29 | console.log 'Stopping.' 30 | process.exit 0 31 | process.stdin.setRawMode true 32 | process.stdin.resume() 33 | -------------------------------------------------------------------------------- /bea/matnd.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | @class MatND 3 | @postAllocator 4 | int bytes = sizeof(*_this) + (int)(_this->dataend - _this->datastart); 5 | args.This()->SetIndexedPropertiesToExternalArrayData(_this->datastart, v8::kExternalUnsignedByteArray, bytes); 6 | v8::V8::AdjustAmountOfExternalAllocatedMemory(bytes); 7 | 8 | @destructor 9 | int bytes = sizeof(*_this) + (int)(_this->dataend - _this->datastart); 10 | v8::V8::AdjustAmountOfExternalAllocatedMemory(-bytes); 11 | delete _this; 12 | 13 | MatND(); 14 | MatND(int _ndims, const int* _sizes, int _type); 15 | MatND(int _ndims, const int* _sizes, int _type, const Scalar& _s); 16 | MatND(const MatND& m); 17 | MatND(const MatND& m, const Range* ranges); 18 | ~MatND(); 19 | 20 | MatND clone() const; 21 | void copyTo( MatND& m ) const; 22 | void copyTo( MatND& m, const MatND& mask ) const; 23 | void convertTo( MatND& m, int rtype, double alpha=1, double beta=0 ) const; 24 | MatND& setTo(const Scalar& s, const MatND& mask=MatND()); 25 | MatND reshape(int _newcn, int _newndims=0, const int* _newsz=0) const; 26 | void create(int _ndims, const int* _sizes, int _type); 27 | bool isContinuous() const; 28 | size_t elemSize() const; 29 | size_t elemSize1() const; 30 | int type() const; 31 | int depth() const; 32 | int channels() const; 33 | size_t step1(int i) const; 34 | 35 | 36 | -------------------------------------------------------------------------------- /scripts/calcHist.coffee: -------------------------------------------------------------------------------- 1 | #Adaptation of the example found here: 2 | #http://opencv.willowgarage.com/documentation/cpp/imgproc_histograms.html?highlight=calchist#calcHist 3 | cv = require './opencv' 4 | keypress = require 'keypress' 5 | path = require 'path' 6 | 7 | fullpath = path.resolve 'lena.jpg' 8 | 9 | console.log 'Opening ', fullpath 10 | src = cv.imread fullpath, 1 11 | return console.log 'Error opening file' if src.empty 12 | 13 | console.log src.size 14 | 15 | cv.namedWindow "org", 0 16 | cv.imshow "org", src 17 | 18 | 19 | hsv = new cv.Mat 20 | cv.cvtColor src, hsv, cv.CV_BGR2HSV 21 | 22 | #quantize hue to 30 levels and saturation to 32 levels 23 | hbins = 30 24 | sbins = 32 25 | 26 | histSize = [hbins, sbins] 27 | hranges = [0, 180] 28 | sranges = [0, 256] 29 | 30 | ranges = [hranges, sranges] 31 | 32 | channels = [0, 1] 33 | 34 | mask = new cv.Mat 35 | hist = new cv.Mat 36 | 37 | cv.calcHist [hsv], channels, mask, hist, 2, histSize, ranges, true, false 38 | minMax = cv.minMaxLoc hist 39 | 40 | tmp = new cv.Mat 41 | 42 | scale = 10 43 | histImg = tmp.zeros sbins * scale, hbins * 10, cv.CV_8UC3 44 | 45 | for h in [0..hbins - 1] 46 | for s in [0..sbins - 1] 47 | binVal = hist.at h, s 48 | intensity = Math.round(binVal * 255 / minMax.maxVal) 49 | cv.rectangle histImg, {x: h*scale, y: s*scale}, {x: (h+1)*scale - 1, y: (s+1)*scale - 1}, [intensity, intensity, intensity, intensity], -1 50 | 51 | cv.namedWindow "H-S Histogram", 1 52 | cv.imshow "H-S Histogram", histImg 53 | 54 | 55 | cv.closeOnEsc() 56 | 57 | #Important on Windows, if you have windows which display stuff. 58 | cv.runMessageLoop() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Purpose 2 | ======= 3 | Expose opencv to the node environment. 4 | 5 | Features 6 | ======== 7 | 8 | - cv::Point, cv::Size, cv::Rect, etc replaced by object notation: 9 | Point -> {x: 0, y:0}, Size -> {width: 33, height: 33} 10 | 11 | - Checks the types of parameters as well as ranges of values on each native call (harder to crash app from script) 12 | - Friendly exception messages on invalid parameters 13 | 14 | Example 15 | ======= 16 | 17 | See scripts/effects.coffee for a full demo. 18 | 19 | 20 | Installation - MacOS X 21 | ======================= 22 | 23 | $ brew install opencv --build32 24 | $ npm install -g opencv-node 25 | $ coffee scripts/tests 26 | 27 | (tested with GCC 4.2.1 and node 0.8.0) 28 | 29 | Troubleshooting 30 | 31 | If brew complains "SHA1 mismatch" error you may find that updating homebrew fixes the issue: 32 | $ brew update 33 | 34 | If brew complains "No available formula" you will need to tap the science repository: 35 | $ brew tap homebrew/science 36 | 37 | Installation - Windows 38 | ======================= 39 | 1. Download OpenCV from http://sourceforge.net/projects/opencvlibrary/files/latest/download 40 | 2. Extract it to a folder, eg C:\OpenCV 41 | 3. Open a Visual Studio command prompt and type 42 | 43 | set OPENCV_ROOT=C:/OpenCV 44 | npm install -g opencv-node 45 | 46 | (tested with Visual Studio 2010 and node 0.8.8) 47 | 48 | API Differences 49 | 50 | Some functions have a more js-friendly API/syntax. 51 | The void functions which return their output in an argument passed by reference, return the result directly: 52 | 53 | cv::split returns an Array and takes only 1 argument 54 | cv::HoughCircles returns an Array 55 | cv::HoughLines returns an Array 56 | cv::cornerSubPix returns an Array 57 | 58 | * others? 59 | 60 | License 61 | ======= 62 | BSD 63 | 64 | Disclaimer 65 | ========== 66 | 67 | Please report any bugs or missing functions. This module has never been used in production and is generally 68 | meant to be used for experimentation. 69 | 70 | -------------------------------------------------------------------------------- /bea/sparsemat.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | @class SparseMat 3 | 4 | @destructor 5 | delete _this; 6 | 7 | SparseMat() 8 | 9 | #SparseMat 10 | SparseMat(std::vector sizes, int type) 11 | //@orgapi SparseMat(int dims, const int* _sizes, int _type); 12 | @call 13 | cv::SparseMat* fnRetVal = new cv::SparseMat(sizes.size(), &sizes[0], type); 14 | 15 | SparseMat(const Mat& m) 16 | ~SparseMat() 17 | 18 | SparseMat clone() 19 | void copyTo(SparseMat& m) 20 | void copyTo(Mat& m) 21 | void convertTo(SparseMat& m, int rtype, double alpha = 1) 22 | void convertTo(Mat& m, int rtype, double alpha=1, double beta=0) 23 | void create(std::vector sizes, int type) 24 | //@orgapi void create(int dims, const int* _sizes, int _type); 25 | @call 26 | _this->create(sizes.size(), &sizes[0], type); 27 | void clear() 28 | 29 | #void addref(); 30 | #void release(); 31 | size_t hash(int i0) 32 | size_t hash(int i0, int i1) 33 | size_t hash(int i0, int i1, int i2) 34 | size_t hash(std::vector idx) 35 | @call 36 | THROW_IF_NOT(idx.size() == _this->dims(), "Input array must have size equal to dims()"); 37 | size_t fnRetVal = _this->hash(&idx[0]); 38 | 39 | int size(int i) 40 | std::vector size() 41 | //@orgapi int* size() 42 | @call 43 | const int* sizeptr = _this->size(); 44 | std::vector fnRetVal(sizeptr, sizeptr + _this->dims()); 45 | 46 | @accessor elemSize size_t 47 | @get _this->elemSize() 48 | @set 49 | @accessor elemSize1 size_t 50 | @get _this->elemSize1() 51 | @set 52 | @accessor type int 53 | @get _this->type() 54 | @set 55 | @accessor depth int 56 | @get _this->depth() 57 | @set 58 | @accessor channels int 59 | @get _this->channels() 60 | @set 61 | @accessor dims int 62 | @get _this->dims() 63 | @set 64 | @accessor nzcount size_t 65 | @get _this->nzcount() 66 | @set 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | 'targets': [{ 3 | 'target_name': 'addon', 4 | 'sources': [ 5 | 'src/addon.cpp', 6 | 'src/opencv_manual.cpp', 7 | 'src/opencvjs.cpp' 8 | ], 9 | 'cflags' : ['-Wno-unused-variable'], 10 | 'cflags!': [ '-fno-exceptions'], 11 | 'cflags_cc!': [ '-fno-exceptions'], 12 | 'conditions': [ 13 | [ 'OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"',{ 14 | 'ldflags': [ ' 5 | #include 6 | 7 | namespace cv{ 8 | 9 | //Helper structure used as return type for the function mixMaxLoc 10 | struct minMaxLocRet{ 11 | double minVal; 12 | double maxVal; 13 | int minIdx; 14 | int maxIdx; 15 | minMaxLocRet(){ 16 | minVal = maxVal = 0.0; 17 | minIdx = maxIdx = 0; 18 | } 19 | }; 20 | } 21 | 22 | namespace bea{ 23 | 24 | template<> struct Convert{ 25 | 26 | static bool Is(v8::Handle v){ 27 | return !v.IsEmpty() && v->IsArray(); 28 | } 29 | 30 | static cv::Vec3f FromJS(v8::Handle v, int nArg){ 31 | const char* msg = "Array of 3 values expected (Vec3f)"; 32 | if (!Is(v)) BEATHROW(); 33 | 34 | v8::HandleScope scope; 35 | 36 | std::vector vec = Convert >::FromJS(v, nArg); 37 | 38 | if (vec.size() < 3) 39 | BEATHROW(); 40 | 41 | cv::Vec3f ret; 42 | ret[0] = vec[0]; 43 | ret[1] = vec[1]; 44 | ret[2] = vec[2]; 45 | return ret; 46 | } 47 | 48 | static v8::Handle ToJS(cv::Vec3f const& v){ 49 | 50 | std::vector ret; 51 | ret.push_back(v[0]); 52 | ret.push_back(v[1]); 53 | ret.push_back(v[2]); 54 | 55 | return Convert >::ToJS(ret); 56 | } 57 | 58 | }; 59 | 60 | template<> struct Convert{ 61 | 62 | static bool Is(v8::Handle v){ 63 | return !v.IsEmpty() && v->IsArray(); 64 | } 65 | 66 | static cv::Vec2f FromJS(v8::Handle v, int nArg){ 67 | const char* msg = "Array of 2 values expected (Vec3f)"; 68 | if (!Is(v)) BEATHROW(); 69 | 70 | v8::HandleScope scope; 71 | 72 | std::vector vec = Convert >::FromJS(v, nArg); 73 | 74 | if (vec.size() < 2) 75 | BEATHROW(); 76 | 77 | cv::Vec2f ret; 78 | 79 | ret[0] = vec[0]; 80 | ret[1] = vec[1]; 81 | return ret; 82 | } 83 | 84 | static v8::Handle ToJS(cv::Vec2f const& v){ 85 | std::vector ret; 86 | ret.push_back(v[0]); 87 | ret.push_back(v[1]); 88 | return Convert >::ToJS(ret); 89 | } 90 | 91 | }; 92 | 93 | 94 | template<> struct Convert { 95 | static bool Is(v8::Handle v) { 96 | return !v.IsEmpty() && v->IsArray(); 97 | } 98 | 99 | static cv::Scalar FromJS(v8::Handle v, int nArg) { 100 | const char* msg = "Array of 3 or 4 values expected (cv::Scalar)"; 101 | if (!Is(v)) BEATHROW(); 102 | 103 | v8::HandleScope scope; 104 | std::vector vec = Convert >::FromJS(v, nArg); 105 | 106 | if (vec.size() < 3 || vec.size() > 4) 107 | BEATHROW(); 108 | 109 | cv::Scalar ret; 110 | 111 | for (int k = 0; k < (int)vec.size(); k++){ 112 | ret[k] = vec[k]; 113 | } 114 | return ret; 115 | } 116 | 117 | static v8::Handle ToJS(cv::Scalar const& v) { 118 | v8::HandleScope scope; 119 | 120 | std::vector retV; 121 | 122 | for (int k = 0; k < 4; k++) 123 | retV.push_back(v[k]); 124 | 125 | return Convert >::ToJS(retV); 126 | } 127 | }; 128 | 129 | ////////////////////////////////////////////////////////////////////////// 130 | 131 | //Fortunately, this is possible for cv::Mat, because of the reference 132 | //counting which cv::Mat implements. 133 | //Converting from vector -> vector 134 | template<> struct Convert >{ 135 | 136 | static bool Is(v8::Handle v){ 137 | return Convert >::Is(v); 138 | } 139 | 140 | static std::vector FromJS(v8::Handle v, int nArg){ 141 | std::vector matV = Convert > ::FromJS(v, nArg); 142 | std::vector ret; 143 | for (size_t k = 0; k < matV.size(); k++) 144 | ret.push_back(*matV[k]); 145 | return ret; 146 | } 147 | 148 | static v8::Handle ToJS(const std::vector& val){ 149 | std::vector retVal; 150 | for (size_t k = 0; k < val.size(); k++) 151 | retVal.push_back (new cv::Mat(val[k])); 152 | 153 | return Convert >::ToJS(retVal); 154 | } 155 | }; 156 | 157 | 158 | } 159 | 160 | #endif //__CUSTOMTYPES_H__ -------------------------------------------------------------------------------- /bea/mat.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | @class Mat "Mat" 3 | @postAllocator 4 | int bytes = sizeof(*_this) + (int)(_this->dataend - _this->datastart); 5 | args.This()->SetIndexedPropertiesToExternalArrayData(_this->datastart, v8::kExternalUnsignedByteArray, bytes); 6 | v8::V8::AdjustAmountOfExternalAllocatedMemory(bytes); 7 | 8 | @destructor 9 | int bytes = sizeof(*_this) + (int)(_this->dataend - _this->datastart); 10 | v8::V8::AdjustAmountOfExternalAllocatedMemory(-bytes); 11 | delete _this; 12 | 13 | Mat() 14 | 15 | Mat(int rows, int cols, int type = CV_8UC3) 16 | THROW_IF_NOT(rows > 0 && cols > 0, "Invalid values for rows/cols parameters"); 17 | 18 | Mat(Size size, int type) 19 | THROW_IF_NOT(size.height > 0 && size.width > 0, "Invalid values for size parameter"); 20 | 21 | Mat(int rows, int cols, int type, const Scalar& s) 22 | THROW_IF_NOT(rows > 0 && cols > 0, "Invalid values for rows/cols parameters"); 23 | 24 | Mat(Size size, int type, const Scalar& s) 25 | THROW_IF_NOT(size.height > 0 && size.width > 0, "Invalid values for size parameter"); 26 | 27 | Mat(const Mat& m) 28 | Mat(const Mat& m, const Range& rowRange, const Range& colRange) 29 | 30 | Mat(const Mat& m, const Rect& r) 31 | THROW_IF_NOT(r.width > 0 && r.height > 0, "Invalid RECT argument"); 32 | 33 | Mat row(int i) 34 | THROW_IF_NOT(i > 0, "Invalid row value"); 35 | 36 | Mat col(int j) 37 | THROW_IF_NOT(j > 0, "Invalid col value"); 38 | 39 | Mat rowRange(int startrow, int endrow) 40 | THROW_IF_NOT(startrow >= 0 && endrow >= startrow && endrow <= _this->rows, "Invalid values for range parameters"); 41 | 42 | Mat rowRange(const Range& r) 43 | THROW_IF_NOT(_this->dims >= 2, "Matrix must have at least 2 dims"); 44 | THROW_IF_NOT( 0 <= r.start && r.start <= r.end && r.end <= _this->rows, "Invalid range"); 45 | 46 | 47 | Mat colRange(int startcol, int endcol) 48 | THROW_IF_NOT(startcol >= 0 && endcol >= startcol && endcol <= _this->cols, "Invalid values for range parameters"); 49 | 50 | Mat colRange(const Range& r) 51 | THROW_IF_NOT(_this->dims >= 2, "Matrix must have at least 2 dims"); 52 | THROW_IF_NOT(0 <= r.start && r.start <= r.end && r.end <= _this->cols, "Invalid range"); 53 | 54 | Mat diag(int d) 55 | Mat clone() 56 | 57 | void copyTo(Mat& m ) 58 | void copyTo(Mat& m, const Mat& mask) 59 | void convertTo(Mat& m, int rtype, double alpha=1, double beta=0) 60 | void assignTo(Mat& m, int type=-1) 61 | void setTo(const Scalar& s, const Mat& mask=Mat()) 62 | Mat reshape(int cn, int rows=0) 63 | THROW_IF_NOT(cn >= 0, "Channels must be 0 or a positive number"); 64 | THROW_IF_NOT(rows >= 0, "Rows must be 0 or a positive number"); 65 | 66 | void inv(int method=cv::DECOMP_LU) 67 | void mul(const Mat& m, double scale=1) 68 | REQUIRE_SAME_SIZE_TYPE(_this, m); 69 | 70 | void t() 71 | Mat cross(const Mat& m) 72 | THROW_IF_NOT(_this->dims <= 2 && m->dims <= 2 && _this->size() == m->size() && _this->type() == m->type() && ((_this->rows == 3 && _this->cols == 1) || (_this->cols*_this->channels() == 3 && _this->rows == 1)), "Invalid matrix size/type"); 73 | THROW_IF_NOT(_this->depth() == CV_32F || _this->depth() == CV_64F, "Matrices must have 32 or 64 bit float format"); 74 | 75 | double dot(const Mat& m) 76 | REQUIRE_SAME_SIZE(m, _this); 77 | 78 | void create(int rows, int cols, int type) 79 | THROW_IF_NOT(rows > 0 && cols > 0, "Invalid values for rows/cols parameters"); 80 | 81 | void create(Size size, int type) 82 | THROW_IF_NOT(size.height > 0 && size.width > 0, "Invalid values for size parameter"); 83 | 84 | Mat eye(int rows, int cols, int type) 85 | THROW_IF_NOT(rows > 0 && cols > 0, "Invalid dimensions"); 86 | 87 | Mat eye(Size size, int type) 88 | Mat ones(int rows, int cols, int type) 89 | Mat ones(Size size, int type) 90 | Mat zeros(int rows, int cols, int type) 91 | Mat zeros(Size size, int type) 92 | void resize(size_t sz) 93 | THROW_IF_NOT(sz > 0, "Invalid size value"); 94 | 95 | void locateROI(Size& wholeSize, Point& ofs) 96 | Mat& adjustROI(int dtop, int dbottom, int dleft, int dright) 97 | size_t step1() 98 | 99 | #Must be called whenever mat internal data size changes. 100 | void indexable() 101 | @call 102 | int bytes = sizeof(*_this) + (int)(_this->dataend - _this->datastart); 103 | args.This()->SetIndexedPropertiesToExternalArrayData(_this->datastart, v8::kExternalUnsignedByteArray, bytes); 104 | 105 | #size_t total() 106 | #bool isContinuous() 107 | #size_t elemSize() 108 | #size_t elemSize1() 109 | #int type() 110 | #int depth() 111 | #int channels() 112 | 113 | @manual void at(int i, int j) 114 | 115 | 116 | #Size size() 117 | #bool empty() 118 | @accessor width int 119 | @get _this->size().width 120 | @set 121 | 122 | @accessor height int 123 | @get _this->size().height 124 | @set 125 | 126 | @accessor size Size 127 | @get _this->size() 128 | @set 129 | 130 | @accessor type int 131 | @get _this->type() 132 | @set 133 | @accessor channels int 134 | @get _this->channels() 135 | @set 136 | 137 | @accessor isContinuous bool 138 | @get _this->isContinuous() 139 | @set 140 | 141 | @accessor elemSize int 142 | @get _this->elemSize() 143 | @set 144 | 145 | @accessor empty bool 146 | @get _this->empty() 147 | @set 148 | 149 | @accessor depth int 150 | @get _this->depth() 151 | @set 152 | 153 | @accessor total int 154 | @get _this->total() 155 | @set 156 | @accessor rows int 157 | @get _this->rows 158 | @set 159 | @accessor cols int 160 | @get _this->cols 161 | @set -------------------------------------------------------------------------------- /src/opencv_manual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "bea.h" 3 | #include "cvcheck.h" 4 | #include "opencvjs.h" 5 | #include "customTypes.h" 6 | #include 7 | 8 | using namespace cv; 9 | 10 | namespace opencvjs { 11 | 12 | v8::Handle JOpenCV::doTick(const v8::Arguments& args){ 13 | METHOD_BEGIN(0); 14 | 15 | #ifdef _WIN32 16 | //Todo: Very raw, probably better to process all messages in the queue and then return 17 | MSG msg; 18 | if (GetMessage(&msg, NULL, 0, 0)){ 19 | TranslateMessage(&msg); 20 | DispatchMessage(&msg); 21 | } 22 | #endif 23 | 24 | return args.This(); 25 | METHOD_END(); 26 | } 27 | 28 | v8::Handle JMat::at(const v8::Arguments& args) { 29 | METHOD_BEGIN(1); 30 | //Experimental - not tested 31 | cv::Mat* _this = bea::Convert::FromJS(args.This(), 0); 32 | int i = bea::Convert::FromJS(args[0], 0); 33 | int j = bea::Optional::FromJS(args, 1, 0); 34 | v8::Handle retVal; 35 | switch(_this->depth()){ 36 | case CV_8U: 37 | retVal = bea::Convert::ToJS(_this->at(i, j)); 38 | break; 39 | case CV_8S: 40 | retVal = bea::Convert::ToJS(_this->at(i, j)); 41 | break; 42 | case CV_16U: 43 | retVal = bea::Convert::ToJS(_this->at(i, j)); 44 | break; 45 | case CV_16S: 46 | retVal = bea::Convert::ToJS(_this->at(i, j)); 47 | break; 48 | case CV_32S: 49 | retVal = bea::Convert::ToJS(_this->at(i, j)); 50 | break; 51 | case CV_32F: 52 | retVal = bea::Convert::ToJS(_this->at(i, j)); 53 | break; 54 | case CV_64F: 55 | retVal = bea::Convert::ToJS(_this->at(i, j)); 56 | break; 57 | 58 | default: 59 | break; 60 | } 61 | 62 | return retVal; 63 | METHOD_END(); 64 | } 65 | 66 | 67 | v8::Handle JOpenCV::discardMats(const v8::Arguments& args) { 68 | METHOD_BEGIN(1); 69 | v8::HandleScope scope; 70 | if (args[0]->IsArray()){ 71 | 72 | v8::Local ar = v8::Array::Cast(*args[0]); 73 | int len = ar->Length(); 74 | for (int k = 0; k < len; k++){ 75 | v8::Handle val = ar->Get(k); 76 | if (bea::Convert::Is(val)){ 77 | v8::Handle obj = val->ToObject(); 78 | cv::Mat* ptr = bea::Convert::FromJS(obj, 0); 79 | v8::V8::AdjustAmountOfExternalAllocatedMemory(-(int)(ptr->dataend - ptr->datastart)); 80 | delete ptr; 81 | obj->SetPointerInInternalField(0, NULL); 82 | } 83 | } 84 | } else { 85 | if (bea::Convert::Is(args[0])){ 86 | v8::Local obj = args[0]->ToObject(); 87 | cv::Mat* ptr = bea::Convert::FromJS(obj, 0); 88 | v8::V8::AdjustAmountOfExternalAllocatedMemory(-(int)(ptr->dataend - ptr->datastart)); 89 | delete ptr; 90 | obj->SetPointerInInternalField(0, NULL); 91 | } 92 | } 93 | return args.This(); 94 | METHOD_END(); 95 | } 96 | 97 | 98 | v8::Handle JOpenCV::cvSmooth(const v8::Arguments& args) { 99 | METHOD_BEGIN(2); 100 | //void cvSmooth(const Mat& src, Mat& dst, int smoothtype=CV_GAUSSIAN, int param1=3, int param2=0, double param3=0, double param4=0) 101 | cv::Mat* src = bea::Convert::FromJS(args[0], 0); 102 | cv::Mat* dst = bea::Convert::FromJS(args[1], 1); 103 | int smoothtype = bea::Optional::FromJS(args, 2, CV_GAUSSIAN); 104 | int param1 = bea::Optional::FromJS(args, 3, 3); 105 | int param2 = bea::Optional::FromJS(args, 4, 0); 106 | double param3 = bea::Optional::FromJS(args, 5, 0); 107 | double param4 = bea::Optional::FromJS(args, 6, 0); 108 | 109 | CvMat cvSrc = *src; 110 | CvMat cvDst = *dst; 111 | 112 | ::cvSmooth(&cvSrc, &cvDst, smoothtype, param1, param2, param3, param4); 113 | return args.This(); 114 | METHOD_END(); 115 | } 116 | 117 | 118 | 119 | v8::Handle JOpenCV::fillPoly(const v8::Arguments& args) { 120 | METHOD_BEGIN(5); 121 | // void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point()) 122 | //TODO: Enter code here 123 | return args.This(); 124 | METHOD_END(); 125 | } 126 | 127 | v8::Handle JOpenCV::getTextSize(const v8::Arguments& args) { 128 | METHOD_BEGIN(5); 129 | // Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine) 130 | //TODO: Enter code here 131 | return args.This(); 132 | METHOD_END(); 133 | } 134 | 135 | v8::Handle JOpenCV::polylines(const v8::Arguments& args) { 136 | METHOD_BEGIN(6); 137 | // void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 138 | //TODO: Enter code here 139 | return args.This(); 140 | METHOD_END(); 141 | } 142 | 143 | v8::Handle JOpenCV::kmeans(const v8::Arguments& args) { 144 | METHOD_BEGIN(7); 145 | // double kmeans(const Mat& samples, int clusterCount, Mat& labels, TermCriteria termcrit, int attempts, int flags, Mat* centers) 146 | //TODO: Enter code here 147 | return args.This(); 148 | METHOD_END(); 149 | } 150 | 151 | v8::Handle JOpenCV::calcBackProject(const v8::Arguments& args) { 152 | METHOD_BEGIN(6); 153 | // void calcBackProject(const Mat* arrays, int narrays, const int* channels, const SparseMat& hist, Mat& backProject, const float** ranges, double scale=1, bool uniform=true) 154 | //TODO: Enter code here 155 | return args.This(); 156 | METHOD_END(); 157 | } 158 | 159 | v8::Handle JOpenCV::detectObject(const v8::Arguments& args) { 160 | METHOD_BEGIN(3); 161 | // std::vector detectObject(const std::string& cascadeName, int imageWidth, int imageHeight) 162 | //TODO: Enter code here 163 | return args.This(); 164 | METHOD_END(); 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /bea/constants.bea: -------------------------------------------------------------------------------- 1 | @const 2 | CV_8U 3 | CV_8S 4 | CV_16U 5 | CV_16S 6 | CV_32S 7 | CV_32F 8 | CV_64F 9 | CV_USRTYPE1 10 | 11 | CV_8UC1 12 | CV_8UC2 13 | CV_8UC3 14 | CV_8UC4 15 | 16 | CV_8SC1 17 | CV_8SC2 18 | CV_8SC3 19 | CV_8SC4 20 | 21 | CV_16UC1 22 | CV_16UC2 23 | CV_16UC3 24 | CV_16UC4 25 | 26 | CV_16SC1 27 | CV_16SC2 28 | CV_16SC3 29 | CV_16SC4 30 | 31 | CV_32SC1 32 | CV_32SC2 33 | CV_32SC3 34 | CV_32SC4 35 | 36 | CV_32FC1 37 | CV_32FC2 38 | CV_32FC3 39 | CV_32FC4 40 | 41 | CV_64FC1 42 | CV_64FC2 43 | CV_64FC3 44 | CV_64FC4 45 | 46 | CV_BGR2BGRA 47 | CV_RGB2RGBA 48 | CV_BGRA2BGR 49 | CV_RGBA2RGB 50 | CV_BGR2RGBA 51 | CV_RGB2BGRA 52 | CV_RGBA2BGR 53 | CV_BGRA2RGB 54 | CV_BGR2RGB 55 | CV_RGB2BGR 56 | CV_BGRA2RGBA 57 | CV_RGBA2BGRA 58 | CV_BGR2GRAY 59 | CV_RGB2GRAY 60 | CV_GRAY2BGR 61 | CV_GRAY2RGB 62 | CV_GRAY2BGRA 63 | CV_GRAY2RGBA 64 | CV_BGRA2GRAY 65 | CV_RGBA2GRAY 66 | CV_BGR2BGR565 67 | CV_RGB2BGR565 68 | CV_BGR5652BGR 69 | CV_BGR5652RGB 70 | CV_BGRA2BGR565 71 | CV_RGBA2BGR565 72 | CV_BGR5652BGRA 73 | CV_BGR5652RGBA 74 | CV_GRAY2BGR565 75 | CV_BGR5652GRAY 76 | CV_BGR2BGR555 77 | CV_RGB2BGR555 78 | CV_BGR5552BGR 79 | CV_BGR5552RGB 80 | CV_BGRA2BGR555 81 | CV_RGBA2BGR555 82 | CV_BGR5552BGRA 83 | CV_BGR5552RGBA 84 | CV_GRAY2BGR555 85 | CV_BGR5552GRAY 86 | CV_BGR2XYZ 87 | CV_RGB2XYZ 88 | CV_XYZ2BGR 89 | CV_XYZ2RGB 90 | CV_BGR2YCrCb 91 | CV_RGB2YCrCb 92 | CV_YCrCb2BGR 93 | CV_YCrCb2RGB 94 | CV_BGR2HSV 95 | CV_RGB2HSV 96 | CV_BGR2Lab 97 | CV_RGB2Lab 98 | CV_BayerBG2BGR 99 | CV_BayerGB2BGR 100 | CV_BayerRG2BGR 101 | CV_BayerGR2BGR 102 | CV_BayerBG2RGB 103 | CV_BayerGB2RGB 104 | CV_BayerRG2RGB 105 | CV_BayerGR2RGB 106 | CV_BGR2Luv 107 | CV_RGB2Luv 108 | CV_BGR2HLS 109 | CV_RGB2HLS 110 | CV_HSV2BGR 111 | CV_HSV2RGB 112 | CV_Lab2BGR 113 | CV_Lab2RGB 114 | CV_Luv2BGR 115 | CV_Luv2RGB 116 | CV_HLS2BGR 117 | CV_HLS2RGB 118 | CV_BayerBG2BGR_VNG 119 | CV_BayerGB2BGR_VNG 120 | CV_BayerRG2BGR_VNG 121 | CV_BayerGR2BGR_VNG 122 | CV_BayerBG2RGB_VNG 123 | CV_BayerGB2RGB_VNG 124 | CV_BayerRG2RGB_VNG 125 | CV_BayerGR2RGB_VNG 126 | CV_BGR2HSV_FULL 127 | CV_RGB2HSV_FULL 128 | CV_BGR2HLS_FULL 129 | CV_RGB2HLS_FULL 130 | CV_HSV2BGR_FULL 131 | CV_HSV2RGB_FULL 132 | CV_HLS2BGR_FULL 133 | CV_HLS2RGB_FULL 134 | CV_LBGR2Lab 135 | CV_LRGB2Lab 136 | CV_LBGR2Luv 137 | CV_LRGB2Luv 138 | CV_Lab2LBGR 139 | CV_Lab2LRGB 140 | CV_Luv2LBGR 141 | CV_Luv2LRGB 142 | CV_BGR2YUV 143 | CV_RGB2YUV 144 | CV_YUV2BGR 145 | CV_YUV2RGB 146 | 147 | THRESH_BINARY 148 | THRESH_BINARY_INV 149 | THRESH_TRUNC 150 | THRESH_TOZERO 151 | THRESH_TOZERO_INV 152 | THRESH_MASK 153 | THRESH_OTSU 154 | 155 | 156 | CV_COVAR_SCRAMBLED 157 | CV_COVAR_NORMAL 158 | CV_COVAR_USE_AVG 159 | CV_COVAR_SCALE 160 | CV_COVAR_ROWS 161 | CV_COVAR_COLS 162 | 163 | 164 | DECOMP_LU 165 | DECOMP_SVD 166 | DECOMP_EIG 167 | DECOMP_CHOLESKY 168 | DECOMP_QR 169 | DECOMP_NORMAL 170 | 171 | 172 | NORM_INF 173 | NORM_L1 174 | NORM_L2 175 | NORM_TYPE_MASK 176 | NORM_RELATIVE 177 | NORM_MINMAX 178 | 179 | 180 | CMP_EQ 181 | CMP_GT 182 | CMP_GE 183 | CMP_LT 184 | CMP_LE 185 | CMP_NE 186 | 187 | GEMM_1_T 188 | GEMM_2_T 189 | GEMM_3_T 190 | 191 | DFT_INVERSE 192 | DFT_SCALE 193 | DFT_ROWS 194 | DFT_COMPLEX_OUTPUT 195 | DFT_REAL_OUTPUT 196 | DCT_INVERSE 197 | DCT_ROWS 198 | 199 | CV_THRESH_BINARY 200 | CV_THRESH_BINARY_INV 201 | CV_THRESH_TRUNC 202 | CV_THRESH_TOZERO 203 | CV_THRESH_TOZERO_INV 204 | CV_THRESH_MASK 205 | CV_THRESH_OTSU 206 | 207 | 208 | CV_EVENT_MOUSEMOVE 209 | CV_EVENT_LBUTTONDOWN 210 | CV_EVENT_RBUTTONDOWN 211 | CV_EVENT_MBUTTONDOWN 212 | CV_EVENT_LBUTTONUP 213 | CV_EVENT_RBUTTONUP 214 | CV_EVENT_MBUTTONUP 215 | CV_EVENT_LBUTTONDBLCLK 216 | CV_EVENT_RBUTTONDBLCLK 217 | CV_EVENT_MBUTTONDBLCLK 218 | 219 | 220 | CV_EVENT_FLAG_LBUTTON 221 | CV_EVENT_FLAG_RBUTTON 222 | CV_EVENT_FLAG_MBUTTON 223 | CV_EVENT_FLAG_CTRLKEY 224 | CV_EVENT_FLAG_SHIFTKEY 225 | CV_EVENT_FLAG_ALTKEY 226 | 227 | CV_INTER_NN 228 | CV_INTER_LINEAR 229 | CV_INTER_CUBIC 230 | CV_INTER_AREA 231 | CV_INTER_LANCZOS4 232 | 233 | CV_WARP_FILL_OUTLIERS 234 | CV_WARP_INVERSE_MAP 235 | 236 | CV_SHAPE_RECT 237 | CV_SHAPE_CROSS 238 | CV_SHAPE_ELLIPSE 239 | CV_SHAPE_CUSTOM 240 | 241 | CV_MOP_ERODE 242 | CV_MOP_DILATE 243 | CV_MOP_OPEN 244 | CV_MOP_CLOSE 245 | CV_MOP_GRADIENT 246 | CV_MOP_TOPHAT 247 | CV_MOP_BLACKHAT 248 | 249 | CV_TM_SQDIFF 250 | CV_TM_SQDIFF_NORMED 251 | CV_TM_CCORR 252 | CV_TM_CCORR_NORMED 253 | CV_TM_CCOEFF 254 | CV_TM_CCOEFF_NORMED 255 | 256 | 257 | CV_RETR_EXTERNAL 258 | CV_RETR_LIST 259 | CV_RETR_CCOMP 260 | CV_RETR_TREE 261 | 262 | CV_CHAIN_CODE 263 | CV_CHAIN_APPROX_NONE 264 | CV_CHAIN_APPROX_SIMPLE 265 | CV_CHAIN_APPROX_TC89_L1 266 | CV_CHAIN_APPROX_TC89_KCOS 267 | CV_LINK_RUNS 268 | 269 | CV_PTLOC_ERROR 270 | CV_PTLOC_OUTSIDE_RECT 271 | CV_PTLOC_INSIDE 272 | CV_PTLOC_VERTEX 273 | CV_PTLOC_ON_EDGE 274 | 275 | CV_NEXT_AROUND_ORG 276 | CV_NEXT_AROUND_DST 277 | CV_PREV_AROUND_ORG 278 | CV_PREV_AROUND_DST 279 | CV_NEXT_AROUND_LEFT 280 | CV_NEXT_AROUND_RIGHT 281 | CV_PREV_AROUND_LEFT 282 | CV_PREV_AROUND_RIGHT 283 | 284 | CV_POLY_APPROX_DP 285 | 286 | CV_CONTOURS_MATCH_I1 287 | CV_CONTOURS_MATCH_I2 288 | CV_CONTOURS_MATCH_I3 289 | 290 | CV_CLOCKWISE 291 | CV_COUNTER_CLOCKWISE 292 | 293 | CV_COMP_CORREL 294 | CV_COMP_CHISQR 295 | CV_COMP_INTERSECT 296 | CV_COMP_BHATTACHARYYA 297 | 298 | CV_DIST_MASK_3 299 | CV_DIST_MASK_5 300 | CV_DIST_MASK_PRECISE 301 | 302 | CV_DIST_USER 303 | CV_DIST_L1 304 | CV_DIST_L2 305 | CV_DIST_C 306 | CV_DIST_L12 307 | CV_DIST_FAIR 308 | CV_DIST_WELSCH 309 | CV_DIST_HUBER 310 | 311 | CV_ADAPTIVE_THRESH_MEAN_C 312 | CV_ADAPTIVE_THRESH_GAUSSIAN_C 313 | 314 | CV_FLOODFILL_FIXED_RANGE 315 | CV_FLOODFILL_MASK_ONLY 316 | 317 | CV_CANNY_L2_GRADIENT 318 | 319 | CV_HOUGH_PROBABILISTIC 320 | CV_HOUGH_STANDARD 321 | CV_HOUGH_MULTI_SCALE 322 | CV_HOUGH_GRADIENT 323 | 324 | CV_BLUR_NO_SCALE 325 | CV_BLUR 326 | CV_GAUSSIAN 327 | CV_MEDIAN 328 | CV_BILATERAL 329 | 330 | CV_GAUSSIAN_5x5 331 | 332 | #CV_INPAINT_NS 333 | #CV_INPAINT_TELEA 334 | 335 | CV_SCHARR 336 | CV_MAX_SOBEL_KSIZE 337 | 338 | FONT_HERSHEY_SIMPLEX 339 | FONT_HERSHEY_PLAIN 340 | FONT_HERSHEY_DUPLEX 341 | FONT_HERSHEY_COMPLEX 342 | FONT_HERSHEY_TRIPLEX 343 | FONT_HERSHEY_COMPLEX_SMALL 344 | FONT_HERSHEY_SCRIPT_SIMPLEX 345 | FONT_HERSHEY_SCRIPT_COMPLEX 346 | FONT_ITALIC 347 | 348 | CV_FILLED 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | -------------------------------------------------------------------------------- /scripts/tests.coffee: -------------------------------------------------------------------------------- 1 | #Primitive unit tests. 2 | #If it runs without crashing, then the addon has been successfully installed 3 | 4 | cv = require './opencv' 5 | Mat = cv.Mat 6 | 7 | Results = 8 | failed: 0 9 | passed: 0 10 | total: 0 11 | 12 | ok = (expr, message) -> 13 | Results.total++ 14 | if expr isnt true 15 | console.log "FAILED: " + message 16 | #console.log new Error().stack 17 | process.exit -1 18 | Results.failed++ 19 | else 20 | console.log "PASSED: " + message 21 | Results.passed++ 22 | mustThrow = (name, fn) -> 23 | excepted = false 24 | try 25 | fn() 26 | catch e 27 | excepted = true 28 | unless excepted 29 | ok false, "Exception not thrown for '" + name + "'" 30 | else 31 | ok true, "Exception thrown for '" + name 32 | test = (name, fnTest) -> 33 | console.log "*****" 34 | console.log name 35 | try 36 | fnTest() 37 | catch e 38 | ok false, "Exception thrown for '" + name + "': " + e.message 39 | 40 | SAME_WIDTH = (src, dst) -> 41 | src.width is dst.width 42 | 43 | SAME_HEIGHT = (src, dst) -> 44 | src.height is dst.height 45 | 46 | SAME_WIDTH_HEIGHT = (src, dst) -> 47 | src.width is dst.width and src.height is dst.height 48 | 49 | SAME_WIDTH_HEIGHT_TYPE = (src, dst) -> 50 | SAME_WIDTH_HEIGHT(src, dst) and src.type is dst.type 51 | 52 | SAME_EVERYTHING = (src, dst) -> 53 | SAME_WIDTH_HEIGHT_TYPE(src, dst) and src.channels is dst.channels and src.depth is dst.depth and src.total is dst.total and src.isContinuous is dst.isContinuous and src.elemSize is dst.elemSize and src.empty is dst.empty 54 | 55 | init = (width, height, format) -> 56 | console.log "Starting tests" 57 | srcMat = new Mat( 58 | width: width 59 | height: height 60 | , format) 61 | srcMat.setTo [ 0, 0, 0 ] 62 | test "createMat", -> 63 | mustThrow "new Mat(156, undefined)", -> 64 | new Mat(156, `undefined`) 65 | 66 | mat1 = new Mat(155, 156) 67 | ok mat1.width is 156 and mat1.height is 155, "Mat1 width/height" 68 | ok mat1.type is cv.CV_8UC3, "Type of mat created with int parameters" 69 | mat1 = new Mat(srcMat) 70 | ok mat1.width is srcMat.width and mat1.height is srcMat.height and mat1.type is srcMat.type and mat1.depth is srcMat.depth and mat1.channels is srcMat.channels, "Width/height/channels/depth of matrix created with Mat parameter" 71 | mat1 = new Mat( 72 | width: 123 73 | height: 124 74 | , cv.CV_8UC4) 75 | ok mat1.width is 123 and mat1.height is 124, "Width/height of Mat created with Size param" 76 | ok mat1.type is cv.CV_8UC4, "Type of mat created with size param" 77 | 78 | mat1 = new Mat(srcMat, 79 | x: 20 80 | y: 20 81 | width: 100 82 | height: 100 83 | ) 84 | ok mat1.type is srcMat.type, "Type of matrix created with Mat and RECT params" 85 | ok mat1.width is 100, "Width of mat created with RECT param" 86 | ok mat1.height is 100, "Height of mat created with RECT param" 87 | mustThrow "new Mat(100)", -> 88 | mat1 = new Mat(100) 89 | 90 | mustThrow "new Mat(100, 0)", -> 91 | mat1 = new Mat(100, 0) 92 | 93 | mustThrow "new Mat(-100, 100)", -> 94 | mat1 = new Mat(-100, 0) 95 | 96 | mustThrow "new Mat({width: 324}, 12)", -> 97 | mat1 = new Mat( 98 | width: 324 99 | , 12) 100 | 101 | test "range", -> 102 | ok srcMat.channels is 3, "3 channels" 103 | myRange = srcMat.colRange(10, 20) 104 | mustThrow "Invalid col range", -> 105 | srcMat.colRange 0, width + 1 106 | 107 | mustThrow "Invalid col range 2", -> 108 | srcMat.colRange 10, 2 109 | 110 | mustThrow "Invalid col range 3", -> 111 | srcMat.colRange -21, width 112 | 113 | colsMat = srcMat.colRange(0, width) 114 | ok colsMat.width is width, "Width must be equal" 115 | ok colsMat.height is height, "Height must be equal" 116 | mustThrow "Invalid row range", -> 117 | srcMat.rowRange 0, height + 1 118 | 119 | mustThrow "Invalid row range 2", -> 120 | srcMat.rowRange 10, 2 121 | 122 | mustThrow "Invalid row range 3", -> 123 | srcMat.rowRange -21, width 124 | 125 | test "clone", -> 126 | cloned = srcMat.clone() 127 | console.log "Sizes: " + srcMat.size.width + "x" + srcMat.size.height + "-" + cloned.size.width + "x" + cloned.size.height 128 | ok SAME_EVERYTHING(srcMat, cloned), "Cloned matrix must be identical" 129 | 130 | test "copyTo", -> 131 | destMat = undefined 132 | mustThrow "Copy to undefined var", -> 133 | srcMat.copyTo destMat 134 | 135 | destMat = new Mat(100, 100, cv.CV_8UC3) 136 | srcMat.copyTo destMat 137 | ok SAME_EVERYTHING(srcMat, destMat), "Copy to different size matrix" 138 | destMat = new Mat(height, width, cv.CV_8UC4) 139 | srcMat.copyTo destMat 140 | ok SAME_EVERYTHING(srcMat, destMat), "Copy to different depth matrix" 141 | mustThrow "Copy to RECT argument", -> 142 | srcMat.copyTo 143 | width: 1090 144 | height: 33 145 | 146 | destMat = new Mat(height, width, cv.CV_8UC3) 147 | ok srcMat.copyTo(destMat) is srcMat, "Copy to good matrix" 148 | 149 | test "setTo", -> 150 | mustThrow "setTo(undefined)", -> 151 | srcMat.setTo() 152 | 153 | mustThrow "setTo(1)", -> 154 | srcMat.setTo 1 155 | 156 | mustThrow "setTo({width: 1900, height: 1000})", -> 157 | srcMat.setTo 158 | width: 1900 159 | height: 1000 160 | 161 | mustThrow "setTo([])", -> 162 | srcMat.setTo [] 163 | 164 | mustThrow "setTo([1, 2])", -> 165 | srcMat.setTo [ 1, 2 ] 166 | 167 | mustThrow "setTo([1, 2, {one: 1}])", -> 168 | srcMat.setTo [ 1, 2, 169 | one: 1 170 | ] 171 | 172 | ok srcMat is srcMat.setTo([ 1, 2, 2 ]), "srcMat == srcMat.setTo([1, 2, 2])" 173 | srcMat.setTo [ 0, 0, 0 ] 174 | ok srcMat[0] is 0, "mat[0] == 0" 175 | ok srcMat[192] is 0, "mat[192] == 0" 176 | srcMat.setTo [ 123, 253, 99 ] 177 | ok srcMat[0] is 123, "mat[0] == 123" 178 | ok srcMat[1] is 253, "mat[1] = 253" 179 | ok srcMat[2] is 99, "mat[2] = 99" 180 | ok srcMat[3] is 123, "mat[3] == 123" 181 | ok srcMat[4] is 253, "mat[4] = 253" 182 | ok srcMat[5] is 99, "mat[5] = 99" 183 | srcMat[33] = 22 184 | ok srcMat[33] is 22, "mat[33] == 22" 185 | 186 | test "mul", -> 187 | 188 | mustThrow "mul(undefined)", -> 189 | srcMat.mul `undefined` 190 | 191 | mustThrow "mul({one: 1})", -> 192 | srcMat.mul one: 1 193 | 194 | mulMat = new Mat(100, 100) 195 | 196 | mustThrow "mul(invalid size matrix)", -> 197 | srcMat.mul mulMat 198 | 199 | srcMat = new Mat 100, 100 200 | 201 | mulMat = new Mat(srcMat.height, srcMat.width, srcMat.type) 202 | newMat = srcMat.mul(mulMat) 203 | ok SAME_WIDTH_HEIGHT_TYPE(newMat, srcMat), "Multiplied must have same w/h/t" 204 | 205 | test "convertTo", -> 206 | mustThrow "convertTo(undefined)", -> 207 | srcMat.convertTo `undefined`, 1 208 | 209 | mustThrow "convertTo(1)", -> 210 | srcMat.convertTo 1, 1 211 | 212 | srcMat = new Mat 100, 100 213 | 214 | dest = new Mat(1, 1, cv.CV_8UC2) 215 | srcMat.convertTo dest, cv.CV_8UC2 216 | ok SAME_WIDTH_HEIGHT(srcMat, dest), "Same width height after convertTo" 217 | ok dest.depth is srcMat.depth, "dest.depth === src.depth" 218 | 219 | test "reshape", -> 220 | 221 | src = new Mat 100, 100 222 | 223 | mustThrow "reshape(-1)", -> 224 | src.reshape -1 225 | 226 | mustThrow "reshape(1, -1)", -> 227 | src.reshape 0, -1 228 | 229 | res = src.reshape(0) 230 | ok SAME_EVERYTHING(res, src), "reshaped must be a cloned src" 231 | res = src.reshape(1, 0) 232 | ok res.width is src.width * 3, "Result width must be 3 * src.width?" 233 | 234 | 235 | init 320, 200, cv.CV_8UC3 236 | 237 | console.log '*** Finished ***' 238 | console.log Results 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | -------------------------------------------------------------------------------- /scripts/effects.coffee: -------------------------------------------------------------------------------- 1 | _ = require 'underscore' 2 | cv = require './opencv' 3 | keypress = require 'keypress' 4 | 5 | hsvFrame = null 6 | thresholded = null 7 | thresholded2 = null 8 | Mat = cv.Mat 9 | 10 | #Base class for effects 11 | class Effect 12 | name: "Untitled" 13 | constructor: -> 14 | runEffect: (frameIn, frameOut) -> 15 | @run frameIn, frameOut 16 | cv.putText(frameOut, @name, {x: 0, y: 50}, cv.FONT_HERSHEY_SIMPLEX , 1, [128, 255, 128], 2, 1); 17 | 18 | #Apply 'negative' effect to the image 19 | class Negative extends Effect 20 | name: "Negative" 21 | run: (frameIn, frameOut) -> cv.bitwise_not frameIn, frameOut 22 | 23 | #Make image black and white 24 | class BlackAndWhite extends Effect 25 | name: "Black and white" 26 | constructor: (size, format) -> 27 | @tmp = new Mat size, cv.CV_8U 28 | @rgb = new Mat size, cv.CV_8UC3 29 | 30 | run: (frameIn, frameOut) -> 31 | cv.cvtColor frameIn, @tmp, cv.CV_RGBA2GRAY 32 | cv.cvtColor @tmp, @rgb, cv.CV_GRAY2RGB 33 | cv.cvtColor @rgb, frameOut, cv.CV_RGB2RGBA 34 | 35 | #Create a 'ghost' effect from the image 36 | class EffectGhost extends Effect 37 | name: "Ghost" 38 | run: (frameIn, frameOut) -> 39 | cv.cvtColor frameIn, frameOut, cv.CV_RGBA2GRAY 40 | cv.bitwise_not frameIn, frameOut 41 | 42 | #Detect the edges 43 | class Edges extends Effect 44 | name: "Edges" 45 | constructor: (size, format) -> 46 | @tmp = new Mat size, format 47 | @rgb = new Mat size, cv.CV_8UC3 48 | run: (frameIn, frameOut) -> 49 | cv 50 | .cvtColor(frameIn, @tmp, cv.CV_BGRA2GRAY) 51 | .GaussianBlur(@tmp, @tmp, {width: 7, height: 7}, 1.5, 1.5) 52 | .Canny(@tmp, @tmp, 3, 7); 53 | 54 | cv.cvtColor @tmp, @rgb, cv.CV_GRAY2RGB 55 | cv.cvtColor @rgb, frameOut, cv.CV_RGB2RGBA 56 | 57 | #Make image look old 58 | class Sepia extends Effect 59 | name: "Sepia" 60 | constructor: (size, format) -> 61 | @hsvImage = new Mat size, cv.CV_8U 62 | @tmp = new Mat size, cv.CV_8UC3 63 | run: (frameIn, frameOut) -> 64 | defaultH = 22; 65 | defaultS = 90; 66 | cv.cvtColor frameIn, @hsvImage, cv.CV_BGR2HSV 67 | components = cv.split @hsvImage 68 | components[0].setTo [defaultH, 0, 0] 69 | components[1].setTo [defaultS, 0, 0] 70 | cv.merge(components, @hsvImage); 71 | cv.cvtColor @hsvImage, @tmp, cv.CV_HSV2BGR 72 | cv.cvtColor @tmp, frameOut, cv.CV_RGB2RGBA 73 | 74 | class Binarize extends Effect 75 | name: "Binarize" 76 | constructor: (size, format)-> 77 | @binaryImage = null 78 | @tmpImage = new Mat 79 | @rgb = new Mat size, cv.CV_8UC3 80 | run: (frameIn, frameOut) -> 81 | #frameIn.copyTo frameOut 82 | cv.cvtColor frameIn, @tmpImage, cv.CV_RGBA2GRAY 83 | 84 | if (!@binaryImage) 85 | @binaryImage = @tmpImage.clone(); 86 | 87 | cv.threshold @tmpImage, @binaryImage, 50, 255, cv.CV_THRESH_BINARY 88 | cv.cvtColor @binaryImage, @rgb, cv.CV_GRAY2RGB 89 | cv.cvtColor @rgb, frameOut, cv.CV_RGB2RGBA 90 | 91 | #This shows how to directly access the Mat pixels 92 | class Mirror extends Effect 93 | constructor: (@size) -> 94 | @tmp = new Mat @size, cv.CV_8UC3 95 | name: "Mirror" 96 | run: (frameIn, frameOut) -> 97 | cv.cvtColor frameIn, @tmp, cv.CV_RGBA2RGB 98 | frame = @tmp 99 | frame.indexable(); #Note this call. Makes it possible to access the frame bytes with [] 100 | bpp = 3 101 | frameWidth = frame.width 102 | frameHeight = frame.height 103 | 104 | halfsizes = frameWidth / 2 105 | frameBytes = frameWidth * bpp - 1 106 | 107 | i = 0 108 | while i++ < frameHeight 109 | offset = i * frameWidth * bpp 110 | j = 0 111 | while j++ < halfsizes 112 | jBytes = offset + frameBytes - (j * 3) 113 | ojBytes = offset + (j * 3) 114 | 115 | frame[jBytes - 2] = frame[ojBytes] 116 | frame[jBytes - 1] = frame[ojBytes+1] 117 | frame[jBytes] = frame[ojBytes+2] 118 | 119 | cv.cvtColor @tmp, frameOut, cv.CV_RGB2RGBA 120 | true 121 | 122 | #detect an orange ball in the image and draw a circle around it. 123 | #works, but accuracy is terrible 124 | class DetectOrangeBall extends Effect 125 | name: "Orange ball" 126 | constructor: (@size) -> 127 | @hsvMin = [0, 50, 170] 128 | @hsvMax = [10, 180, 256] 129 | 130 | @hsvMin2 = [170, 50, 170] 131 | @hsvMax2 = [256, 180, 256] 132 | @hsvFrame = new Mat size, cv.CV_8U 133 | @thresholded = new Mat size, cv.CV_8U 134 | @thresholded2 = new Mat size, cv.CV_8U 135 | 136 | run: (frameIn, frameOut) -> 137 | frameIn.copyTo frameOut 138 | cv.cvtColor frameIn, @hsvFrame, cv.CV_BGR2HSV 139 | 140 | cv.inRange @hsvFrame, @hsvMin, @hsvMax, @thresholded 141 | cv.inRange @hsvFrame, @hsvMin2, @hsvMax2, @thresholded2 142 | 143 | cv.bitwise_or @thresholded, @thresholded2, @thresholded 144 | 145 | circles = cv.HoughCircles @thresholded, cv.CV_HOUGH_GRADIENT, 2, @thresholded.height/4, 100, 40, 20, 50 146 | @drawCircles circles, frameOut 147 | 148 | drawCircles: (circles, frameOut) -> 149 | _.each circles, (circle, i) -> 150 | center = 151 | x: Math.round circle[0] 152 | y: Math.round circle[1] 153 | cv.circle frameOut, center, Math.round(circle[2]), [255, 0, 0], 3, 8 154 | 155 | 156 | class RotateImage extends Effect 157 | name: "Rotated" 158 | constructor: (@size, format) -> 159 | @rotAngle = 0 160 | run: (frameIn, frameOut) -> 161 | rotation = cv.getRotationMatrix2D({x: frameIn.width / 2, y: frameIn.height / 2}, @rotAngle++, 1); 162 | cv.warpAffine(frameIn, frameOut, rotation, frameIn.size); 163 | @rotAngle = (@rotAngle + 10) % 360; 164 | 165 | class CircleDetector extends Effect 166 | constructor: -> 167 | @count = 0 168 | @lastDetection = new Date().getTime() 169 | hasCircle: (frameIn, frameOut) -> 170 | curTime = new Date().getTime() 171 | if curTime - @lastDetection < 3000 172 | @count = 0 173 | return false 174 | gray = new Mat 175 | cv.cvtColor frameIn, gray, cv.CV_RGBA2GRAY 176 | cv.GaussianBlur( gray, gray, {width: 9, height: 9}, 2, 2 ) 177 | circles = cv.HoughCircles(gray, cv.CV_HOUGH_GRADIENT, 2, gray.rows/4, 200, 100 ); 178 | 179 | has = _.any circles, (circle) -> 180 | circle[2] > 60 && circle[2] < 120 181 | 182 | if has then @count++ else @count = 0 183 | 184 | if @count >= 14 185 | @count = 0 186 | @lastDetection = new Date().getTime() 187 | return true 188 | 189 | return false 190 | 191 | drawCircles: (circles, frameOut) -> 192 | _.each circles, (circle, i) -> 193 | center = 194 | x: Math.round circle[0] 195 | y: Math.round circle[1] 196 | cv.circle frameOut, center, Math.round(circle[2]), [255, 0, 0], 3, 8 197 | 198 | #Run all effects at once 199 | class NineEffects extends Effect 200 | name: "Nine effects" 201 | constructor: (size, format) -> 202 | @n = 9 203 | @efSize = 204 | width: Math.floor(size.width / 3) 205 | height: Math.floor(size.height / 3) 206 | 207 | @effects = [ 208 | new Mirror(@efSize, format), 209 | new BlackAndWhite(@efSize, format), 210 | new Binarize(@efSize, format), 211 | new Edges(@efSize, format), 212 | new Sepia(@efSize, format), 213 | new Negative, 214 | new Mirror(@efSize, format), 215 | new DetectOrangeBall(@efSize, format), 216 | new RotateImage(@efSize, format) 217 | 218 | ] 219 | 220 | @tmp = new Mat @efSize, cv.CV_8UC4 221 | 222 | run: (frameIn, frameOut) -> 223 | 224 | sFrameIn = cv.resizeImage frameIn, @efSize 225 | 226 | i = 0 227 | row = 0 228 | while row < 3 229 | col = 0 230 | while col < 3 231 | 232 | colStart = col * @efSize.width 233 | colEnd = col * @efSize.width + @efSize.width 234 | rowStart = row * @efSize.height 235 | rowEnd = row * @efSize.height + @efSize.height 236 | part = frameOut.colRange(colStart, colEnd).rowRange(rowStart, rowEnd) 237 | #console.log "#{i}. x = #{colStart}, y = #{rowStart}; width=#{part.width}, height=#{part.height}" 238 | #console.log 'Running ', @effects[i].name 239 | @effects[i].run sFrameIn, part 240 | col = col + 1 241 | i = i + 1 242 | true 243 | row = row + 1 244 | true 245 | 246 | class EffectRunner 247 | constructor: (width, height, format) -> 248 | console.log "Init: #{width}x#{height}:#{format}" 249 | 250 | size = 251 | width: width, 252 | height: height 253 | 254 | cv.namedWindow "output", 0 255 | 256 | @effects = [ 257 | new NineEffects(size, format), 258 | new BlackAndWhite(size, format), 259 | new Binarize(size, format), 260 | new Edges(size, format), 261 | new Sepia(size, format), 262 | new Negative, 263 | new Mirror(size, format), 264 | new DetectOrangeBall(size, format), 265 | new RotateImage(size, format), 266 | ] 267 | 268 | @ei = 0 #effect index 269 | @circleDetector = new CircleDetector 270 | true 271 | 272 | 273 | processFrame: (frameIn, frameOut) -> 274 | if frameIn.type != cv.CV_8UC4 275 | tmp = new Mat 276 | cv.cvtColor frameIn, tmp, cv.CV_RGB2RGBA 277 | else 278 | tmp = frameIn 279 | 280 | 281 | @effects[@ei].runEffect(tmp, frameOut) 282 | 283 | cv.imshow "output", frameOut 284 | true 285 | 286 | nextEffect: -> 287 | @ei = @ei + 1 288 | @ei = @ei % @effects.length 289 | prevEffect: -> 290 | @ei = @ei - 1 291 | if @ei < 0 then @ei = @effects.length - 1 292 | 293 | 294 | 295 | class Capturer 296 | constructor: -> 297 | go: -> 298 | @cap = new cv.VideoCapture 0 299 | if not @cap.isOpened() 300 | console.log 'Couldnt open video device' 301 | return false 302 | frame = new Mat() 303 | @cap.read frame 304 | @outFrame = new Mat {width:frame.width, height:frame.height}, cv.CV_8UC4 305 | @effectRunner = new EffectRunner frame.width, frame.height, frame.type 306 | @nextFrame() 307 | 308 | nextFrame: => 309 | frame = new Mat 310 | @cap.read frame 311 | @effectRunner.processFrame frame, @outFrame 312 | process.nextTick @nextFrame 313 | 314 | 315 | capturer = new Capturer() 316 | capturer.go() 317 | 318 | cv.closeOnEsc() 319 | 320 | process.stdin.on 'keypress', (char, key) -> 321 | if key.name == 'space' or key.name == 'right' 322 | capturer.effectRunner.nextEffect() 323 | else if key.name == 'left' 324 | capturer.effectRunner.prevEffect() 325 | 326 | console.log 'Press ESC to stop.' 327 | console.log 'Press space or right/left keys to cycle through effects' -------------------------------------------------------------------------------- /bea/cv.bea: -------------------------------------------------------------------------------- 1 | @namespace cv 2 | 3 | @static OpenCV 4 | 5 | void GaussianBlur(const Mat& src, Mat& dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=cv::BORDER_DEFAULT) 6 | REQUIRE_SAME_SIZE_TYPE(src, dst); 7 | 8 | void Canny(const Mat& image, Mat& edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false) 9 | apertureSize &= INT_MAX; 10 | REQUIRE_SAME_SIZE_TYPE(image, edges); 11 | THROW_IF_NOT(image->channels() == 1, "Image must have 1 channel"); 12 | THROW_IF_NOT( !((apertureSize & 1) == 0 || apertureSize < 3 || apertureSize > 7 ), "Invalid aperture size"); 13 | 14 | void bilateralFilter(const Mat& src, Mat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=cv::BORDER_DEFAULT) 15 | REQUIRE_SAME_SIZE_TYPE(src, dst); 16 | 17 | void cvtColor(const Mat& src, Mat& dst, int code, int dstCn=0) 18 | 19 | void addWeighted(const Mat& src1, double alpha, const Mat& src2, double beta, double gamma, Mat& dst) 20 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 21 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 22 | 23 | void resize(const Mat& src, Mat& dst, Size dsize, double fx=0, double fy=0, int interpolation=cv::INTER_LINEAR) 24 | THROW_IF_NOT((dsize.width > 0 && dsize.height > 0) || (fx > 0 && fy > 0) , "Invalid size or fx fy"); 25 | THROW_IF_NOT(src->total() > 0 && dst->total() > 0, "One or both images are invalid"); 26 | 27 | 28 | double threshold(const Mat& src, Mat& dst, double thresh, double maxVal, int thresholdType) 29 | REQUIRE_SAME_SIZE_TYPE(src, dst); 30 | 31 | void warpAffine(const Mat& src, Mat& dst, const Mat& M, Size dsize, int flags=cv::INTER_LINEAR, int borderMode=cv::BORDER_CONSTANT, const Scalar& borderValue=cv::Scalar()) 32 | REQUIRE_SAME_TYPE(src, dst); 33 | THROW_IF_NOT (dst->size() == dsize, "Destination matrix must have same size as size parameter"); 34 | THROW_IF_NOT(M->rows == 2 && M->cols == 3, "M matrix must be a 2x3 matrix"); 35 | 36 | Mat getRotationMatrix2D(Point2f center, double angle, double scale) 37 | 38 | void namedWindow(const std::string& winname, int flags) 39 | void imshow(const std::string& winname, const Mat& image) 40 | 41 | void dilate(const Mat& src, Mat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, int borderType=cv::BORDER_CONSTANT, const Scalar& borderValue=cv::morphologyDefaultBorderValue()) 42 | REQUIRE_SAME_SIZE_TYPE(src, dst); 43 | 44 | void erode(const Mat& src, Mat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, int borderType=cv::BORDER_CONSTANT, const Scalar& borderValue=cv::morphologyDefaultBorderValue()) 45 | REQUIRE_SAME_SIZE_TYPE(src, dst); 46 | 47 | void absdiff(const Mat& src1, const Mat& src2, Mat& dst) 48 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 49 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 50 | 51 | void absdiff(const Mat& src1, const Scalar& sc, Mat& dst) 52 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 53 | 54 | void add(const Mat& src1, const Mat& src2, Mat& dst) 55 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 56 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 57 | 58 | void add(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask) 59 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 60 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 61 | THROW_IF_NOT(bea::Convert::Is(args[3]) && mask->channels() == 1, "Mask must be a single-channel 8 bit matrix"); 62 | 63 | void add(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=cv::Mat()) 64 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 65 | THROW_IF_NOT(bea::Convert::Is(args[3]) && mask->channels() == 1, "Mask must be a single-channel 8 bit matrix"); 66 | 67 | 68 | void addWeighted(const Mat& src1, double alpha, const Mat& src2, double beta, double gamma, Mat& dst) 69 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 70 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 71 | 72 | void bitwise_and(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask=cv::Mat()) 73 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 74 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 75 | 76 | void bitwise_and(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=cv::Mat()) 77 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 78 | 79 | void bitwise_not(const Mat& src, Mat& dst) 80 | 81 | void bitwise_or(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask=cv::Mat()) 82 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 83 | 84 | void bitwise_or(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=cv::Mat()) 85 | 86 | void bitwise_xor(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask=cv::Mat()) 87 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 88 | 89 | void bitwise_xor(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=cv::Mat()) 90 | 91 | void calcCovarMatrix(const Mat& samples, Mat& covar, Mat& mean, int flags, int ctype=CV_64F) 92 | THROW_IF_NOT(covar->type() == ctype, "Covar matrix must have same type as ctype"); 93 | 94 | void cartToPolar(const Mat& x, const Mat& y, Mat& magnitude, Mat& angle, bool angleInDegrees=false) 95 | REQUIRE_SAME_SIZE_TYPE(x, y); 96 | REQUIRE_SAME_SIZE_TYPE(x, magnitude); 97 | REQUIRE_SAME_SIZE_TYPE(x, angle); 98 | 99 | 100 | void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop) 101 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 102 | REQUIRE_SAME_SIZE(src1, dst); 103 | THROW_IF_NOT(dst->type() == CV_8UC1, "Destination must have type CV_8UC1"); 104 | 105 | void compare(const Mat& src1, double value, Mat& dst, int cmpop) 106 | REQUIRE_SAME_SIZE(src1, dst); 107 | THROW_IF_NOT(dst->type() == CV_8UC1, "Destination must have type CV_8UC1"); 108 | 109 | void completeSymm(Mat& mtx, bool lowerToUpper=false) 110 | void convertScaleAbs(const Mat& src, Mat& dst, double alpha=1, double beta=0) 111 | int countNonZero(const Mat& mtx) 112 | float cubeRoot(float val) 113 | void dct(const Mat& src, Mat& dst, int flags=0) 114 | REQUIRE_SAME_SIZE_TYPE(src, dst); 115 | 116 | void dft(const Mat& src, Mat& dst, int flags=0, int nonzeroRows=0) 117 | 118 | void divide(double scale, const Mat& src2, Mat& dst) 119 | REQUIRE_SAME_SIZE_TYPE(src2, dst); 120 | 121 | void divide(const Mat& src1, const Mat& src2, Mat& dst, double scale=1) 122 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 123 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 124 | 125 | double determinant(const Mat& mtx) 126 | THROW_IF_NOT(mtx->type() == CV_32FC1 || mtx->type() == CV_64FC1, "Matrix must be of type CV_32FC1 or CV_64FC1 and have square size"); 127 | 128 | bool eigen(const Mat& src, Mat& eigenvalues, int lowindex=-1, int highindex=-1) 129 | 130 | bool eigen(const Mat& src, Mat& eigenvalues, Mat& eigenvectors, int lowindex=-1, int highindex=-1) 131 | REQUIRE_SAME_SIZE_TYPE(src, eigenvectors); 132 | 133 | void exp(const Mat& src, Mat& dst) 134 | float fastAtan2(float y, float x) 135 | 136 | 137 | void flip(const Mat& src, Mat& dst, int flipCode) 138 | REQUIRE_SAME_SIZE_TYPE(src, dst); 139 | 140 | void gemm(const Mat& src1, const Mat& src2, double alpha, const Mat& src3, double beta, Mat& dst, int flags=0) 141 | THROW_IF_NOT(src1 && src2 && src3 && dst, "Invalid matrix argument"); 142 | int type = src1->type(); 143 | THROW_IF_NOT(type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2, "Unsupported matrix type"); 144 | REQUIRE_SAME_TYPE(src1, src2); 145 | REQUIRE_SAME_TYPE(src1, src3); 146 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 147 | 148 | void idct(const Mat& src, Mat& dst, int flags=0) 149 | REQUIRE_SAME_SIZE_TYPE(src, dst); 150 | 151 | void idft(const Mat& src, Mat& dst, int flags=0, int outputRows=0) 152 | //TODO: Check flags and determine if dst type is good 153 | 154 | void inRange(const Mat& src, const Mat& lowerb, const Mat& upperb, Mat& dst) 155 | REQUIRE_SAME_SIZE_TYPE(src, upperb); 156 | REQUIRE_SAME_SIZE_TYPE(src, lowerb); 157 | REQUIRE_SAME_SIZE(src, dst); 158 | THROW_IF_NOT(dst->depth() == CV_8U, "Destination must be a CV_8U matrix"); 159 | 160 | void inRange(const Mat& src, const Scalar& lowerb, const Scalar& upperb, Mat& dst) 161 | REQUIRE_SAME_SIZE(src, dst); 162 | THROW_IF_NOT(dst->depth() == CV_8U, "Destination must be a CV_8U matrix"); 163 | 164 | double invert(const Mat& src, Mat& dst, int method=cv::DECOMP_LU) 165 | REQUIRE_SAME_TYPE(src, dst); 166 | 167 | void log(const Mat& src, Mat& dst) 168 | REQUIRE_SAME_SIZE_TYPE(src, dst); 169 | 170 | void LUT(const Mat& src, const Mat& lut, Mat& dst) 171 | //TODO: check LUT size/channels 172 | THROW_IF_NOT(src->channels() == dst->channels() && dst->depth() == lut->depth(), "Destination matrix must have the same size and the same number of channels as src , and the same depth as lut"); 173 | 174 | void magnitude(const Mat& x, const Mat& y, Mat& magnitude) 175 | REQUIRE_SAME_SIZE(x, y); 176 | REQUIRE_SAME_SIZE_TYPE(x, magnitude); 177 | double Mahalanobis(const Mat& vec1, const Mat& vec2, const Mat& icovar) 178 | 179 | void max(const Mat& src1, double value, Mat& dst) 180 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 181 | 182 | void max(const Mat& src1, const Mat& src2, Mat& dst) 183 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 184 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 185 | 186 | Scalar mean(const Mat& mtx) 187 | Scalar mean(const Mat& mtx, const Mat& mask) 188 | 189 | void meanStdDev(const Mat& mtx, Scalar& mean, Scalar& stddev, const Mat& mask=cv::Mat()) 190 | //TODO: check mask type/size 191 | 192 | void min(const Mat& src1, double value, Mat& dst) 193 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 194 | 195 | void min(const Mat& src1, const Mat& src2, Mat& dst) 196 | REQUIRE_SAME_SIZE_TYPE(src1, dst); 197 | REQUIRE_SAME_SIZE_TYPE(src1, src2); 198 | 199 | void mulSpectrums(const Mat& src1, const Mat& src2, Mat& dst, int flags, bool conj=false) 200 | double norm(const Mat& src1, int normType=cv::NORM_L2) 201 | double norm(const Mat& src1, const Mat& src2, int normType=cv::NORM_L2) 202 | double norm(const Mat& src1, int normType, const Mat& mask) 203 | double norm(const Mat& src1, const Mat& src2, int normType, const Mat& mask) 204 | void multiply(const Mat& src1, const Mat& src2, Mat& dst, double scale=1) 205 | void mulTransposed(const Mat& src, Mat& dst, bool aTa, const Mat& delta=cv::Mat(), double scale=1, int rtype=-1) 206 | void normalize(const Mat& src, Mat& dst, double alpha=1, double beta=0, int normType=cv::NORM_L2, int rtype=-1, const Mat& mask=cv::Mat()) 207 | void perspectiveTransform(const Mat& src, Mat& dst, const Mat& mtx) 208 | void phase(const Mat& x, const Mat& y, Mat& angle, bool angleInDegrees=false) 209 | void polarToCart(const Mat& magnitude, const Mat& angle, Mat& x, Mat& y, bool angleInDegrees=false) 210 | void pow(const Mat& src, double p, Mat& dst) 211 | void randu(Mat& mtx, const Scalar& low, const Scalar& high) 212 | void randn(Mat& mtx, const Scalar& mean, const Scalar& stddev) 213 | void reduce(const Mat& mtx, Mat& vec, int dim, int reduceOp, int dtype=-1) 214 | void repeat(const Mat& src, int ny, int nx, Mat& dst) 215 | void scaleAdd(const Mat& src1, double scale, const Mat& src2, Mat& dst) 216 | void setIdentity(Mat& dst, const Scalar& value=Scalar(1)) 217 | bool solve(const Mat& src1, const Mat& src2, Mat& dst, int flags=cv::DECOMP_LU) 218 | void solveCubic(const Mat& coeffs, Mat& roots) 219 | void solvePoly(const Mat& coeffs, Mat& roots, int maxIters=20) 220 | void sort(const Mat& src, Mat& dst, int flags) 221 | void sortIdx(const Mat& src, Mat& dst, int flags) 222 | void sqrt(const Mat& src, Mat& dst) 223 | void subtract(const Mat& src1, const Mat& src2, Mat& dst) 224 | void subtract(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask) 225 | void subtract(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=cv::Mat()) 226 | void subtract(const Scalar& sc, const Mat& src2, Mat& dst, const Mat& mask=cv::Mat()) 227 | Scalar sum(const Mat& mtx) 228 | Scalar trace(const Mat& mtx) 229 | void transform(const Mat& src, Mat& dst, const Mat& mtx) 230 | void transpose(const Mat& src, Mat& dst) 231 | void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 232 | bool clipLine(Size imgSize, Point& pt1, Point& pt2) 233 | bool clipLine(Rect imgRect, Point& pt1, Point& pt2) 234 | void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 235 | void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=8) 236 | void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 237 | void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 238 | void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false) 239 | void equalizeHist(const Mat& src, Mat& dst) 240 | void ellipse2Poly(Point center, Size axes, int angle, int startAngle, int endAngle, int delta, std::vector& pts) 241 | 242 | bool checkRange(const Mat& src, bool quiet=true, std::vector pos = std::vector(), double minVal=-DBL_MAX, double maxVal=DBL_MAX) 243 | //bool checkRange(const Mat& src, bool quiet=true, Point* pos=0, double minVal=-DBL_MAX, double maxVal=DBL_MAX) 244 | @call bool fnRetVal = cv::checkRange(*src, quiet, pos.size() ? &pos[0] : NULL, minVal, maxVal); 245 | 246 | void merge(std::vector mv, Mat& dst) 247 | //void merge(const Mat* mv, size_t count, Mat& dst) 248 | THROW_IF_NOT(mv.size() > 0, "Empty array"); 249 | 250 | std::vector split(const Mat& mtx) 251 | @call 252 | std::vector fnRetVal; 253 | cv::split(*mtx, fnRetVal); 254 | 255 | 256 | void fillConvexPoly(Mat& img, const std::vector &pts, const Scalar& color, int lineType=8, int shift=0) 257 | //calling void fillConvexPoly(Mat& img, const Point* pts, int npts, const Scalar& color, int lineType=8, int shift=0) 258 | @call cv::fillConvexPoly(*img, &pts[0], pts.size(), color, lineType, shift); 259 | 260 | Mat resizeImage(const Mat& src, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR ) 261 | @call 262 | cv::Mat tmp; 263 | cv::resize(*src, tmp, dsize, fx, fy, interpolation); 264 | cv::Mat* fnRetVal = new Mat(tmp); 265 | 266 | 267 | #HighGUI 268 | Mat imdecode(const Mat& buf, int flags) 269 | Mat imread(const std::string& filename, int flags=1) 270 | @call 271 | cv::Mat tmp = cv::imread(filename, flags); 272 | THROW_IF_NOT(tmp.total() > 0, "Couldn't load file"); 273 | cv::Mat* fnRetVal = new Mat(tmp); 274 | 275 | bool imwrite(const std::string& filename, const Mat& img, const std::vector& params=std::vector()) 276 | int waitKey(int delay = 0) 277 | 278 | std::vector HoughCircles(Mat& image, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0) 279 | @call 280 | std::vector fnRetVal; 281 | cv::HoughCircles(*image, fnRetVal, method, dp, minDist, param1, param2, minRadius, maxRadius); 282 | 283 | 284 | std::vector HoughLines(Mat& image, double rho, double theta, int threshold, double srn=0, double stn=0) 285 | @call 286 | std::vector fnRetVal; 287 | cv::HoughLines(*image, fnRetVal, rho, theta, threshold, srn, stn); 288 | 289 | void preCornerDetect(const Mat& src, Mat& dst, int apertureSize, int borderType=BORDER_DEFAULT) 290 | void goodFeaturesToTrack(const Mat& image, std::vector& corners, int maxCorners, double qualityLevel, double minDistance, const Mat& mask=Mat(), int blockSize=3, bool useHarrisDetector=false, double k=0.04) 291 | std::vector cornerSubPix(const Mat& image, Size winSize, Size zeroZone, TermCriteria criteria) 292 | @call 293 | std::vector fnRetVal; 294 | cv::cornerSubPix(*image, fnRetVal, winSize, zeroZone, criteria); 295 | 296 | void cornerMinEigenVal(const Mat& src, Mat& dst, int blockSize, int apertureSize=3, int borderType=BORDER_DEFAULT) 297 | void cornerHarris(const Mat& src, Mat& dst, int blockSize, int apertureSize, double k, int borderType=BORDER_DEFAULT) 298 | void cornerEigenValsAndVecs(const Mat& src, Mat& dst, int blockSize, int apertureSize, int borderType=BORDER_DEFAULT) 299 | 300 | void calcHist(std::vector arrays, std::vector channels, const Mat& mask, cv::Mat& hist, int dims, std::vector histSize, std::vector > ranges, bool uniform = true, bool accumulate = false) 301 | #@orgapi void calcHist(const Mat* arrays, int narrays, const int* channels, const Mat& mask, SparseMat& hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false) 302 | @call 303 | 304 | std::vector vranges = std::vector(ranges.size()); 305 | for (int k = 0 ; k < ranges.size(); k++){ 306 | vranges[k] = &((ranges[k])[0]); 307 | } 308 | 309 | cv::calcHist(&arrays[0], arrays.size(), &channels[0], *mask, *hist, dims, &histSize[0], (const float**)&vranges[0], uniform, accumulate); 310 | 311 | void calcBackProject(std::vector arrays, std::vector channels, const SparseMat& hist, Mat& backProject, std::vector > ranges, double scale = 1, bool uniform = true) 312 | #@orgapi void calcBackProject(const Mat* arrays, int narrays, const int* channels, const SparseMat& hist, Mat& backProject, const float** ranges, double scale=1, bool uniform=true) 313 | @call 314 | std::vector vranges = std::vector(ranges.size()); 315 | for (int k = 0 ; k < ranges.size(); k++){ 316 | vranges[k] = &((ranges[k])[0]); 317 | } 318 | cv::calcBackProject(&arrays[0], arrays.size(), &channels[0], *hist, *backProject, (const float**)&vrangs[0], scale, uniform); 319 | 320 | minMaxLocRet minMaxLoc(const Mat& a); 321 | #@orgapi void minMaxLoc(const Mat& a, double* minVal, double* maxVal, int* minIdx=0, int* maxIdx=0 ) 322 | @call 323 | minMaxLocRet fnRetVal; 324 | cv::minMaxLoc(*a, &fnRetVal.minVal, &fnRetVal.maxVal, NULL, NULL); 325 | 326 | @manual void cvSmooth(const Mat& src, Mat& dst, int smoothtype=CV_GAUSSIAN, int param1=3, int param2=0, double param3=0, double param4=0) 327 | 328 | 329 | #Manual functions 330 | @manual void doTick() #windows only 331 | @manual void discardMats() 332 | @manual void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point()) 333 | @manual Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine) 334 | @manual void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0) 335 | @manual double kmeans(const Mat& samples, int clusterCount, Mat& labels, TermCriteria termcrit, int attempts, int flags, Mat* centers) 336 | @manual void calcBackProject(const Mat* arrays, int narrays, const int* channels, const SparseMat& hist, Mat& backProject, const float** ranges, double scale=1, bool uniform=true) 337 | @manual std::vector detectObject(const std::string& cascadeName, int imageWidth, int imageHeight) -------------------------------------------------------------------------------- /src/bea.h: -------------------------------------------------------------------------------- 1 | #ifndef __BEA_H__ 2 | #define __BEA_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace bea{ 12 | class Exception{ 13 | protected: 14 | v8::Handle m_exception; 15 | 16 | public: 17 | Exception(){} 18 | Exception(const char* message){ 19 | m_exception = v8::ThrowException(v8::Exception::TypeError(v8::String::NewSymbol(message))); 20 | } 21 | inline v8::Handle v8exception(){ 22 | return m_exception; 23 | } 24 | }; 25 | 26 | class ArgConvertException : public Exception{ 27 | public: 28 | ArgConvertException(int arg, const char* message){ 29 | std::stringstream s; 30 | s << "Argument " << arg << ": " << message; 31 | m_exception = v8::ThrowException(v8::Exception::TypeError(v8::String::NewSymbol(s.str().c_str()))); 32 | } 33 | }; 34 | 35 | ////////////////////////////////////////////////////////////////////////// 36 | 37 | #define BEATHROW() throw bea::ArgConvertException(nArg, msg) 38 | 39 | class Indexable{ 40 | public: 41 | void setPtr(void* ptr, int size, int type){} 42 | }; 43 | 44 | class BeaBuffer{ 45 | char* m_buffer; 46 | int m_size; 47 | int m_type; 48 | public: 49 | inline BeaBuffer(int size, int type){ 50 | m_buffer = new char[size]; 51 | m_size = size; 52 | m_type = type; 53 | } 54 | inline ~BeaBuffer(){ 55 | delete[] m_buffer; 56 | } 57 | 58 | inline void* ptr(){ 59 | return m_buffer; 60 | } 61 | 62 | inline int size(){ 63 | return m_size; 64 | } 65 | 66 | inline int type(){ 67 | return m_type; 68 | } 69 | 70 | 71 | }; 72 | 73 | 74 | template 75 | class vector : public std::vector 76 | { 77 | public: 78 | operator T* (){ 79 | return &(this->operator[](0)); 80 | } 81 | }; 82 | 83 | 84 | class string: public std::string{ 85 | public: 86 | string(){} 87 | string(const char* s): std::string(s){} 88 | 89 | operator const char* (){ 90 | return this->c_str(); 91 | } 92 | }; 93 | 94 | template 95 | class external{ 96 | protected: 97 | external(){} 98 | public: 99 | void* ptr; 100 | external(T* p): ptr(p){ 101 | 102 | } 103 | operator T*(){ 104 | return (T*)ptr; 105 | } 106 | }; 107 | 108 | 109 | template 110 | struct Convert{ 111 | static bool Is(v8::Handle v); 112 | static T FromJS(v8::Handle v, int nArg); 113 | static v8::Handle ToJS(const T& val); 114 | }; 115 | 116 | template 117 | struct Optional{ 118 | 119 | static inline T FromJS(const v8::Arguments& args, int nArg, const T& def){ 120 | if (args.Length() > nArg) 121 | return Convert::FromJS(args[nArg], nArg); 122 | return def; 123 | } 124 | 125 | static inline bool Is(const v8::Arguments& args, int nArg){ 126 | if (args.Length() > nArg) 127 | return Convert::Is(args[nArg]); 128 | return true; 129 | } 130 | }; 131 | 132 | //Int 133 | template<> 134 | struct Convert { 135 | 136 | static inline bool Is(v8::Handle v){ 137 | return !v.IsEmpty() && v->IsInt32(); 138 | } 139 | 140 | static inline int FromJS(v8::Handle v, int nArg){ 141 | static const char* msg = "Integer expected"; 142 | if (!Is(v)) 143 | BEATHROW(); 144 | return v->Int32Value(); 145 | } 146 | 147 | static v8::Handle ToJS(const int& val){ 148 | return v8::Integer::New(val); 149 | } 150 | }; 151 | 152 | //Double 153 | template<> 154 | struct Convert{ 155 | static inline bool Is(v8::Handle v){ 156 | return (!v.IsEmpty() && v->IsNumber()); 157 | } 158 | 159 | static inline double FromJS(v8::Handle v, int nArg){ 160 | static const char* msg = "Number expected"; 161 | 162 | if (!Is(v)) 163 | BEATHROW(); 164 | 165 | return v->NumberValue(); 166 | } 167 | 168 | static inline v8::Handle ToJS(const double& val){ 169 | return v8::Number::New(val); 170 | } 171 | }; 172 | 173 | //Float 174 | template<> 175 | struct Convert{ 176 | static inline bool Is(v8::Handle v){ 177 | return Convert::Is(v); 178 | } 179 | static inline float FromJS(v8::Handle v, int nArg){ 180 | return (float)Convert::FromJS(v, nArg); 181 | } 182 | static inline v8::Handle ToJS(const float& val){ 183 | return Convert::ToJS(val); 184 | } 185 | }; 186 | 187 | 188 | //Bool 189 | template<> 190 | struct Convert{ 191 | static inline bool Is(v8::Handle v){ 192 | return !v.IsEmpty() && v->IsBoolean(); 193 | } 194 | static inline bool FromJS(v8::Handle v, int nArg){ 195 | 196 | static const char* msg = "Boolean value expected"; 197 | if (!Is(v)) BEATHROW(); 198 | 199 | return v->BooleanValue(); 200 | } 201 | 202 | static inline v8::Handle ToJS(const bool& val){ 203 | return v8::Boolean::New(val); 204 | } 205 | }; 206 | 207 | //bea::string 208 | template<> 209 | struct Convert{ 210 | static inline bool Is(v8::Handle v){ 211 | return !v.IsEmpty() && v->IsString(); 212 | } 213 | 214 | static inline bea::string FromJS(v8::Handle v, int nArg){ 215 | static const char* msg = "v8::String expected"; 216 | 217 | if (!Is(v)) 218 | BEATHROW(); 219 | 220 | return *v8::String::AsciiValue(v->ToString()); 221 | } 222 | 223 | static inline v8::Handle ToJS(const bea::string& val){ 224 | return v8::String::New (val.c_str()); 225 | } 226 | }; 227 | 228 | //std::string 229 | template<> 230 | struct Convert{ 231 | static bool Is(v8::Handle v){ 232 | return Convert::Is(v); 233 | } 234 | 235 | static inline std::string FromJS(v8::Handle v, int nArg){ 236 | return Convert::FromJS(v, nArg); 237 | } 238 | 239 | static inline v8::Handle ToJS(const std::string& val){ 240 | return Convert::ToJS((const bea::string&)val); 241 | } 242 | }; 243 | 244 | //bea::vector 245 | template 246 | struct Convert >{ 247 | 248 | static inline bool Is(v8::Handle v){ 249 | return !v.IsEmpty() && v->IsArray(); 250 | } 251 | 252 | static inline bea::vector FromJS(v8::Handle v, int nArg){ 253 | 254 | static const char* msg = "Array expected"; 255 | 256 | if (!Is(v)) BEATHROW(); 257 | 258 | bea::vector ret; 259 | 260 | v8::Local array = v8::Array::Cast(*v); 261 | 262 | size_t len = (size_t)array->Length() ; 263 | 264 | for (size_t k = 0; k < len; k++) 265 | { 266 | ret.push_back(Convert::FromJS(array->Get((int32_t)k), nArg)); 267 | } 268 | 269 | return ret; 270 | } 271 | 272 | static inline v8::Handle ToJS(const bea::vector& val){ 273 | 274 | v8::HandleScope scope; 275 | int len = (int)val.size(); 276 | v8::Local jsArray = v8::Array::New(len); 277 | 278 | for (int i = 0; i < len; i++) 279 | jsArray->Set(i, Convert::ToJS(val[i])); 280 | 281 | return scope.Close(jsArray); 282 | 283 | } 284 | }; 285 | 286 | //std::vector 287 | template 288 | struct Convert >{ 289 | static bool Is(v8::Handle v){ 290 | return Convert >::Is(v); 291 | } 292 | 293 | static inline std::vector FromJS(v8::Handle v, int nArg){ 294 | return Convert >::FromJS(v, nArg); 295 | } 296 | 297 | static inline v8::Handle ToJS(const std::vector& val){ 298 | return Convert >::ToJS((const bea::vector&)val); 299 | } 300 | }; 301 | 302 | ///??? 303 | template<> 304 | struct Convert{ 305 | static inline bool Is(v8::Handle v){ 306 | return !v.IsEmpty() && v->IsInt32(); 307 | } 308 | 309 | static inline char FromJS(v8::Handle v, int nArg){ 310 | static const char* msg = "Integer(byte) value expected"; 311 | if (!Is(v)) BEATHROW(); 312 | 313 | return (char)v->Int32Value(); 314 | } 315 | 316 | static inline v8::Handle ToJS(const char& val){ 317 | return v8::Int32::New(val); 318 | } 319 | }; 320 | //unsigned char 321 | template<> 322 | struct Convert{ 323 | static inline bool Is(v8::Handle v){ 324 | return !v.IsEmpty() && v->IsUint32(); 325 | } 326 | 327 | static inline unsigned char FromJS(v8::Handle v, int nArg){ 328 | static const char* msg = "Integer(byte) value expected"; 329 | if (!Is(v)) BEATHROW(); 330 | 331 | return (unsigned char)v->Uint32Value(); 332 | } 333 | 334 | static inline v8::Handle ToJS(const unsigned char& val){ 335 | return v8::Uint32::New(val); 336 | } 337 | }; 338 | //short 339 | template <> 340 | struct Convert{ 341 | static inline bool Is(v8::Handle v){ 342 | return !v.IsEmpty() && v->IsInt32(); 343 | } 344 | static inline short FromJS(v8::Handle v, int nArg){ 345 | static const char* msg = "Integer(byte) value expected"; 346 | if (!Is(v)) BEATHROW(); 347 | return v->Int32Value() & 0xffff; 348 | } 349 | 350 | static inline v8::Handle ToJS(const short& val){ 351 | return v8::Int32::New(val); 352 | } 353 | }; 354 | //unsigned short 355 | template <> 356 | struct Convert{ 357 | static inline bool Is(v8::Handle v){ 358 | return !v.IsEmpty() && v->IsUint32(); 359 | } 360 | static inline unsigned short FromJS(v8::Handle v, int nArg){ 361 | static const char* msg = "Integer(byte) value expected"; 362 | if (!Is(v)) BEATHROW(); 363 | return v->Uint32Value() & 0xffff; 364 | } 365 | 366 | static inline v8::Handle ToJS(const unsigned short& val){ 367 | return v8::Uint32::New(val); 368 | } 369 | }; 370 | 371 | template <> 372 | struct Convert{ 373 | static inline bool Is(v8::Handle v){ 374 | return !v.IsEmpty() && v->IsUint32(); 375 | } 376 | static inline unsigned int FromJS(v8::Handle v, int nArg){ 377 | static const char* msg = "Integer(byte) value expected"; 378 | if (!Is(v)) BEATHROW(); 379 | return v->Uint32Value(); 380 | } 381 | 382 | static inline v8::Handle ToJS(const unsigned int& val){ 383 | return v8::Uint32::New(val); 384 | } 385 | }; 386 | 387 | template<> struct Convert { 388 | static bool Is(v8::Handle v) { 389 | return bea::Convert::Is(v); 390 | } 391 | 392 | static unsigned long FromJS(v8::Handle v, int nArg) { 393 | return (unsigned long)bea::Convert::FromJS(v, nArg); 394 | } 395 | 396 | static v8::Handle ToJS(unsigned long const& v) { 397 | return bea::Convert::ToJS(v); 398 | } 399 | 400 | }; 401 | 402 | template<> struct Convert { 403 | static bool Is(v8::Handle v) { 404 | return bea::Convert::Is(v); 405 | } 406 | 407 | static unsigned long FromJS(v8::Handle v, int nArg) { 408 | return (long)bea::Convert::FromJS(v, nArg); 409 | } 410 | 411 | static v8::Handle ToJS(long const& v) { 412 | return bea::Convert::ToJS((int const&)v); 413 | } 414 | 415 | }; 416 | 417 | 418 | typedef void (*reportExceptionCb)(v8::TryCatch&); 419 | 420 | struct Global{ 421 | static v8::Persistent externalTemplate; 422 | static std::string scriptDir; 423 | static reportExceptionCb reportException; 424 | 425 | static void InitExternalTemplate(){ 426 | v8::HandleScope scope; 427 | v8::Handle otmpl = v8::ObjectTemplate::New(); 428 | otmpl->SetInternalFieldCount(2); 429 | externalTemplate = v8::Persistent::New(otmpl); 430 | } 431 | }; 432 | 433 | template 434 | struct IndexType{ 435 | enum {Value = 0}; 436 | }; 437 | 438 | template<> struct IndexType{ 439 | enum {Value = v8::kExternalUnsignedByteArray}; 440 | }; 441 | template<> struct IndexType{ 442 | enum {Value = v8::kExternalByteArray}; 443 | }; 444 | template<> struct IndexType{ 445 | enum {Value = v8::kExternalShortArray}; 446 | }; 447 | template<> struct IndexType{ 448 | enum {Value = v8::kExternalUnsignedShortArray}; 449 | }; 450 | template<> struct IndexType{ 451 | enum {Value = v8::kExternalIntArray}; 452 | }; 453 | template<> struct IndexType{ 454 | enum {Value = v8::kExternalUnsignedIntArray}; 455 | }; 456 | template<> struct IndexType{ 457 | enum {Value = v8::kExternalFloatArray}; 458 | }; 459 | 460 | 461 | template 462 | struct Convert >{ 463 | static bool Is(v8::Handle v){ 464 | return !v.IsEmpty() && v->IsExternal(); 465 | } 466 | 467 | static external FromJS(v8::Handle v, int nArg){ 468 | const char* msg = "Externally allocated buffer expected"; 469 | if (!Is(v)) BEATHROW(); 470 | v8::Handle ext = v8::Handle::Cast(v); 471 | 472 | return external(static_cast(ext->Value())); 473 | } 474 | 475 | static v8::Handle ToJS(const external& val){ 476 | return v8::External::New(val.ptr); 477 | } 478 | }; 479 | 480 | #if 0 481 | template 482 | struct Convert >{ 483 | static bool Is(v8::Handle v){ 484 | v8::Handle obj = v->ToObject(); 485 | return !v.IsEmpty() && 486 | v->IsObject() && 487 | obj->InternalFieldCount() == 2 && 488 | (int)obj->GetPointerFromInternalField(1) == (int)IndexType::Value; 489 | } 490 | 491 | static external FromJS(v8::Handle v, int nArg){ 492 | const char* msg = "Externally allocated buffer expected"; 493 | if (!Is(v)) BEATHROW(); 494 | v8::Handle thisObj = v->ToObject(); 495 | 496 | void* ptr = thisObj->GetPointerFromInternalField(0); 497 | 498 | return external(static_cast(ptr)); 499 | } 500 | 501 | static v8::Handle ToJS(const external& val){ 502 | //return v8::External::New(val.ptr); 503 | 504 | v8::HandleScope scope; 505 | v8::Handle obj = Global::externalTemplate->NewInstance(); 506 | 507 | obj->SetPointerInInternalField(0, val.ptr); 508 | obj->SetPointerInInternalField(1, (void*)(int)IndexType::Value); 509 | return scope.Close(obj); 510 | } 511 | }; 512 | #endif 513 | 514 | 515 | 516 | 517 | 518 | 519 | ////////////////////////////////////////////////////////////////////////// 520 | 521 | 522 | template 523 | class ExposedClass { 524 | private: 525 | v8::Persistent function_template; 526 | std::string m_objectName; 527 | v8::InvocationCallback m_constructor; 528 | v8::InvocationCallback m_postAlloc; 529 | typedef void (*DestructorCallback)(v8::Handle val); 530 | 531 | DestructorCallback m_destructor; 532 | 533 | public: 534 | static ExposedClass * Instance; 535 | 536 | //Constructor: objectName is the name in Javascript 537 | inline ExposedClass( const char* objectName ) { 538 | v8::HandleScope scope; 539 | m_objectName = objectName; 540 | v8::Local vData = v8::External::New(this); 541 | v8::Local t = v8::FunctionTemplate::New(New, vData); 542 | function_template = v8::Persistent::New(t); 543 | function_template->InstanceTemplate()->SetInternalFieldCount(2); 544 | function_template->SetClassName(v8::String::NewSymbol(objectName)); 545 | m_constructor = NULL; 546 | m_postAlloc = NULL; 547 | m_destructor = NULL; 548 | } 549 | inline ~ExposedClass(){ 550 | } 551 | 552 | //Expose a method to Javascript. 553 | inline void exposeMethod( const char* name, v8::InvocationCallback cb ) { 554 | v8::HandleScope scope; 555 | v8::Local fn = v8::FunctionTemplate::New(cb); 556 | function_template->PrototypeTemplate()->Set(v8::String::NewSymbol(name), fn); 557 | } 558 | 559 | //Expose a property to javascript 560 | inline void exposeProperty(const char* name, v8::AccessorGetter get, v8::AccessorSetter set){ 561 | function_template->InstanceTemplate()->SetAccessor(v8::String::New(name), get, set); 562 | } 563 | 564 | inline void exposeTo( v8::Handle target ){ 565 | target->Set(v8::String::NewSymbol(m_objectName.c_str()), function_template->GetFunction()); 566 | } 567 | 568 | //Called when the garbage collector decides to dispose of value 569 | static inline void WeakCallback (v8::Persistent value, void *data) { 570 | ExposedClass* _this = static_cast*>(data); 571 | v8::HandleScope scope; 572 | v8::Local o = value->ToObject(); 573 | void* p = o->GetPointerFromInternalField(0); 574 | 575 | if (p != NULL){ 576 | if (_this->m_destructor) 577 | _this->m_destructor(value); 578 | else{ 579 | delete static_cast(p); 580 | } 581 | 582 | } 583 | value.Dispose(); 584 | } 585 | 586 | //Called by javascript when issuing a 'new MyObject' or by a NewInstance() issued by ToJS() 587 | 588 | inline v8::Handle createNew( const v8::Arguments& args ) { 589 | v8::HandleScope scope; 590 | 591 | v8::Handle ext; 592 | v8::Handle res; 593 | 594 | if (args[0]->IsExternal()) 595 | ext = v8::Handle::Cast(args[0]); 596 | else 597 | { 598 | assert(m_constructor != NULL && "Constructor not set!"); 599 | 600 | res = m_constructor(args); 601 | 602 | if (res->IsExternal()) 603 | ext = v8::Handle::Cast(res); 604 | else 605 | res = ThrowException(v8::Exception::TypeError(v8::String::NewSymbol("Invalid constructor parameters"))); 606 | 607 | } 608 | 609 | if (!ext.IsEmpty()){ 610 | args.This()->SetInternalField(0, ext); 611 | 612 | if (m_destructor){ 613 | v8::Persistent persObj = v8::Persistent::New(args.This()); 614 | persObj.MakeWeak(this, WeakCallback); 615 | } 616 | 617 | if (m_postAlloc) 618 | m_postAlloc(args); 619 | 620 | return scope.Close(args.This()); 621 | } 622 | 623 | return scope.Close(res); 624 | } 625 | 626 | static inline v8::Handle New( const v8::Arguments& args ) { 627 | v8::HandleScope scope; 628 | v8::Local edata = v8::Local::Cast(args.Data()); 629 | ExposedClass* that = static_cast(edata->Value()); 630 | return that->createNew(args); 631 | } 632 | 633 | static inline bool Is( v8::Handle v ) { 634 | if (v.IsEmpty() || !v->IsObject()) 635 | return false; 636 | 637 | v8::Local o = v->ToObject(); 638 | 639 | int nField = o->InternalFieldCount(); 640 | bool isExt = v->IsExternal(); 641 | 642 | if (o->InternalFieldCount() < 2) 643 | return false; 644 | 645 | void* p = o->GetPointerFromInternalField(1); 646 | return true; 647 | } 648 | 649 | static inline v8::Handle ToJS( T* value ){ 650 | ExposedClass* inst = ExposedClass::Instance; 651 | v8::HandleScope scope; 652 | v8::Handle cons = inst->function_template->GetFunction(); 653 | v8::Handle ext = v8::External::New(value); 654 | 655 | v8::Handle argv[1] = {ext}; 656 | 657 | v8::Handle res = cons->NewInstance(1, argv); 658 | 659 | return scope.Close(res); 660 | } 661 | 662 | void inline setConstructor( v8::InvocationCallback cb ) { 663 | m_constructor = cb; 664 | } 665 | 666 | void inline setPostAllocator(v8::InvocationCallback cb){ 667 | m_postAlloc = cb; 668 | } 669 | 670 | void inline setDestructor(DestructorCallback cb){ 671 | m_destructor = cb; 672 | } 673 | 674 | static inline T* FromJS(v8::Handle v, int nArg) { 675 | const char* msg = "Wrapped object expected"; 676 | 677 | if (!Is(v)) 678 | throw bea::ArgConvertException(nArg, msg); 679 | 680 | v8::HandleScope scope; 681 | v8::Local obj = v->ToObject(); 682 | void * p = obj->GetPointerFromInternalField(0); 683 | 684 | return static_cast(p); 685 | } 686 | 687 | }; 688 | 689 | template 690 | class ExposedStatic{ 691 | static v8::Persistent oTemplate; 692 | v8::Handle m_obj; 693 | std::string m_objName; 694 | public: 695 | 696 | static inline void Init(){ 697 | v8::HandleScope handle_scope; 698 | v8::Handle obj_templ = v8::ObjectTemplate::New(); 699 | obj_templ->SetInternalFieldCount(1); 700 | oTemplate = v8::Persistent::New(obj_templ); 701 | } 702 | 703 | static inline ExposedStatic* Create(T* ptr, const char* objectName){ 704 | 705 | if (oTemplate.IsEmpty()) 706 | Init(); 707 | 708 | return new ExposedStatic(ptr, objectName); 709 | } 710 | 711 | inline ExposedStatic(T* ptr, const char* objectName){ 712 | m_obj = oTemplate->NewInstance(); 713 | m_obj->SetInternalField(0, v8::External::New(ptr)); 714 | m_objName = objectName; 715 | } 716 | 717 | inline void exposeMethod(const char* name, v8::InvocationCallback cb){ 718 | m_obj->Set(v8::String::NewSymbol(name), v8::FunctionTemplate::New(cb)->GetFunction()); 719 | } 720 | 721 | inline void exposeTo(v8::Handle target){ 722 | target->Set(v8::String::NewSymbol(m_objName.c_str()), m_obj); 723 | } 724 | }; 725 | } 726 | 727 | 728 | namespace bea{ 729 | class DerivedClass{ 730 | protected: 731 | v8::Persistent __jsInstance; 732 | ~DerivedClass(){ 733 | __jsInstance.Dispose(); 734 | } 735 | 736 | v8::Handle bea_derived_callJS(const char* name, int nargs, v8::Handle args[]){ 737 | 738 | //Enter the javascript context - this is a call from native code 739 | //v8::Context::Scope contextScope(Handle::Cast(Global::context)); 740 | //Global::context->Enter(); 741 | 742 | 743 | v8::HandleScope scope; 744 | v8::Handle result; 745 | v8::Handle oFn = __jsInstance->Get(v8::String::New(name)); 746 | 747 | if (!oFn.IsEmpty() && oFn->IsFunction()){ 748 | v8::Handle fn = v8::Handle::Cast(oFn); 749 | v8::TryCatch try_catch; 750 | result = fn->Call(__jsInstance, nargs, args); 751 | if (result.IsEmpty()) 752 | bea::Global::reportException(try_catch); 753 | } 754 | 755 | //Global::context->Exit(); 756 | 757 | return scope.Close(result); 758 | } 759 | 760 | bool bea_derived_hasOverride(const char* name){ 761 | return __jsInstance->HasRealNamedProperty(v8::String::NewSymbol(name)); 762 | } 763 | public: 764 | void bea_derived_setInstance(v8::Handle obj){ 765 | __jsInstance = v8::Persistent::New(obj); 766 | } 767 | }; 768 | } 769 | 770 | #define DECLARE_EXPOSED_CLASS(typeName) template<> bea::ExposedClass* bea::ExposedClass::Instance = NULL 771 | #define EXPOSE_CLASS(typeName, jsName) bea::ExposedClass::Instance = new bea::ExposedClass(jsName) 772 | 773 | #define DECLARE_STATIC(typeName) template<> v8::Persistent bea::ExposedStatic::oTemplate = v8::Persistent() 774 | #define EXPOSE_STATIC(typeName, jsName) bea::ExposedStatic::Create(new (typeName), jsName) 775 | 776 | //Throw if number of arguments is smaller than n 777 | #define REQUIRE_ARGS(args, n) if ((args).Length() < (n)) return v8::ThrowException(v8::Exception::TypeError(v8::String::NewSymbol("Wrong number of arguments"))) 778 | 779 | //Every method accessible by javascript must start with this macro 780 | #define METHOD_BEGIN(nArgs) REQUIRE_ARGS(args, (nArgs)); try { 781 | #define DESTRUCTOR_BEGIN() try{ 782 | #define DESTRUCTOR_END() } catch(bea::ArgConvertException& ){ } 783 | 784 | //Every method must end with this macro 785 | #define METHOD_END() } catch(bea::ArgConvertException& e){ return e.v8exception();} 786 | 787 | //Copied from NODE_DEFINE_CONSTANT in node.js 788 | #define BEA_DEFINE_CONSTANT(target, constant) \ 789 | (target)->Set(v8::String::NewSymbol(#constant), \ 790 | v8::Integer::New(constant), \ 791 | static_cast(v8::ReadOnly|v8::DontDelete)) 792 | 793 | //Copied from NODE_SET_METHOD in node.js 794 | #define BEA_SET_METHOD(obj, name, callback) \ 795 | obj->Set(v8::String::NewSymbol(name), \ 796 | v8::FunctionTemplate::New(callback)->GetFunction()) 797 | 798 | 799 | 800 | #endif //__BEA_H__ 801 | -------------------------------------------------------------------------------- /src/opencvjs.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENCVJS_H 2 | #define OPENCVJS_H 3 | #include 4 | #include "cvcheck.h" 5 | #include 6 | #include 7 | #include "bea.h" 8 | #include "customTypes.h" 9 | namespace bea { 10 | template<> struct Convert { 11 | static bool Is(v8::Handle v) { 12 | return !v.IsEmpty() && v->IsObject(); 13 | } 14 | 15 | static cv::TermCriteria FromJS(v8::Handle v, int nArg) { 16 | const char* msg = "Object with the following properties expected: type, maxCount, epsilon. This will be cast to 'cv::TermCriteria'"; 17 | if (!Is(v)) BEATHROW(); 18 | v8::HandleScope scope; 19 | v8::Local obj = v->ToObject(); 20 | cv::TermCriteria ret; 21 | ret.type = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("type")), nArg); 22 | ret.maxCount = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("maxCount")), nArg); 23 | ret.epsilon = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("epsilon")), nArg); 24 | return ret; 25 | } 26 | 27 | static v8::Handle ToJS(cv::TermCriteria const& v) { 28 | v8::HandleScope scope; 29 | v8::Local obj = v8::Object::New(); 30 | obj->Set(v8::String::NewSymbol("type"), bea::Convert::ToJS(v.type)); 31 | obj->Set(v8::String::NewSymbol("maxCount"), bea::Convert::ToJS(v.maxCount)); 32 | obj->Set(v8::String::NewSymbol("epsilon"), bea::Convert::ToJS(v.epsilon)); 33 | return scope.Close(obj); 34 | } 35 | 36 | }; 37 | 38 | template<> struct Convert { 39 | static bool Is(v8::Handle v) { 40 | return !v.IsEmpty() && v->IsObject(); 41 | } 42 | 43 | static cv::Point FromJS(v8::Handle v, int nArg) { 44 | const char* msg = "Object with the following properties expected: x, y. This will be cast to 'cv::Point'"; 45 | if (!Is(v)) BEATHROW(); 46 | v8::HandleScope scope; 47 | v8::Local obj = v->ToObject(); 48 | cv::Point ret; 49 | ret.x = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("x")), nArg); 50 | ret.y = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("y")), nArg); 51 | return ret; 52 | } 53 | 54 | static v8::Handle ToJS(cv::Point const& v) { 55 | v8::HandleScope scope; 56 | v8::Local obj = v8::Object::New(); 57 | obj->Set(v8::String::NewSymbol("x"), bea::Convert::ToJS(v.x)); 58 | obj->Set(v8::String::NewSymbol("y"), bea::Convert::ToJS(v.y)); 59 | return scope.Close(obj); 60 | } 61 | 62 | }; 63 | 64 | template<> struct Convert { 65 | static bool Is(v8::Handle v) { 66 | return !v.IsEmpty() && v->IsObject(); 67 | } 68 | 69 | static cv::Point2f FromJS(v8::Handle v, int nArg) { 70 | const char* msg = "Object with the following properties expected: x, y. This will be cast to 'cv::Point2f'"; 71 | if (!Is(v)) BEATHROW(); 72 | v8::HandleScope scope; 73 | v8::Local obj = v->ToObject(); 74 | cv::Point2f ret; 75 | ret.x = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("x")), nArg); 76 | ret.y = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("y")), nArg); 77 | return ret; 78 | } 79 | 80 | static v8::Handle ToJS(cv::Point2f const& v) { 81 | v8::HandleScope scope; 82 | v8::Local obj = v8::Object::New(); 83 | obj->Set(v8::String::NewSymbol("x"), bea::Convert::ToJS(v.x)); 84 | obj->Set(v8::String::NewSymbol("y"), bea::Convert::ToJS(v.y)); 85 | return scope.Close(obj); 86 | } 87 | 88 | }; 89 | 90 | template<> struct Convert { 91 | static bool Is(v8::Handle v) { 92 | return !v.IsEmpty() && v->IsObject(); 93 | } 94 | 95 | static cv::Size FromJS(v8::Handle v, int nArg) { 96 | const char* msg = "Object with the following properties expected: width, height. This will be cast to 'cv::Size'"; 97 | if (!Is(v)) BEATHROW(); 98 | v8::HandleScope scope; 99 | v8::Local obj = v->ToObject(); 100 | cv::Size ret; 101 | ret.width = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("width")), nArg); 102 | ret.height = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("height")), nArg); 103 | return ret; 104 | } 105 | 106 | static v8::Handle ToJS(cv::Size const& v) { 107 | v8::HandleScope scope; 108 | v8::Local obj = v8::Object::New(); 109 | obj->Set(v8::String::NewSymbol("width"), bea::Convert::ToJS(v.width)); 110 | obj->Set(v8::String::NewSymbol("height"), bea::Convert::ToJS(v.height)); 111 | return scope.Close(obj); 112 | } 113 | 114 | }; 115 | 116 | template<> struct Convert { 117 | static bool Is(v8::Handle v) { 118 | return !v.IsEmpty() && v->IsObject(); 119 | } 120 | 121 | static cv::Size2f FromJS(v8::Handle v, int nArg) { 122 | const char* msg = "Object with the following properties expected: width, height. This will be cast to 'cv::Size2f'"; 123 | if (!Is(v)) BEATHROW(); 124 | v8::HandleScope scope; 125 | v8::Local obj = v->ToObject(); 126 | cv::Size2f ret; 127 | ret.width = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("width")), nArg); 128 | ret.height = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("height")), nArg); 129 | return ret; 130 | } 131 | 132 | static v8::Handle ToJS(cv::Size2f const& v) { 133 | v8::HandleScope scope; 134 | v8::Local obj = v8::Object::New(); 135 | obj->Set(v8::String::NewSymbol("width"), bea::Convert::ToJS(v.width)); 136 | obj->Set(v8::String::NewSymbol("height"), bea::Convert::ToJS(v.height)); 137 | return scope.Close(obj); 138 | } 139 | 140 | }; 141 | 142 | template<> struct Convert { 143 | static bool Is(v8::Handle v) { 144 | return !v.IsEmpty() && v->IsObject(); 145 | } 146 | 147 | static cv::Rect FromJS(v8::Handle v, int nArg) { 148 | const char* msg = "Object with the following properties expected: x, y, width, height. This will be cast to 'cv::Rect'"; 149 | if (!Is(v)) BEATHROW(); 150 | v8::HandleScope scope; 151 | v8::Local obj = v->ToObject(); 152 | cv::Rect ret; 153 | ret.x = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("x")), nArg); 154 | ret.y = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("y")), nArg); 155 | ret.width = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("width")), nArg); 156 | ret.height = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("height")), nArg); 157 | return ret; 158 | } 159 | 160 | static v8::Handle ToJS(cv::Rect const& v) { 161 | v8::HandleScope scope; 162 | v8::Local obj = v8::Object::New(); 163 | obj->Set(v8::String::NewSymbol("x"), bea::Convert::ToJS(v.x)); 164 | obj->Set(v8::String::NewSymbol("y"), bea::Convert::ToJS(v.y)); 165 | obj->Set(v8::String::NewSymbol("width"), bea::Convert::ToJS(v.width)); 166 | obj->Set(v8::String::NewSymbol("height"), bea::Convert::ToJS(v.height)); 167 | return scope.Close(obj); 168 | } 169 | 170 | }; 171 | 172 | template<> struct Convert { 173 | static bool Is(v8::Handle v) { 174 | return !v.IsEmpty() && v->IsObject(); 175 | } 176 | 177 | static cv::Range FromJS(v8::Handle v, int nArg) { 178 | const char* msg = "Object with the following properties expected: start, end. This will be cast to 'cv::Range'"; 179 | if (!Is(v)) BEATHROW(); 180 | v8::HandleScope scope; 181 | v8::Local obj = v->ToObject(); 182 | cv::Range ret; 183 | ret.start = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("start")), nArg); 184 | ret.end = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("end")), nArg); 185 | return ret; 186 | } 187 | 188 | static v8::Handle ToJS(cv::Range const& v) { 189 | v8::HandleScope scope; 190 | v8::Local obj = v8::Object::New(); 191 | obj->Set(v8::String::NewSymbol("start"), bea::Convert::ToJS(v.start)); 192 | obj->Set(v8::String::NewSymbol("end"), bea::Convert::ToJS(v.end)); 193 | return scope.Close(obj); 194 | } 195 | 196 | }; 197 | 198 | template<> struct Convert { 199 | static bool Is(v8::Handle v) { 200 | return !v.IsEmpty() && v->IsObject(); 201 | } 202 | 203 | static cv::RotatedRect FromJS(v8::Handle v, int nArg) { 204 | const char* msg = "Object with the following properties expected: center, size, angle. This will be cast to 'cv::RotatedRect'"; 205 | if (!Is(v)) BEATHROW(); 206 | v8::HandleScope scope; 207 | v8::Local obj = v->ToObject(); 208 | cv::RotatedRect ret; 209 | ret.center = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("center")), nArg); 210 | ret.size = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("size")), nArg); 211 | ret.angle = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("angle")), nArg); 212 | return ret; 213 | } 214 | 215 | static v8::Handle ToJS(cv::RotatedRect const& v) { 216 | v8::HandleScope scope; 217 | v8::Local obj = v8::Object::New(); 218 | obj->Set(v8::String::NewSymbol("center"), bea::Convert::ToJS(v.center)); 219 | obj->Set(v8::String::NewSymbol("size"), bea::Convert::ToJS(v.size)); 220 | obj->Set(v8::String::NewSymbol("angle"), bea::Convert::ToJS(v.angle)); 221 | return scope.Close(obj); 222 | } 223 | 224 | }; 225 | 226 | template<> struct Convert { 227 | static bool Is(v8::Handle v) { 228 | return !v.IsEmpty() && v->IsObject(); 229 | } 230 | 231 | static cv::minMaxLocRet FromJS(v8::Handle v, int nArg) { 232 | const char* msg = "Object with the following properties expected: minVal, maxVal, minIdx, maxIdx. This will be cast to 'cv::minMaxLocRet'"; 233 | if (!Is(v)) BEATHROW(); 234 | v8::HandleScope scope; 235 | v8::Local obj = v->ToObject(); 236 | cv::minMaxLocRet ret; 237 | ret.minVal = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("minVal")), nArg); 238 | ret.maxVal = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("maxVal")), nArg); 239 | ret.minIdx = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("minIdx")), nArg); 240 | ret.maxIdx = bea::Convert::FromJS(obj->Get(v8::String::NewSymbol("maxIdx")), nArg); 241 | return ret; 242 | } 243 | 244 | static v8::Handle ToJS(cv::minMaxLocRet const& v) { 245 | v8::HandleScope scope; 246 | v8::Local obj = v8::Object::New(); 247 | obj->Set(v8::String::NewSymbol("minVal"), bea::Convert::ToJS(v.minVal)); 248 | obj->Set(v8::String::NewSymbol("maxVal"), bea::Convert::ToJS(v.maxVal)); 249 | obj->Set(v8::String::NewSymbol("minIdx"), bea::Convert::ToJS(v.minIdx)); 250 | obj->Set(v8::String::NewSymbol("maxIdx"), bea::Convert::ToJS(v.maxIdx)); 251 | return scope.Close(obj); 252 | } 253 | 254 | }; 255 | 256 | template<> struct Convert { 257 | static bool Is(v8::Handle v) { 258 | return bea::ExposedClass::Is(v); 259 | } 260 | 261 | static cv::Mat* FromJS(v8::Handle v, int nArg) { 262 | return bea::ExposedClass::FromJS(v, nArg); 263 | } 264 | 265 | static v8::Handle ToJS(cv::Mat* const& v) { 266 | return bea::ExposedClass::ToJS(v); 267 | } 268 | 269 | }; 270 | 271 | template<> struct Convert { 272 | static bool Is(v8::Handle v) { 273 | return bea::ExposedClass::Is(v); 274 | } 275 | 276 | static cv::VideoCapture* FromJS(v8::Handle v, int nArg) { 277 | return bea::ExposedClass::FromJS(v, nArg); 278 | } 279 | 280 | static v8::Handle ToJS(cv::VideoCapture* const& v) { 281 | return bea::ExposedClass::ToJS(v); 282 | } 283 | 284 | }; 285 | 286 | template<> struct Convert { 287 | static bool Is(v8::Handle v) { 288 | return bea::ExposedClass::Is(v); 289 | } 290 | 291 | static cv::VideoWriter* FromJS(v8::Handle v, int nArg) { 292 | return bea::ExposedClass::FromJS(v, nArg); 293 | } 294 | 295 | static v8::Handle ToJS(cv::VideoWriter* const& v) { 296 | return bea::ExposedClass::ToJS(v); 297 | } 298 | 299 | }; 300 | 301 | template<> struct Convert { 302 | static bool Is(v8::Handle v) { 303 | return bea::ExposedClass::Is(v); 304 | } 305 | 306 | static cv::SparseMat* FromJS(v8::Handle v, int nArg) { 307 | return bea::ExposedClass::FromJS(v, nArg); 308 | } 309 | 310 | static v8::Handle ToJS(cv::SparseMat* const& v) { 311 | return bea::ExposedClass::ToJS(v); 312 | } 313 | 314 | }; 315 | 316 | template<> struct Convert { 317 | static bool Is(v8::Handle v) { 318 | return bea::ExposedClass::Is(v); 319 | } 320 | 321 | static cv::CascadeClassifier* FromJS(v8::Handle v, int nArg) { 322 | return bea::ExposedClass::FromJS(v, nArg); 323 | } 324 | 325 | static v8::Handle ToJS(cv::CascadeClassifier* const& v) { 326 | return bea::ExposedClass::ToJS(v); 327 | } 328 | 329 | }; 330 | 331 | } 332 | 333 | namespace opencvjs { 334 | class JMat { 335 | protected: 336 | //Destructor 337 | static void __destructor(v8::Handle value); 338 | //Exported methods 339 | static v8::Handle __postAllocator(const v8::Arguments& args); 340 | static v8::Handle __constructor(const v8::Arguments& args); 341 | static v8::Handle row(const v8::Arguments& args); 342 | static v8::Handle col(const v8::Arguments& args); 343 | static v8::Handle rowRange(const v8::Arguments& args); 344 | static v8::Handle colRange(const v8::Arguments& args); 345 | static v8::Handle diag(const v8::Arguments& args); 346 | static v8::Handle clone(const v8::Arguments& args); 347 | static v8::Handle copyTo(const v8::Arguments& args); 348 | static v8::Handle convertTo(const v8::Arguments& args); 349 | static v8::Handle assignTo(const v8::Arguments& args); 350 | static v8::Handle setTo(const v8::Arguments& args); 351 | static v8::Handle reshape(const v8::Arguments& args); 352 | static v8::Handle inv(const v8::Arguments& args); 353 | static v8::Handle mul(const v8::Arguments& args); 354 | static v8::Handle t(const v8::Arguments& args); 355 | static v8::Handle cross(const v8::Arguments& args); 356 | static v8::Handle dot(const v8::Arguments& args); 357 | static v8::Handle create(const v8::Arguments& args); 358 | static v8::Handle eye(const v8::Arguments& args); 359 | static v8::Handle ones(const v8::Arguments& args); 360 | static v8::Handle zeros(const v8::Arguments& args); 361 | static v8::Handle resize(const v8::Arguments& args); 362 | static v8::Handle locateROI(const v8::Arguments& args); 363 | static v8::Handle adjustROI(const v8::Arguments& args); 364 | static v8::Handle step1(const v8::Arguments& args); 365 | static v8::Handle indexable(const v8::Arguments& args); 366 | static v8::Handle at(const v8::Arguments& args); 367 | //Accessors - Getters 368 | static v8::Handle accGet_width(v8::Local prop, const v8::AccessorInfo& info); 369 | static v8::Handle accGet_height(v8::Local prop, const v8::AccessorInfo& info); 370 | static v8::Handle accGet_size(v8::Local prop, const v8::AccessorInfo& info); 371 | static v8::Handle accGet_type(v8::Local prop, const v8::AccessorInfo& info); 372 | static v8::Handle accGet_channels(v8::Local prop, const v8::AccessorInfo& info); 373 | static v8::Handle accGet_isContinuous(v8::Local prop, const v8::AccessorInfo& info); 374 | static v8::Handle accGet_elemSize(v8::Local prop, const v8::AccessorInfo& info); 375 | static v8::Handle accGet_empty(v8::Local prop, const v8::AccessorInfo& info); 376 | static v8::Handle accGet_depth(v8::Local prop, const v8::AccessorInfo& info); 377 | static v8::Handle accGet_total(v8::Local prop, const v8::AccessorInfo& info); 378 | static v8::Handle accGet_rows(v8::Local prop, const v8::AccessorInfo& info); 379 | static v8::Handle accGet_cols(v8::Local prop, const v8::AccessorInfo& info); 380 | //Accessors - Setters 381 | static void accSet_width(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 382 | static void accSet_height(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 383 | static void accSet_size(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 384 | static void accSet_type(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 385 | static void accSet_channels(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 386 | static void accSet_isContinuous(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 387 | static void accSet_elemSize(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 388 | static void accSet_empty(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 389 | static void accSet_depth(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 390 | static void accSet_total(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 391 | static void accSet_rows(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 392 | static void accSet_cols(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 393 | public: 394 | static void _InitJSObject(v8::Handle target); 395 | }; 396 | 397 | } 398 | 399 | namespace opencvjs { 400 | class JVideoCapture { 401 | protected: 402 | //Exported methods 403 | static v8::Handle __constructor(const v8::Arguments& args); 404 | static v8::Handle open(const v8::Arguments& args); 405 | static v8::Handle isOpened(const v8::Arguments& args); 406 | static v8::Handle release(const v8::Arguments& args); 407 | static v8::Handle grab(const v8::Arguments& args); 408 | static v8::Handle retrieve(const v8::Arguments& args); 409 | static v8::Handle read(const v8::Arguments& args); 410 | static v8::Handle get(const v8::Arguments& args); 411 | static v8::Handle set(const v8::Arguments& args); 412 | static v8::Handle __postAllocator(const v8::Arguments& args); 413 | public: 414 | static void _InitJSObject(v8::Handle target); 415 | }; 416 | 417 | } 418 | 419 | namespace opencvjs { 420 | class JVideoWriter { 421 | protected: 422 | //Exported methods 423 | static v8::Handle __constructor(const v8::Arguments& args); 424 | static v8::Handle open(const v8::Arguments& args); 425 | static v8::Handle isOpened(const v8::Arguments& args); 426 | static v8::Handle write(const v8::Arguments& args); 427 | static v8::Handle __postAllocator(const v8::Arguments& args); 428 | public: 429 | static void _InitJSObject(v8::Handle target); 430 | }; 431 | 432 | } 433 | 434 | namespace opencvjs { 435 | class JSparseMat { 436 | protected: 437 | //Destructor 438 | static void __destructor(v8::Handle value); 439 | //Exported methods 440 | static v8::Handle __constructor(const v8::Arguments& args); 441 | static v8::Handle clone(const v8::Arguments& args); 442 | static v8::Handle copyTo(const v8::Arguments& args); 443 | static v8::Handle convertTo(const v8::Arguments& args); 444 | static v8::Handle create(const v8::Arguments& args); 445 | static v8::Handle clear(const v8::Arguments& args); 446 | static v8::Handle hash(const v8::Arguments& args); 447 | static v8::Handle size(const v8::Arguments& args); 448 | static v8::Handle __postAllocator(const v8::Arguments& args); 449 | //Accessors - Getters 450 | static v8::Handle accGet_elemSize(v8::Local prop, const v8::AccessorInfo& info); 451 | static v8::Handle accGet_elemSize1(v8::Local prop, const v8::AccessorInfo& info); 452 | static v8::Handle accGet_type(v8::Local prop, const v8::AccessorInfo& info); 453 | static v8::Handle accGet_depth(v8::Local prop, const v8::AccessorInfo& info); 454 | static v8::Handle accGet_channels(v8::Local prop, const v8::AccessorInfo& info); 455 | static v8::Handle accGet_dims(v8::Local prop, const v8::AccessorInfo& info); 456 | static v8::Handle accGet_nzcount(v8::Local prop, const v8::AccessorInfo& info); 457 | //Accessors - Setters 458 | static void accSet_elemSize(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 459 | static void accSet_elemSize1(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 460 | static void accSet_type(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 461 | static void accSet_depth(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 462 | static void accSet_channels(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 463 | static void accSet_dims(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 464 | static void accSet_nzcount(v8::Local prop, v8::Local v, const v8::AccessorInfo& info); 465 | public: 466 | static void _InitJSObject(v8::Handle target); 467 | }; 468 | 469 | } 470 | 471 | namespace opencvjs { 472 | class JOpenCV { 473 | protected: 474 | //Exported methods 475 | static v8::Handle GaussianBlur(const v8::Arguments& args); 476 | static v8::Handle Canny(const v8::Arguments& args); 477 | static v8::Handle bilateralFilter(const v8::Arguments& args); 478 | static v8::Handle cvtColor(const v8::Arguments& args); 479 | static v8::Handle addWeighted(const v8::Arguments& args); 480 | static v8::Handle resize(const v8::Arguments& args); 481 | static v8::Handle threshold(const v8::Arguments& args); 482 | static v8::Handle warpAffine(const v8::Arguments& args); 483 | static v8::Handle getRotationMatrix2D(const v8::Arguments& args); 484 | static v8::Handle namedWindow(const v8::Arguments& args); 485 | static v8::Handle imshow(const v8::Arguments& args); 486 | static v8::Handle dilate(const v8::Arguments& args); 487 | static v8::Handle erode(const v8::Arguments& args); 488 | static v8::Handle absdiff(const v8::Arguments& args); 489 | static v8::Handle add(const v8::Arguments& args); 490 | static v8::Handle bitwise_and(const v8::Arguments& args); 491 | static v8::Handle bitwise_not(const v8::Arguments& args); 492 | static v8::Handle bitwise_or(const v8::Arguments& args); 493 | static v8::Handle bitwise_xor(const v8::Arguments& args); 494 | static v8::Handle calcCovarMatrix(const v8::Arguments& args); 495 | static v8::Handle cartToPolar(const v8::Arguments& args); 496 | static v8::Handle compare(const v8::Arguments& args); 497 | static v8::Handle completeSymm(const v8::Arguments& args); 498 | static v8::Handle convertScaleAbs(const v8::Arguments& args); 499 | static v8::Handle countNonZero(const v8::Arguments& args); 500 | static v8::Handle cubeRoot(const v8::Arguments& args); 501 | static v8::Handle dct(const v8::Arguments& args); 502 | static v8::Handle dft(const v8::Arguments& args); 503 | static v8::Handle divide(const v8::Arguments& args); 504 | static v8::Handle determinant(const v8::Arguments& args); 505 | static v8::Handle eigen(const v8::Arguments& args); 506 | static v8::Handle exp(const v8::Arguments& args); 507 | static v8::Handle fastAtan2(const v8::Arguments& args); 508 | static v8::Handle flip(const v8::Arguments& args); 509 | static v8::Handle gemm(const v8::Arguments& args); 510 | static v8::Handle idct(const v8::Arguments& args); 511 | static v8::Handle idft(const v8::Arguments& args); 512 | static v8::Handle inRange(const v8::Arguments& args); 513 | static v8::Handle invert(const v8::Arguments& args); 514 | static v8::Handle log(const v8::Arguments& args); 515 | static v8::Handle LUT(const v8::Arguments& args); 516 | static v8::Handle magnitude(const v8::Arguments& args); 517 | static v8::Handle Mahalanobis(const v8::Arguments& args); 518 | static v8::Handle max(const v8::Arguments& args); 519 | static v8::Handle mean(const v8::Arguments& args); 520 | static v8::Handle meanStdDev(const v8::Arguments& args); 521 | static v8::Handle min(const v8::Arguments& args); 522 | static v8::Handle mulSpectrums(const v8::Arguments& args); 523 | static v8::Handle norm(const v8::Arguments& args); 524 | static v8::Handle multiply(const v8::Arguments& args); 525 | static v8::Handle mulTransposed(const v8::Arguments& args); 526 | static v8::Handle normalize(const v8::Arguments& args); 527 | static v8::Handle perspectiveTransform(const v8::Arguments& args); 528 | static v8::Handle phase(const v8::Arguments& args); 529 | static v8::Handle polarToCart(const v8::Arguments& args); 530 | static v8::Handle pow(const v8::Arguments& args); 531 | static v8::Handle randu(const v8::Arguments& args); 532 | static v8::Handle randn(const v8::Arguments& args); 533 | static v8::Handle reduce(const v8::Arguments& args); 534 | static v8::Handle repeat(const v8::Arguments& args); 535 | static v8::Handle scaleAdd(const v8::Arguments& args); 536 | static v8::Handle setIdentity(const v8::Arguments& args); 537 | static v8::Handle solve(const v8::Arguments& args); 538 | static v8::Handle solveCubic(const v8::Arguments& args); 539 | static v8::Handle solvePoly(const v8::Arguments& args); 540 | static v8::Handle sort(const v8::Arguments& args); 541 | static v8::Handle sortIdx(const v8::Arguments& args); 542 | static v8::Handle sqrt(const v8::Arguments& args); 543 | static v8::Handle subtract(const v8::Arguments& args); 544 | static v8::Handle sum(const v8::Arguments& args); 545 | static v8::Handle trace(const v8::Arguments& args); 546 | static v8::Handle transform(const v8::Arguments& args); 547 | static v8::Handle transpose(const v8::Arguments& args); 548 | static v8::Handle circle(const v8::Arguments& args); 549 | static v8::Handle clipLine(const v8::Arguments& args); 550 | static v8::Handle ellipse(const v8::Arguments& args); 551 | static v8::Handle line(const v8::Arguments& args); 552 | static v8::Handle rectangle(const v8::Arguments& args); 553 | static v8::Handle putText(const v8::Arguments& args); 554 | static v8::Handle equalizeHist(const v8::Arguments& args); 555 | static v8::Handle ellipse2Poly(const v8::Arguments& args); 556 | static v8::Handle checkRange(const v8::Arguments& args); 557 | static v8::Handle merge(const v8::Arguments& args); 558 | static v8::Handle split(const v8::Arguments& args); 559 | static v8::Handle fillConvexPoly(const v8::Arguments& args); 560 | static v8::Handle resizeImage(const v8::Arguments& args); 561 | static v8::Handle imdecode(const v8::Arguments& args); 562 | static v8::Handle imread(const v8::Arguments& args); 563 | static v8::Handle imwrite(const v8::Arguments& args); 564 | static v8::Handle waitKey(const v8::Arguments& args); 565 | static v8::Handle HoughCircles(const v8::Arguments& args); 566 | static v8::Handle HoughLines(const v8::Arguments& args); 567 | static v8::Handle preCornerDetect(const v8::Arguments& args); 568 | static v8::Handle goodFeaturesToTrack(const v8::Arguments& args); 569 | static v8::Handle cornerSubPix(const v8::Arguments& args); 570 | static v8::Handle cornerMinEigenVal(const v8::Arguments& args); 571 | static v8::Handle cornerHarris(const v8::Arguments& args); 572 | static v8::Handle cornerEigenValsAndVecs(const v8::Arguments& args); 573 | static v8::Handle calcHist(const v8::Arguments& args); 574 | static v8::Handle calcBackProject(const v8::Arguments& args); 575 | static v8::Handle minMaxLoc(const v8::Arguments& args); 576 | static v8::Handle cvSmooth(const v8::Arguments& args); 577 | static v8::Handle doTick(const v8::Arguments& args); 578 | static v8::Handle discardMats(const v8::Arguments& args); 579 | static v8::Handle fillPoly(const v8::Arguments& args); 580 | static v8::Handle getTextSize(const v8::Arguments& args); 581 | static v8::Handle polylines(const v8::Arguments& args); 582 | static v8::Handle kmeans(const v8::Arguments& args); 583 | static v8::Handle detectObject(const v8::Arguments& args); 584 | public: 585 | static void _InitJSObject(v8::Handle target); 586 | }; 587 | 588 | } 589 | 590 | namespace opencvjs { 591 | class JCascadeClassifier { 592 | protected: 593 | //Exported methods 594 | static v8::Handle __constructor(const v8::Arguments& args); 595 | static v8::Handle empty(const v8::Arguments& args); 596 | static v8::Handle load(const v8::Arguments& args); 597 | static v8::Handle detectMultiScale(const v8::Arguments& args); 598 | static v8::Handle __postAllocator(const v8::Arguments& args); 599 | public: 600 | static void _InitJSObject(v8::Handle target); 601 | }; 602 | 603 | } 604 | 605 | namespace opencvjs { 606 | static void ExposeConstants(v8::Handle target); 607 | } 608 | 609 | namespace opencvjs { 610 | class Project { 611 | public: 612 | static void expose(v8::Handle target); 613 | }; 614 | 615 | } 616 | 617 | #endif //#ifndef OPENCVJS_H --------------------------------------------------------------------------------