说明:使用Anaconda可以方便安装Pyhton下的opencv和numpy等各种运行库
编译python使用的pyCharm
Anaconda官网:https://www.anaconda.com/distribution/
安装opencv : 找到Anaconda目录下的Anaconda Prompt 输入命令pyhton,然后输入pip install opencv-python
测试opencv是否安装成功:import cv2
查看numpy版本
import numpy
numpy.version.version
查看numpy路径
numpy.__file__
遇到的问题以及解决办法:
1、pycharm更换使用的python版本
在Existing environment中选择Anaconda中python的路径-------精确到python.exe
2、C++传输图像给Python
C++代码
// 初始化Python
//在使用Python系统前,必须使用Py_Initialize对其
//进行初始化。它会载入Python的内建模块并添加系统路
//径到模块搜索路径中。这个函数没有返回值,检查系统
//是否初始化成功需要使用Py_IsInitialized。
//Python.exe对应的路径
Py_SetPythonHome(L"C:/Users/Administrator/Anaconda3");
Py_Initialize();
init_numpy();
//import_array();
// 检查初始化是否成功
if (!Py_IsInitialized())
{
return -1;
}
PyRun_SimpleString("print ('hello')");
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs;
// 载入名为pytest的脚本
//pName = PyString_FromString("testvideo");
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/home/vetec-p/Pan/project/run-maskrcnn')");
PyRun_SimpleString("sys.path.append('/home/vetec-p/Pan/project/run-maskrcnn/build')");
PyRun_SimpleString("sys.path.append('/home/vetec-p/Pan/Detectron-master')");
pModule = PyImport_ImportModule("mian");
if (!pModule)
{
printf("can't find testvideo.py");
//getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if (!pDict)
{
return -1;
}
pFunc = PyDict_GetItemString(pDict, "run");
if (!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [run]");
getchar();
return -1;
}
for (;;)
{
clock_t start, finish;
double totaltime;
start = clock();
//Mat img = imread("/media/vetec-p/Data/Rubbish/maskrcnn_dataset/0803_mask/train_all/pic/" + to_string(i) + ".png");
//Mat img = imread("2.jpg");
if (m_vecImage.size() > 0)
{
cv::Mat img = m_vecImage[0];
if (img.empty())
return -1;
// imshow("img",img);
// waitKey(0);
clock_t s1;
s1 = clock();
//PyObject *PyList = PyList_New(data_size);//定义一个与数组等长的PyList对象数组
PyObject *ArgList = PyTuple_New(1);
auto sz = img.size();
int x = sz.width;
int y = sz.height;
int z = img.channels();
uchar *CArrays = new uchar[x*y*z];
int iChannels = img.channels();
int iRows = img.rows;
int iCols = img.cols * iChannels;
if (img.isContinuous())
{
iCols *= iRows;
iRows = 1;
}
uchar* p;
int id = -1;
for (int i = 0; i < iRows; i++)
{
// get the pointer to the ith row
p = img.ptr<uchar>(i);
// operates on each pixel
for (int j = 0; j < iCols; j++)
{
CArrays[++id] = p[j];//连续空间
}
}
npy_intp Dims[3] = { y, x, z }; //注意这个维度数据!
PyObject *PyArray = PyArray_SimpleNewFromData(3, Dims, NPY_UBYTE, CArrays);
PyTuple_SetItem(ArgList, 0, PyArray);
clock_t e1 = clock();
cout << "\n赋值为" << (double)(e1 - s1) / CLOCKS_PER_SEC << "秒!" << endl;
//PyTuple_SetItem(ArgList, 0, PyList);//将PyList对象放入PyTuple对象中
PyObject *pReturn = PyObject_CallObject(pFunc, ArgList);
clock_t e2 = clock();
m_vecImage.erase(m_vecImage.begin());
//cout<<"\n detect为"<<(double)(e2-e1)/CLOCKS_PER_SEC<<"秒!"<<endl;
}
}
Py_DECREF(pModule);
// 关闭Python
Py_Finalize();
Python代码
import cv2
def run(imgdata):
print("dedededede")
cv2.imshow("img", imgdata)
cv2.waitKey(1)
环境配置参考别的文章