Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);

	// Set the Python int as the first and second arguments to the method.

	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?
Posted
Comments
Richard MacCutchan 8-May-18 3:26am    
The "mat image" is just data, so you pass it in exactly the same way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900