在python的c扩展中使用opencv时出现问题?
我正在尝试编写一个简单的python c扩展,其中包含一些opencv代码。这是我的C++代码:在python的c扩展中使用opencv时出现问题?,python,c++,opencv,python-c-api,Python,C++,Opencv,Python C Api,我正在尝试编写一个简单的python c扩展,其中包含一些opencv代码。这是我的C++代码: #include "Python.h" #include "numpy/arrayobject.h" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> /* .... C matrix utility functions ..................*/ double
#include "Python.h"
#include "numpy/arrayobject.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
/* .... C matrix utility functions ..................*/
double **pymatrix_to_Carrayptrs(PyArrayObject *arrayin);
double **ptrvector(long n);
void free_Carrayptrs(double **v);
int not_doublematrix(PyArrayObject *mat);
static PyObject *simple_cv_ops(PyObject *self, PyObject *args)
{
PyArrayObject *matin, *matout;
double **cin, **cout;
int n,m, dims[2];
/* Parse tuples separately since args will differ between C fcns */
if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &matin)) return NULL;
if (NULL == matin) return NULL;
/* Check that object input is 'double' type and a matrix
Not needed if python wrapper function checks before call to this routine */
if (not_doublematrix(matin)) return NULL;
/* Get the dimensions of the input */
n=dims[0]=matin->dimensions[0];
m=dims[1]=matin->dimensions[1];
/* Make a new double matrix of same dims */
matout=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE);
/* Change contiguous arrays into C ** arrays (Memory is Allocated!) */
cin=pymatrix_to_Carrayptrs(matin);
cout=pymatrix_to_Carrayptrs(matout);
// _______ Program LOGIC HERE ________
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(10, 10), cv::Point(-1, -1));
cv::Mat imgIn(n, m, CV_64F, cin);
cv::Mat imgOut(n, m, CV_64F);
cv::morphologyEx(imgIn, imgOut, cv::MORPH_CLOSE, kernel);
cv::GaussianBlur(imgOut, imgOut, cv::Size(5, 5), 0, 0);
cv::medianBlur(imgOut, imgOut, 5);
cv::threshold(imgOut, imgOut, 127, 255, 0);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout[i][j] = imgOut.at<double>(i,j);
}
}
// ____________________________________
/* Free memory, close file and return */
free_Carrayptrs(cin);
free_Carrayptrs(cout);
return PyArray_Return(matout);
}
/* ==== Set up the methods table ====================== */
static PyMethodDef simple_cv_methods[] = {
{"simple_cv_ops", simple_cv_ops, METH_VARARGS, "Just a simple cv operation"},
{NULL, NULL, 0, NULL} /* Sentinel - marks the end of this structure */
};
static struct PyModuleDef simple_cv_modules = {
PyModuleDef_HEAD_INIT,
"simple_cv",
"Python interface for the simple cv ops C library function",
-1,
simple_cv_methods
};
PyMODINIT_FUNC PyInit_simple_cv(void) {
PyObject *pyob;
pyob = PyModule_Create(&simple_cv_modules);
import_array();
return pyob;
}
/* ==== Create Carray from PyArray ======================
Assumes PyArray is contiguous in memory.
Memory is allocated! */
double **pymatrix_to_Carrayptrs(PyArrayObject *arrayin) {
double **c, *a;
int i,n,m;
n=arrayin->dimensions[0];
m=arrayin->dimensions[1];
c=ptrvector(n);
a=(double *) arrayin->data; /* pointer to arrayin data as double */
for ( i=0; i<n; i++) {
c[i]=a+i*m; }
return c;
}
/* ==== Allocate a double *vector (vec of pointers) ======================
Memory is Allocated! See void free_Carray(double ** ) */
double **ptrvector(long n) {
double **v;
v=(double **)malloc((size_t) (n*sizeof(double)));
if (!v) {
printf("In **ptrvector. Allocation of memory for double array failed.");
exit(0); }
return v;
}
/* ==== Free a double *vector (vec of pointers) ========================== */
void free_Carrayptrs(double **v) {
free((char*) v);
}
/* ==== Check that PyArrayObject is a double (Float) type and a matrix ==============
return 1 if an error and raise exception */
int not_doublematrix(PyArrayObject *mat) {
if (mat->descr->type_num != NPY_DOUBLE || mat->nd != 2) {
PyErr_SetString(PyExc_ValueError,
"In not_doublematrix: array must be of type Float and 2 dimensional (n x m).");
return 1; }
return 0;
}
运行python安装程序时,会出现以下错误:
simple_cv.obj:错误LNK2001:未解析的外部符号“double”
__cdecl cv::阈值(类别cv:_输入数组常量&,类别cv:_输出数组常量&,double,double,int)”
(?threshold@cv@@亚纳波夫_InputArray@1@AEBV_OutputArray@1@NNH@Z)
simple_cv.obj:错误LNK2001:未解析的外部符号“public:
void uu cdecl cv::Mat::deallocate(void)”(?deallocate@Mat@cv@@QEAAXZ)
simple_cv.obj:错误LNK2001:未解析的外部符号“public:
void uu cdecl cv::Mat::create(int,int const*,int)”
(?create@Mat@简历@@QEAAXHPEBHH@Z)simple_cv.obj:错误LNK2001:
未解析的外部符号“类别cv::Mat\uu cdecl
cv::getStructuringElement(int,类cv::Size,类
cv::点()
(?getStructuringElement@cv@@是吗?AVMat@1@HV?$尺寸_@H@1@五分_@H@1@@Z)
simple_cv.obj:错误LNK2001:未解析的外部符号“void”
__cdecl cv::GaussianBlur(类别cv:_输入阵列常数&,类别cv::输出阵列常数&,类别cv::大小,双精度,双精度,整数)”
(?GaussianBlur@cv@@雅克萨韦布_InputArray@1@AEBV_OutputArray@1@V?$Size_@H@1@NNH@Z)
simple_cv.obj:错误LNK2001:未解析的外部符号“void”
__cdecl cv::fastFree(void*)“(?)?fastFree@cv@@YAXPEAX@Z)simple_cv.obj:错误LNK2001:未解析的外部符号“private:void u cdecl”
cv::String::解除分配(无效)“”(?deallocate@String@cv@@AEAAXXZ)
simple_cv.obj:错误LNK2001:未解析的外部符号“private:
char*\uuuCDECL cv::String::allocate(无符号\uuuuInt64)”
(?allocate@String@cv@@AEAAPEAD_K@Z)simple_cv.obj:错误LNK2001:
未解析的外部符号“void\uu cdecl cv::error(int,class
cv::字符串常量&,字符常量*,字符常量*,int)”
(?error@cv@@YAXHAEBVString@1@PEBD1H@Z)simple_cv.obj:错误LNK2001:
未解析的外部符号“void\uu cdecl cv::morphologyEx(类
cv::_输入数组常量&,类cv:_输出数组常量&,int,类
cv::InputArray常量&,类cv::Point,int,int,class
cv::标量常数&)
(?morphologyEx@cv@@雅克萨韦布_InputArray@1@AEBV_OutputArray@1@H0V?$Point_@H@1@HHAEBV?$Scalar_@N@1@@Z)
simple_cv.obj:错误LNK2001:未解析的外部符号“void”
__cdecl cv::medianBlur(类别cv:_输入列常量&,类别cv:_输出列常量&,int)”
(?medianBlur@cv@@雅克萨韦布_InputArray@1@AEBV_OutputArray@1@H@Z)
build\lib.win-amd64-3.6\simple\u cv\u c.cp36-win\u amd64.pyd:致命错误
LNK1120:12未解析的外部错误:命令“C:\Program Files”
(x86)\Microsoft Visual Studio
Studio\2017\Enterprise\VC\Tools\MSVC\14.12.25827\bin\HostX86\x64\link.exe'
失败,退出状态为1120
现在我知道这些都是链接器错误。我在VisualStudio中编码时遇到了这个问题,我通过在VisualStudio项目设置的链接器选项卡中将lib路径添加到其他库目录中来解决这个问题。但在这里我不知道该怎么办
顺便说一下,我正在使用windows 10、Visual Studio 2017和python 3.6
有人能帮我吗?我找到了答案
看起来像是来自distutils的Pythons扩展类。核心模块有两个附加的库输入参数,分别是库目录和库
因此,我必须更改我的setup.py代码,如下所示:
import numpy
from distutils.core import setup, Extension
def main():
setup(name="simple_cv",
version="1.0.0",
description="Python interface for the simple cv C extension library function",
author="My Name",
author_email="my_email@email.com",
ext_modules=[Extension("simple_cv", ["simple_cv.cpp"],
include_dirs=[numpy.get_include(), 'C:/opencv/build/include']),
library_dirs = ['C:\\opencv\\build\\x64\\vc14\\lib'],
libraries = ['opencv_world330']])
if __name__ == "__main__":
main()
import numpy
from distutils.core import setup, Extension
def main():
setup(name="simple_cv",
version="1.0.0",
description="Python interface for the simple cv C extension library function",
author="My Name",
author_email="my_email@email.com",
ext_modules=[Extension("simple_cv", ["simple_cv.cpp"],
include_dirs=[numpy.get_include(), 'C:/opencv/build/include']),
library_dirs = ['C:\\opencv\\build\\x64\\vc14\\lib'],
libraries = ['opencv_world330']])
if __name__ == "__main__":
main()