将 OpenCV C++ 附加组件添加到 node.js 的最基本示例

Bk *_*zor 5 c++ opencv node.js node.js-nan node.js-napi

所以最近我一直在使用 C++ 进入 OpenCV。我已经建立了一些我想导出到 Nodejs 的库和应用程序,但我终生无法弄清楚。

我试图在下面的这个 repo 中查看他是如何做到的,但是特别是因为这是我的第一个附加组件。 https://github.com/peterbraden/node-opencv/blob/master/binding.gyp

我不介意它与 NAN 或 N-API 一起使用,我只是希望有一些简单易懂的东西,可以看到什么发生在何处以及为什么发生。

这是一个简单的 OpenCV 函数,它只打开一个图像,我试图将其用作 Node 的插件:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;

int ShowImage()
{
  String imageName("./image.png");
  Mat image;
  image = imread(imageName, IMREAD_COLOR);
  namedWindow("Display window", WINDOW_AUTOSIZE);
  imshow("Display window", image);
  waitKey(0);
}
Run Code Online (Sandbox Code Playgroud)

N1m*_*r0d 6

您将需要三个主要文件。

  1. 绑定.gyp
  2. 模块.cpp
  3. 索引.js

绑定.gyp

对我来说最困难的部分是弄清楚如何将 openCV 包含到项目中。我不知道这是否正确,但我将 binding.gyp 文件视为典型 C++ 项目中的 make 文件。考虑到这一点,这就是我的 binding.gyp 文件的样子。

{
    "targets": [{
        "target_name": "module",
    'include_dirs': [
        '.',
        '/user/local/lib',
    ],
    'cflags': [
        '-std=c++11',
    ],
    'link_settings': {
        'libraries': [
            '-L/user/local/lib', 
             '-lopencv_core', 
             '-lopencv_imgproc', 
             '-lopencv_highgui'
        ],
    },
    "sources": [ "./src/module.cpp",
                "./src/ImageProcessing.cpp" ]
    }]
}
Run Code Online (Sandbox Code Playgroud)

我编写的 ImageProcessing.cpp 文件需要 c++11,所以这就是为什么我添加了这个标志,不需要让 openCV 工作。

binding.gyp 文件的关键是链接设置。这就是您将 openCV 实际包含到您的项目中的方式。还要确保在源列表中包含所有源文件(我最初忘记包含我的 ImageProcessing.cpp 文件)

模块.cpp

我使用了 n-api 所以我的 module.cpp 文件看起来像这样

#include <node_api.h>
#include "ImageProcessing.hpp"
#include "opencv.hpp"

template <typename T>
ostream& operator<<(ostream& output, std::vector<T> const& values)
{
    for (auto const& value : values)
    {
        output << value;
    }
    return output;
}

napi_value processImages(napi_env env, napi_callback_info info)
{
    napi_status status;
    size_t argc = 3;
    napi_value argv[1];
    status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);

    char PathName[100];
    size_t result;
    status = napi_get_value_string_utf8(env, argv[0], PathName, 100, &result);

    char FileName1[100];
    status = napi_get_value_string_utf8(env, argv[1], FileName1, 100, &result);

    char FileName2[100];
    status = napi_get_value_string_utf8(env, argv[2], FileName2, 100, &result);

    vector< vector<Point> > Anchors;        //to store coordinates of all anchor points
    vector< vector<Point> > Regions[4];     //to store coordinates of all corners of all pages
    vector<int> Parameters;                 // image processing parameters
    vector<string> FileList1;
    vector<string> FileList2;
    Mat TemplateROI[NUM_SHEET][4];
    Mat Result1, Result2;

    string FileName;
    string testName = FileName1;


    int i;

    // The first function to be called only at startup of the program
    // provide the path to folder where the data and reference image files are saved
    getAnchorRegionRoI(PathName, &Anchors, Regions, &Parameters, TemplateROI);

    vector< vector<int> > Answers;


    if (Parameters.at(0)) {
            namedWindow("Display1", CV_WINDOW_AUTOSIZE);
            namedWindow("Display2", CV_WINDOW_AUTOSIZE);
    }



    napi_value outer;
    status = napi_create_array(env, &outer);
    //This will need to be changed to watch for new files and then process them
    Answers = scanBothSides(FileName1, FileName2, "./Output/", &Result1, &Result2, &Anchors, Regions, Parameters, TemplateROI);

    for(int k = 0; k<Answers.size(); k++){
      napi_value inner;
      status = napi_create_array(env, &inner);
      int j;
      for(j = 0; j<Answers[k].size(); j++){
        napi_value test;
        napi_create_int32(env, Answers[k][j], &test);
        napi_set_element(env,inner, j, test);
      }
      napi_value index;
      napi_create_int32(env, k, &index);
      napi_set_element(env,inner, j, index);

      napi_set_element(env,outer, k, inner);
    }


    if (Parameters.at(0)) {
        if (!Result1.empty() && !Result1.empty()) {
            FileName = "./Output/" + string("O ") + FileList1[i];
            imwrite(FileName, Result1);
            FileName = "./Output/" + string("O ") + FileList2[i];
            imwrite(FileName, Result2);
            resize(Result1, Result1, Size(772, 1000));
            resize(Result2, Result2, Size(772, 1000));
            imshow("Display1", Result1);
            imshow("Display2", Result2);
            waitKey(0);
        }
    }

    if (status != napi_ok)
    {
      napi_throw_error(env, NULL, "Failed to parse arguments");
    }

    //return PathName;
    return outer;
}

napi_value Init(napi_env env, napi_value exports)
{
  napi_status status;
  napi_value fn;

  status = napi_create_function(env, NULL, 0, processImages, NULL, &fn);
  if (status != napi_ok)
  {
    napi_throw_error(env, NULL, "Unable to wrap native function");
  }

  status = napi_set_named_property(env, exports, "processImages", fn);
  if (status != napi_ok)
  {
    napi_throw_error(env, NULL, "Unable to populate exports");
  }

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
Run Code Online (Sandbox Code Playgroud)

这是与 C/C++ 和 node.js 接口的文件。

我在找到 opencv.hpp 文件时遇到了麻烦,所以我现在只是将它移到我的工作目录中。这就是为什么我使用引号而不是括号来包含它。

使用 n-api 需要一点时间来适应,因此请确保您阅读此处的文档

索引.js

最后这是我的 index.js 文件

const express = require('express');
const app = express();
const addon = require('./build/Release/module');
const value = "./Data/";

let FileName1 = "./Images/Back1.jpg";
let FileName2 = "./Images/Front1.jpg";
let result = addon.processImages(value, FileName1, FileName2);
console.log("Results: "+result);
server.listen(3000, () => console.log('Example app listening on port 3000!'))
Run Code Online (Sandbox Code Playgroud)

所以你所要做的就是从 build/Release 文件夹中获取你的模块,然后像任何其他 js 函数一样调用它。

再次查看 module.cpp 代码,您将看到在 init 函数中您使用了 n-api 创建了一个新函数。我打电话给我的 processImages。此名称与 module.cpp 文件顶部的 processImages 函数的名称相匹配。最后在我的 index.js 文件中调用 addon.processImages()。

提示:

我通过运行全局安装了 node-gyp npm install -g node-gyp

我使用以下命令编译了我的代码: node-gyp configure build

首先尝试让一个简单的 n-api 项目工作,然后添加 openCV。我使用教程开始