I need to call a python code from a .cpp file and I should be able to pass cv:Mat image from that cpp file to python .
What I have tried:
From python documentation , I have searched how to call .py file from the cpp file.
I am able to do that, also I could pass some variable.
// Below is the working code :-
CPP Code
cv::Mat imgpy = imread("D:\\DefectedImg.bmp");
Py_Initialize();
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue1, *pValue2, *mat;
pName = PyString_FromString("Sample");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyObject_GetAttrString(pDict, "add");
pArgs = PyTuple_New(2);
pValue1 = PyInt_FromLong(2);
pValue2 = PyInt_FromLong(3);
PyTuple_SetItem(pArgs, 0, pValue1);
PyTuple_SetItem(pArgs, 1, pValue2);
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Python file Sample.py
import sys
import cv2
# import numpy as np
# Returns the sum of two numbers.
def add(a, b):
return a+b
This is working on passing two variable to py file and return result.
I need to pass the mat image imgpy from C++ file to puthon file.
Any easy method to do that?