繁体   English   中英

如何在 C 代码中包装 OpenCV C++ 函数

[英]How to wrap OpenCV C++ function in C code

我能够运行我的 C++ 代码来显示图像,但我正在尝试将它与其他一些 C 代码集成。

我正在寻找如何在 OpenCV 中为我的 C++ 代码编写 C 包装器的演练。 将来我将需要能够在我的 C 代码中调用这个 C++ 方法!

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

这是我目前首先使用的示例 OpenCV C++ 代码。

包装 C++ 代码以便它可以被 C 调用可以通过一些 C++ 函数来完成,这些函数将自己呈现为 C 函数。 例如,让我们假设我有一个名为 MyObj 的类......

// MyObj.h
#pragma once
#include <iostream>

class MyObj
{
  int m_thing = 42;
public: 
  MyObj() = default;
  ~MyObj() = default;

  void printThing() const {
    std::cout << "MyObj: " << m_thing << std::endl;
  }
  int getThing() const {
    return m_thing;
  }
  void setThing(int v) {
    m_thing = v;
  }
};

我需要将它包装在一些 C 函数中(用 C 链接声明)。

// MyObjWrapper.h
#pragma once

/* 
 * use C name mangling if compiling as C++ code. 
 * When compiling as C, this is ignored. 
 */
#ifdef __cplusplus
extern "C" {
#endif

struct ObjWrapper;

/* return a newly created object */
struct ObjWrapper* createObj();

/* delete an object */
void deleteObj(struct ObjWrapper*);

/* do something on an object */
void printThing(const struct ObjWrapper*);

/* get value from object */
int getThing(const struct ObjWrapper*);

/* set value on object */
void setThing(struct ObjWrapper*, int);

#ifdef __cplusplus
}
#endif

现在在C++包装文件中,我们可以封装所有的C++,只留下一个C接口。

// MyObjWrapper.cpp
#include "MyObj.h"
#include "MyObjWrapper.h"
#include <cassert>

/* use C name mangling */
#ifdef __cplusplus
extern "C" {
#endif

struct ObjWrapper
{
  MyObj obj;
};

/* return a newly created object */
struct ObjWrapper* createObj()
{
  return new ObjWrapper;
}

/* delete an object */
void deleteObj(struct ObjWrapper* wrapper)
{
  assert(wrapper);
  delete wrapper;
}

/* do something on an object */
void printThing(const struct ObjWrapper* wrapper)
{
  assert(wrapper);
  wrapper->obj.printThing();
}

/* get value from object */
int getThing(const struct ObjWrapper* wrapper)
{
  assert(wrapper);
  return wrapper->obj.getThing();
}

/* set value on object */
void setThing(struct ObjWrapper* wrapper, int thing)
{
  assert(wrapper);
  wrapper->obj.setThing(thing);
}

#ifdef __cplusplus
}
#endif

暂无
暂无

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

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