从 C++ OpenCV 3.0 调用 Python

Call Python from C++ OpenCV 3.0

所以我使用 PiCam 获取视频源,但我想我可以尝试将流传输到 C++ 进行处理。

Python代码:

import time
import picamera
import picamera.array
import cv2

with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(2)
    with picamera.array.PiRGBArray(camera) as stream:
        camera.capture(stream, format='bgr')
        # At this point the image is available as stream.array
        image = stream.array

那么,在我的 .cpp 文件中做什么? 我一直在研究 boost::python,但他们的文档很烂..

发送 numpy 数组而不是直接在 Python 代码中转换为 Cv.Mat 然后从 C++ 调用它有什么好处? Like this.

有什么问题吗? 感谢所有帮助!

编辑: 忘记说了,试过this没成功。 现在找到 pyopencv_to() 和 pyopencv_from(),但不确定如何使用?对不起,新来的。 (本来可以链接上面的pyopencv_,但是不允许post超过两个链接。)

我自己用管道解决了。我想我应该分享我的解决方案:

C++ 端:

union pipe
{
    uint8_t image[height] [width] [colors];
    uint8_t data [height * width * colors];
} raw;

int main(){
    // Reads in the raw data
    fread(&raw.image, 1, sizeof(raw.data), stdin);

    // Rebuild raw data to cv::Mat
    image = Mat(height, width, CV_8UC3, *raw.image);
}

Python 方面:(刚刚在上面的代码末尾添加了这个)

sys.stdout.buffer.write(image.tostring())

运行 在终端输入:

python img.py | ./img

对我来说效果很好!