首先感谢网络的大佬分享的代码,让我也攻克了这一个问题,现在也把我的经验分享出来
C++代码在下面
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Python.h"
//#include "numpy/arrayobject.h"
#include <opencv2/opencv.hpp>
#include <QDebug>
#include <old_defines.h>
#include <noprefix.h>
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
using namespace std;
PyObject *m_PyDict, *m_PyFooBar; // for the references to all the functions
PyObject* m_PyModule; // for the reference to the Pyhton module
Py_Initialize();
PyRun_SimpleString("import sys");// PySys_SetArgv(argc, (wchar_t**)argv);
PyRun_SimpleString("sys.path.append('./')");
PyObject *sys_path = PySys_GetObject("path");
PyList_Append(sys_path, PyUnicode_FromString("."));//没懂
import_array1(); // this macro is defined be NumPy and must be included
m_PyModule = PyImport_ImportModule("cal_Back");
if (m_PyModule != NULL)
{
m_PyDict = PyModule_GetDict(m_PyModule); // get dictionary of available items in the module
m_PyFooBar = PyDict_GetItemString(m_PyDict, "foo_bar"); // grab the functions we are interested in
if (m_PyFooBar != NULL) // execute the function
{
//主要代码功能区域
cv::Mat img = cv::imread("C:/PythonDir/1.png");
cv::Mat img1 = cv::imread("C:/PythonDir/2.png");
img.convertTo(img, CV_8U);
int r = img.rows;
int c = img.cols;
int chnl = img.channels();
int nElem = r * c * chnl;// // total number of elements (here it's an RGB image of size 640x480)
uchar* m = new uchar[nElem];// // create an array of apropriate datatype
std::memcpy(m, img.data, nElem * sizeof(uchar));// // copy the data from the cv::Mat object into the array
npy_intp mdim[] = { r, c, chnl}; // the dimensions of the matrix
PyObject *PyArray = PyArray_SimpleNewFromData(3, mdim, NPY_UINT8, (void*) m); //在这里没有通过
r = img1.rows;
c = img1.cols;
chnl = img1.channels();
nElem = r * c * chnl;
uchar* m1 = new uchar[nElem];
std::memcpy(m1, img1.data, nElem * sizeof(uchar));
npy_intp mdim1[] = { r, c, chnl};
PyObject *PyArray1 = PyArray_SimpleNewFromData(3, mdim1, NPY_UINT8, (void*) m1);//生成包含这个多维数组的PyObject对象,使用PyArray_SimpleNewFromData函数,第一个参数3表示维度,第二个为维度数组Dims,第三个参数指出数组的类型,第四个参数为数组
PyObject *ArgArray = PyTuple_New(2);
PyTuple_SetItem(ArgArray, 0, PyArray); //同样定义大小与Python函数参数个数一致的PyTuple对象
PyTuple_SetItem(ArgArray, 1, PyArray1); //同样定义大小与Python函数参数个数一致的PyTuple对象
PyObject_CallObject(m_PyFooBar, ArgArray);//调用函数,传入Numpy Array 对象。
//主要代码功能区域
}
}
else
{
qDebug() << "Failed to load the Python module!" ;
PyErr_Print();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
python代码在下面
```python
import cv2
import numpy as np
def foo_bar(img, img1):
# if img is not None:
print("this is python world! shit changed ?")
# if img is not None:
# img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(img.shape)
print("hello python !!!!!")
print(img.dtype)
print("end image dtype")
print(img[0])
# print(img[1])
# cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
print(img.dtype)
# img = cv2.convertScaleAbs(img)
# img = img.astype(np.uint8)
print(img.dtype)
print("qiqiguaiguai")
cv2.namedWindow('image in python', 0)
# cv2.resizeWindow('image in python', 500, 500)
print(img.shape)
cv2.imshow("image in python", img)
cv2.imshow("image in python1", img1)
# cv2.imshow("image in python1", img1)
# cv2.imwrite("/home/tt/test/111.png", img)
cv2.waitKey(0)
if __name__=='__main__':
image = cv2.imread("C:/PythonDir/1.png")
image1 = cv2.imread("C:/PythonDir/2.png")
print("ddddddd")
foo_bar(image, image1)
遇到的问题:
1、网络上看到//#include “numpy/arrayobject.h”,其实根本没有找到,可以看我另一个博客
2、 import_array1();这个一共有3个,在QT里面点击F2找到合适自己的哪一个
3、PyArray_SimpleNewFromData(3, mdim1, NPY_UINT8, (void*) m1);这里一定要注意是什么意思,理解了就明白了,这里蒋图片处理成3个维度,8位(!!!!!!一定注意,不能是32或者64或者16,如果是,就自己再弄一下)