在C/C ++和Python之间共享内存 [英] Share memory between C/C++ and Python

查看:1834
本文介绍了在C/C ++和Python之间共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种共享内存的方法来在C/C ++和python之间共享一个openCV图像(C +++中的MAT和python中的numpy)?不需要多平台,我在linux中做,我想过mmap或类似想法之间的共享.

Is there a way to share memory to share an openCV image (MAT in C+++ and numpy in python) image between a C/C++ and python? Multiplataform is not needed, I'm doing it in linux, I've thought share between mmap or similar think.

我有两个正在运行的进程,一个是用C编写的,另一个是python,我需要在它们之间共享一个映像.

I have two running processes one is written in C and the other is python, and I need to share an image between them.

我将通过套接字从c进程调用python,但是我需要通过内存进行发送和映像.

I will call from the c process to python via socket but I need to send and image and via memory.

另一种方法是将其写入内存文件,不确定是否会更耗时.

Another alternative could be write in memory file, not sure if it could be more time consuming.

推荐答案

好的,这并不是真正意义上的内存共享. IPC希望将图像数据从一个过程发送到另一个过程.

OK, this is not exactly a memory sharing in its real sense. What you want is IPC to send image data from one process to another.

我建议您使用Unix命名管道.您将必须以C/C ++的字符串格式获取原始数据,通过管道或Unix套接字将其发送到Python,然后从发送的数据中获取一个numpy数组.也许使用np.fromstring()函数.

I suggestthat you use Unix named pipes. You will have to get the raw data in a string format in C/C++, send it through pipe or Unix socket to Python and there get a numpy array from the sent data. Perhaps using np.fromstring() function.

不用担心速度,管道非常快.本地和Unix套接字也是如此.在获取字符串表示形式并将其返回到矩阵方面,大多数时间都将浪费.

Do not worry about the speed, pipes are pretty fast. Local and Unix sockets as well. Most time will be lost on getting the string representation and turning it back to matrix.

您可能会创建实际的共享内存空间,并从C/C ++中的OpenCV中直接将数据获取到Python中,然后在Python中使用OpenCV来获取numpy数组,但这很复杂.如果您不需要光速,最好的选择就是烟斗.

There is a possibility that you can create real shared memory space and get the data from OpenCV in C/C++ directly into Python, and then use OpenCV in Python to get out numpy array, but it would be complicated. If you don't need speed of light your best bet are named pipes.

这篇关于在C/C ++和Python之间共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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