c++ - 我可以用 C 包装 OpenCV 的 C++ 接口(interface),然后用 Lisp 的 CFFI 包装它吗?

标签 c++ c opencv common-lisp cffi

我还想知道是否有可能将 OpenCV 的 C++ 接口(interface)用 C 包装,然后将其包装在 Lisp 中,这样我就可以将所有 C++ 功能也添加到我的 cl-opencv 包装器中,因为我想让它完整... . 还想知道我是否这样做,我可以在 lisp 中使用 C++ 包装器和 C 包装器 ....如果你能给我一个快速的示例程序,比如打开的窗口和显示图片功能,只有在c 和 c++ 一起......就像使用 cv::namedWindow 而不是 cvNamedWindow 和所有其他部分都是 c ......这是我尝试下面的程序运行时我只使用 cv::namedWindow 但失败了

 shape.cpp:37:32: error: invalid initialization of 
 reference of type ‘cv::InputArray {aka const cv::_InputArray&}’
  from expression of type ‘IplImage* {aka _IplImage*}’In file included from 
 /usr/local/include/opencv/highgui.h:48:0,

                   from shape.cpp:4:
 /usr/local/include/opencv2/highgui/highgui.hpp:78:19: error: 
 in passing argument 2 of ‘void cv::imshow(const string&, cv::InputArray)’
 Compilation exited abnormally with code 1 at Thu Sep 26 21:18:00

当我添加 cv::imshow 时

  #include <cv.h>
  #include <highgui.h>
  using namespace std;


  int main(){
        CvCapture* capture =0;       

        capture = cvCaptureFromCAM(0);
        if(!capture){
  printf("Capture failure\n");
  return -1;
        }

        IplImage* frame=0;


        cv::namedWindow("Video");



   // cout << "colorModel = " << endl << " " << size << endl << endl;


   while(true){

              frame = cvQueryFrame(capture);           
              if(!frame) break;

              frame=cvCloneImage(frame);




          cv::imshow("Video", frame );


              cvReleaseImage(&frame);

              //Wait 50mS
              int c = cvWaitKey(10);
              //If 'ESC' is pressed, break the loop
              if((char)c==27 ) break;      
        }

        cvDestroyAllWindows() ;
        cvReleaseCapture(&capture);     

        return 0;
  }

我想知道它是否可行...就像在我开始之前 100% 确定我至少可以将每个 c++ 函数包装在 c 中并用 lisp 包装它......或者如果你认为 id run在某些地方陷入障碍甚至不可能.....并且将它包裹两次会使它变慢?并确定 c 接口(interface)比 c++ 更好/更差..或者我可以在 c 接口(interface)中完成我在 c++ 中可以完成的所有事情

我问这个是因为在 swig 和 cffi 文档中它说 c++ 支持不完整。

哦,是的,我还尝试使用所有这些 header 运行上面的代码

#include <cv.h>
#include <highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

仍然出现上述错误

最佳答案

来自 the OpenCV documentation , InputArray

a class that can be constructed from Mat, Mat_, Matx, std::vector, std::vector > or std::vector. It can also be constructed from a matrix expression.

您正在尝试传递 IplImage,其中需要 InputArray,但这是不允许的。 你可以使用

cvShowImage("Video", frame);

或者将您的 IplImage 转换为 Mat 并将其传递给 imshow():

IplImage* frame;
// write to frame
...
// convert to cv::Mat and show the converted image
cv::Mat mat_frame(frame);
cv::imshow("Video", mat_frame)

更好的办法是完全不使用 IplImage,它是遗留 API 的一部分。垫子是首选。

cv::VideoCapture capture;
capture.open(0);
cv::Mat frame;
cv::namedWindow("Video");

if (capture.isOpened()) {
   while (true) {
       capture >> frame;
       if (!frame.empty()) {
           cv::imshow("Video", frame);

           int c = cv::waitKey(10);
           if ((char) c == 27) {
               break;
           }
       }
    }
}

理论上,您可以为所有内容编写包装器以允许从 Lisp CFFI 调用,但这可能不值得花费时间和精力。我会用 C++ 编写应用程序的 OpenCV 部分,然后使用 C/CFFI 从 Lisp 调用它。

关于c++ - 我可以用 C 包装 OpenCV 的 C++ 接口(interface),然后用 Lisp 的 CFFI 包装它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19042791/

相关文章:

c++ - 我会因单例过度而感到过度杀伤力吗?

c++ - SDL 将光标更改为 Windows 默认值

c++ - 将 floodfill 输出(连接的像素)复制到新的垫子中

c - 如何使用函数返回两个不同的链表?

python - OpenCV GrabCut 在 GC_INIT_WITH_MASK 模式下不更新掩码

C++ OpenCV 3.4/FFMPEG 3.4.1 VideoWriter 和 MP4 输出文件格式

c++ - g++-8 和早期版本之间的奇怪行为

c++ - 使用委托(delegate)构造函数来避免泄漏

c - C中的频率直方图

c - 从文件的前 N ​​个文件中读取数字