简体   繁体   English

将Opencv Mat返回到node.js v8

[英]Return Opencv Mat to node.js v8

How can I wrap an Opencv Mat and return it to node.js. 如何包装Opencv Mat并将其返回到node.js。 Thank you. 谢谢。 This is what I have. 这就是我所拥有的。

void testFunc(const Nan::FunctionCallbackInfo<v8::Value>& info){
Mat img = imread("test.jpg");
do stuff...
return image "img"
}


void Init(v8::Local<v8::Object> exports) {  
exports->Set(Nan::New("pow").ToLocalChecked(),
             Nan::New<v8::FunctionTemplate>(testFunc)->GetFunction());
}

NODE_MODULE(pow, Init)

Depends on what you want to do with the Mat on the JavaScript side. 取决于您要在JavaScript端使用Mat做什么。

  • If you just want to pass it around, you can return it as an "internal field" on some object; 如果只想传递它,则可以将其作为“内部字段”返回给某个对象。 other C++ functions called with that object later can then retrieve it from there. 之后,其他使用该对象调用的C ++函数可以从那里检索它。
  • If you want to work with it in JavaScript, you'll have to convert (eg copy pixel-by-pixel) it to some object that JavaScript understands -- a Uint8ClampedArray might serve the purpose, but it depends on your use case. 如果要在JavaScript中使用它,则必须将其转换(例如,逐像素复制)为JavaScript可以理解的某个对象-Uint8ClampedArray可以达到目的,但这取决于您的用例。
  • You can also craft a JS-compatible object that uses indexed interceptors to access a Mat used as its backing store. 您还可以设计一个与JS兼容的对象,该对象使用索引拦截器访问用作其后备存储的Mat Note, however, that crossing the API boundary is relatively slow, so for fast code, it typically makes more sense to pass lots of data at once (like the Array suggestion above), and then avoid API-crossing calls inside hot loops. 但是请注意,越过API边界相对较慢,因此对于快速代码而言,一次传递大量数据(例如上面的Array建议)并避免在热循环内进行API交叉调用通常更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM