opencv - 如何在C++ OpenCV代码中将PyObject变量转换为Mat

标签 opencv mat python-c-api python-embedding pyobject

我在opencv中有一个c++人脸识别代码和一个python代码。在python代码中,我从机器人读取了帧,我想将此帧发送到我的c++代码。
我在C++函数中使用this link tho调用python函数。
我的C++函数是embed.cpp:

#include <Python.h>
#include <stdlib.h>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <fstream>
#include <sstream>
#include </home/nao/Desktop/python/boost_1_61_0/boost/python/extract.hpp>
#include </home/nao/Desktop/python/boost_1_61_0/boost/python/converter/registry.hpp>

namespace py = boost::python;
int main()
{

// Set PYTHONPATH TO working directory
   setenv("PYTHONPATH",".",1);
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString((char*)"alvideo2");
// Load the module object
 pModule = PyImport_Import(pName);
// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, (char*)"showNaoImage");

if (PyCallable_Check(pFunc))
{
 pValue=Py_BuildValue("(z)",(char*)"something");
PyErr_Print();
printf("Let's give this a shot!\n");
presult=PyObject_CallObject(pFunc,pValue);
PyErr_Print();
} else{
PyErr_Print();
}

cv::imshow("Original_image",presult);

cvWaitKey(0);

Py_DECREF(pValue);
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
 Py_Finalize();
 return 0;
}
而我的python代码是alvideo2.py:
# -*- encoding: UTF-8 -*-
# Get an image from NAO. Display it and save it using PIL.

import cv2
import os #new
import sys
import time 
import numpy as np
from PIL import Image
from naoqi import ALProxy
from PIL import ImageFont
from PIL import ImageDraw 

#---------------------------------------------------------------------------
def showNaoImage(text):
  """
  First get an image from Nao, then show it on the screen with PIL.
  """
  IP = "192.168.1.18"  # Replace here with your NaoQi's IP address.
  PORT = 9559

  # Read IP address from first argument if any.
  #if len(sys.argv) > 1:
    #IP = sys.argv[1]

  camProxy = ALProxy("ALVideoDevice", IP, PORT)
  resolution = 2    # VGA
  colorSpace = 11   # RGB

  videoClient = camProxy.subscribe("python_client", resolution, colorSpace, 5)

  #t0 = time.time()

  # Get a camera image.
  # image[6] contains the image data passed as an array of ASCII chars.
  naoImage = camProxy.getImageRemote(videoClient)

  # Time the image transfer.
  #print "acquisition delay ", t1 - t0

  camProxy.unsubscribe(videoClient)


  # Now we work with the image returned and save it as a PNG  using ImageDraw
  # package.

  # Get the image size and pixel array.
  imageWidth = naoImage[0]
  imageHeight = naoImage[1]
  array = naoImage[6]

  # Create a PIL Image from our pixel array.
  im = Image.frombytes("RGB", (imageWidth, imageHeight), array)
  frame=np.array(im)
  #cv2.imshow("Faces found", frame)
  #cv2.waitKey(0) 
  return frame
所以,我的问题是如何在cv::imshow("Original_image",presult);中使用返回的fram? 。否则我如何将pyobject转换为Mat?
非常感谢。

最佳答案

对于 Python 3.x C-API :

在Python端,以frame格式返回bytearray:

return bytearray(frame)

在cpp端通过PyByteArray_AsString函数获取它:
pData = PyObject_CallObject(pFunc, pArgs);
uchar *data = (uchar *)PyByteArray_AsString(pData );
cv::Mat img(rows, cols, CV_8UC3, data)

关于opencv - 如何在C++ OpenCV代码中将PyObject变量转换为Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201533/

相关文章:

c++ - 如何通过另一个具有位置的垫子访问opencv中的矩阵数据(索引)

android - OpenCv4Android : frame masking by an image

c++ - Python 调用 DLL 调用 Python, "WindowsError: exception: access violation reading 0x00000004"

c++ - 使用 Python/C API 在 Python-List 上使用 C++-Iterators?

python - 如何使用 opencv 集中和调整数字大小?

python - Heroku上带有OpenCV的Python应用程序

c++ - 使用 giflib 将 opencv mat 编码为 gif

Python C API 读取列表列表,分配内存和全局变量

opencv - 为 haar 级联生成良好的训练数据

python - 如何使用python连接图像的灰色和RGB channel