使用boost.python导入一个带有opencv调用的方法,但由于编译后找不到符号而失败

发布于 2024-08-25 22:15:01 字数 3297 浏览 5 评论 0原文

所以我现在没有代码,因为我不在家...但我在 C++ 中使用了 python 的 boost 库,以允许 python 访问

C++ 源代码中 名为 loadImageIntoMainWindow(string filepath) 的函数方法调用在文件顶部导入的 opencv 方法,我将 opencv 包含在我的 Jamroot 文件中,并且还找到了一种在命令行上手动编译和链接的方法...无论哪种情况,当我运行 python 文件时,它都会抱怨没有找到对 opencv 方法的第一个函数调用的符号...

我一回到家就会更新 C++、命令行编译行、Jamroot,

这里的 python 文件是 Jamroot:

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

using python ;
lib libboost_python : : <name>boost_python-mt-py26 ;
# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : ./ ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.

project
  : requirements
        <search>/usr
        <library>libboost_python
        <include>/usr/include/opencv ;
# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
{
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;
}

# Declare test targets

在我运行 bjam --preserve-test-targets 后

或者

g++ -c -g -Wall -fPIC -pipe -DBOOST_PYTHON_MAX_ARITY=20 -I.  -I/usr/include/opencv/ - /usr/include/python2.6 `pkg-config --libs opencv` uTrackSpheresForPyInterface.cpp
g++ -shared -o uTrackSpheresForPyInterface.so uTrackSpheresForPyInterface.o -L/usr/lib - python2.6 -lboost_python-mt-py26

我得到这个:

nathan@innovation:~/Research RIT/openCv$ python uTrackSpheres.py
Traceback (most recent call last):
  File "uTrackSpheres.py", line 18, in <module>
    import uTrackSpheresForPyInterface
ImportError: /home/nathan/Research RIT/openCv/uTrackSpheresForPyInterface.so: undefined symbol: cvCvtColor
nathan@innovation:~/Research RIT/openCv$ 

在 cpp 文件中我做的比这多一点:

#include <iostream>
using namespace std;
#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#endif

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>


int loadImageIntoMainWindow(string imgPath) {

        if( (imgLoaded = cvLoadImage(imgPath.c_str(),1)) == 0 )
                return 0;
        imgMain = cvCreateImage( cvSize(imgLoaded->width, imgLoaded->height), 8, 1 );
        cvCvtColor( imgLoaded, imgMain, CV_BGR2GRAY );

        cvNamedWindow( charCurrentFilename,CV_WINDOW_AUTOSIZE);
        cvSetMouseCallback(charCurrentFilename, on_mouse_imgMain, 0 );
        cvShowImage(charCurrentFilename, imgMain);

        return 1;
}

BOOST_PYTHON_MODULE(uTrackSpheresForPyInterface)
{
    using namespace boost::python;
    def("loadImageIntoMainWindow", loadImageIntoMainWindow);
}

So I don't have the code right now, as I am not home... but i used the boost library for python in C++ to allow python to access a function called something like loadImageIntoMainWindow(string filepath)

in the C++ source code the method calls opencv methods that are imported at the top of the file, I included opencv in my Jamroot file, and also found a way to compile and link manually on the command line... in either case when I run my python file it complains that the symbols aren't found for the first function call to an opencv method...

I will update as soon as I get home with the C++, the command line compilation lines, the Jamroot, and the python files

here is the Jamroot:

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

using python ;
lib libboost_python : : <name>boost_python-mt-py26 ;
# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : ./ ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.

project
  : requirements
        <search>/usr
        <library>libboost_python
        <include>/usr/include/opencv ;
# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
{
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;
}

# Declare test targets

after I run bjam --preserve-test-targets

or

g++ -c -g -Wall -fPIC -pipe -DBOOST_PYTHON_MAX_ARITY=20 -I.  -I/usr/include/opencv/ - /usr/include/python2.6 `pkg-config --libs opencv` uTrackSpheresForPyInterface.cpp
g++ -shared -o uTrackSpheresForPyInterface.so uTrackSpheresForPyInterface.o -L/usr/lib - python2.6 -lboost_python-mt-py26

I get this:

nathan@innovation:~/Research RIT/openCv$ python uTrackSpheres.py
Traceback (most recent call last):
  File "uTrackSpheres.py", line 18, in <module>
    import uTrackSpheresForPyInterface
ImportError: /home/nathan/Research RIT/openCv/uTrackSpheresForPyInterface.so: undefined symbol: cvCvtColor
nathan@innovation:~/Research RIT/openCv$ 

and in the cpp file I'm doing a litte more than this:

#include <iostream>
using namespace std;
#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#endif

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>


int loadImageIntoMainWindow(string imgPath) {

        if( (imgLoaded = cvLoadImage(imgPath.c_str(),1)) == 0 )
                return 0;
        imgMain = cvCreateImage( cvSize(imgLoaded->width, imgLoaded->height), 8, 1 );
        cvCvtColor( imgLoaded, imgMain, CV_BGR2GRAY );

        cvNamedWindow( charCurrentFilename,CV_WINDOW_AUTOSIZE);
        cvSetMouseCallback(charCurrentFilename, on_mouse_imgMain, 0 );
        cvShowImage(charCurrentFilename, imgMain);

        return 1;
}

BOOST_PYTHON_MODULE(uTrackSpheresForPyInterface)
{
    using namespace boost::python;
    def("loadImageIntoMainWindow", loadImageIntoMainWindow);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-09-01 22:15:01

将行(根据需要编辑 /your/lib/path...): 添加

lib cvlib : : <name>cv <search>/your/lib/path/lib ;
lib cxcorelib : : <name>cxcore <search>/your/lib/path/lib ;

到您的 Jamfile 中,并进行编辑

python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

,使其显示为:

python-extension uTrackSpheresForPyInterface :
    uTrackSpheresForPyInterface.cpp
    cvlib
    cxcorelib ;

Add the lines (editing /your/lib/path as appropriate...):

lib cvlib : : <name>cv <search>/your/lib/path/lib ;
lib cxcorelib : : <name>cxcore <search>/your/lib/path/lib ;

to your Jamfile, and edit

python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

so that it reads:

python-extension uTrackSpheresForPyInterface :
    uTrackSpheresForPyInterface.cpp
    cvlib
    cxcorelib ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文