如何将 OpenCV 图像数据从 Python 转换为 C++? [英] How to convert OpenCV image data from Python to C++?

查看:185
本文介绍了如何将 OpenCV 图像数据从 Python 转换为 C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 OpenCV 3 图像从 Python 3 传递到 C++?在技​​术方面,我如何将 NumPy 数组转换为 <代码>cv::Mat 对象然后返回?

How can I pass an OpenCV 3 image from Python 3 to C++? In technical terms, how do I convert a NumPy array to a cv::Mat object and back?

为了让我的问题更具体,让我们假设:

To make my question more specific, let's assume:

  1. 我有一个 Python 程序,它使用 OpenCV 从网络摄像头捕获图像并显示它们.(在现实生活中,我正在那里设计更多功能的原型,例如一些网络服务器功能.)

  1. I have a Python program that uses OpenCV to capture images from the webcam and display them. (In real life, I'm prototyping further functions there, such as some web server features.)

cam.py:

import cv2

def main():
    cam = cv2.VideoCapture(0)
    while True:
        _, img = cam.read()
        cv2.imshow('cam', img)

        # TODO: Call the C++ function from the sample below 
        # img = helloWorld(img) 

        cv2.waitKey(16) # let imshow do its work (16 ms -> 60 FPS)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

运行 python3 cam.py 试试看.

我想使用某个处理 OpenCV 图像的 C++ 库中现有的 helloWorld 函数.(在现实生活中,它会进行一些信号处理并从图像中提取信息.)为了简单起见,下面的示例函数只是水平镜像图像.main 函数用于检查 helloWorld 是否按预期工作.

I would like to use an existing helloWorld function from some C++ library that processes the OpenCV images. (In real life, it does some signal processing and extracts information from the images.) For the sake of simplicity, my sample function below just mirrors the image horizontally. The main function is there to check that helloWorld works as expected.

cam.cpp:

#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

void helloWorld(Mat img) {
    flip(img, img, 1);
}

int main() {
    VideoCapture stream1(0);

    while (true) {
        Mat img;
        stream1.read(img);

        helloWorld(img);

        imshow("cam", img);
        waitKey(16);
    }

    return 0;
}

要编译这段代码,运行g++ -std=c++11 -lopencv_core -lopencv_highgui -lopencv_videoio cam.cpp -o cam,然后执行./cam试试看.

To compile this code, run g++ -std=c++11 -lopencv_core -lopencv_highgui -lopencv_videoio cam.cpp -o cam, then execute ./cam to try it out.

现在我应该对代码进行哪些更改,以便可以从 Python 应用程序中调用 helloWorld C++ 函数?幸运的是,我可以访问 C++ 源代码,以便在必要时扩展和重新编译它.

Now what changes should I make to my code so that I can call the helloWorld C++ function out of my Python app? Luckily, I have access to the C++ source code, so that I can extend and recompile it if necessary.

推荐答案

参见 Artanis 的要点 Python C 扩展 Hello世界开始创建一个模块,允许您从 Python 中调用 C 代码.

See Artanis's gist Python C Extension Hello World for getting started with the creation of a module allowing you to call C code out of Python.

为了在 Python 3 NumPy 数组和 C++ Mat 之间进行转换,至少有两个地方可以找到示例 C++ 代码:

For converting between the Python 3 NumPy array and the C++ Mat, there are at least 2 places where you can find sample C++ code:

  1. 文件cv2.cpp 包含未在其 API 中公开的函数 一个>:

  • bool pyopencv_to(PyObject* o, UMat& um, const char* name)

PyObject* pyopencv_from(const Mat& m).

pyboostcvconverter 提供了类似的独立转换器功能

pyboostcvconverter provides similar standalone converter functions

  • PyObject* fromMatToNDArray(const Mat& m)
  • Mat fromNDArrayToMat(PyObject* o).

在调用其中任何一个之前,确保您调用了 NumPy 函数 import_array()一次以避免在运行时出现 Segmentation Fault: 11.

Before calling any of these, make sure you call the NumPy function import_array() once to avoid a Segmentation Fault: 11 at runtime.

我已将所有内容放在一个 代码示例中在 GitHub 上.Python 从相机中抓取图像,将其传递给 C++ 模块,然后 C++ 将图像镜像并传回给 Python,最后 Python 将图像显示出来.

I've put it all together in a code sample on GitHub. Python grabs an image from the camera, passes it to the C++ module, then C++ mirrors the image and passes it back to Python, and finally Python displays the image.

这篇关于如何将 OpenCV 图像数据从 Python 转换为 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆