Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9287359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:39:01+00:00 2026-06-18T19:39:01+00:00

I want to wrap my C++ OpenCV code with boost::python , and to learn

  • 0

I want to wrap my C++ OpenCV code with boost::python, and to learn how to do it, I tried a toy example, in which

  • I use the Boost.Numpy project to provide me with boost::numpy::ndarray.

  • The C++ function to be wrapped, square() takes a boost::numpy::ndarray and modifies it in place by squaring each element in it.

  • The exported Python module name is called test.

  • The square() C++ function is exported as the square name in the exported module.

  • I am not using bjam because IMO it is too complicated and just doesn’t work for me no matter what. I’m using good old make.

Now, here’s the code:

// test.cpp
#include <boost/python.hpp>
#include <boost/numpy.hpp>
#include <boost/scoped_array.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

namespace py = boost::python;
namespace np = boost::numpy;

void square(np::ndarray& array)
{
    if (array.get_dtype() != np::dtype::get_builtin<int>())
    {
        PyErr_SetString(PyExc_TypeError, "Incorrect array data type.");
        py::throw_error_already_set();
    }
    size_t rows = array.shape(0), cols = array.shape(1);
    size_t stride_row = array.strides(0) / sizeof(int), 
           stride_col = array.strides(1) / sizeof(int);
    cv::Mat mat(rows, cols, CV_32S);
    int *row_iter = reinterpret_cast<int*>(array.get_data());
    for (int i = 0; i < rows; i++, row_iter += stride_row)
    {
        int *col_iter = row_iter;
        int *mat_row = (int*)mat.ptr(i);
        for (int j = 0; j < cols; j++, col_iter += stride_col)
        {
            *(mat_row + j) = (*col_iter) * (*col_iter); 
        }
    }

    for (int i = 0; i < rows; i++, row_iter += stride_row)
    {
        int *col_iter = row_iter;
        int *mat_row = (int*)mat.ptr(i);
        for (int j = 0; j < cols; j++, col_iter += stride_col)
        {
            *col_iter = *(mat_row + j);
        }
    }
}


BOOST_PYTHON_MODULE(test)
{
   using namespace boost::python;
   def("square", square);
}

And here’s the Makefile:

PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

BOOST_INC = /usr/local/include
BOOST_LIB = /usr/local/lib
OPENCV_LIB = $$(pkg-config --libs opencv)
OPENCV_INC = $$(pkg-config --cflags opencv)

TARGET = test

$(TARGET).so: $(TARGET).o
        g++ -shared -Wl,--export-dynamic \
        $(TARGET).o -L$(BOOST_LIB) -lboost_python \
        $(OPENCV_LIB) \
        -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) \
        -o $(TARGET).so

$(TARGET).o: $(TARGET).cpp
        g++ -I$(PYTHON_INCLUDE) $(OPENCV_INC) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

With this scheme, I can type make and test.so gets created. But when I try to import it,

In [1]: import test
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-73ae3ffe1045> in <module>()
----> 1 import test

ImportError: ./test.so: undefined symbol:        _ZN5boost6python9converter21object_manager_traitsINS_5numpy7ndarrayEE10get_pytypeEv

In [2]: 

This is a linker error which I can’t seem to fix. Can anyone please help me with what’s going on? Do you have (links to) code that already does integrate OpenCV, numpy and Boost.Python without things like Py++ or the likes?.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:39:02+00:00Added an answer on June 18, 2026 at 7:39 pm

    Okay I fixed this. It was a simple issue, but a sleepy brain and servings of bjam had made me ignore it. In the Makefile, I’d forgotten to put -lboost_numpy that links the Boost.Numpy libs to my lib. So, the modified Makefile looks like this:

    PYTHON_VERSION = 2.7
    PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
    
    BOOST_INC = /usr/local/include
    BOOST_LIB = /usr/local/lib
    OPENCV_LIB = $$(pkg-config --libs opencv)
    OPENCV_INC = $$(pkg-config --cflags opencv)
    
    TARGET = test
    
    $(TARGET).so: $(TARGET).o
            g++ -shared -Wl,--export-dynamic \
            $(TARGET).o -L$(BOOST_LIB) -lboost_python -lboost_numpy \
            $(OPENCV_LIB) \
            -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) \
            -o $(TARGET).so
    
    $(TARGET).o: $(TARGET).cpp
            g++ -I$(PYTHON_INCLUDE) $(OPENCV_INC) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a project we want to wrap the Boost Asio socket in a way,
I use Leiningen to manage my CLJ project. When I want to wrap a
I want to wrap a C++ vector of vectors to Python code by using
I want to wrap around a list/vector in C++ like in Python. Basically I
Ok, basically there is a large C++ project (Recast) that I want to wrap
Suppose I want to wrap code that can throw exceptions with a try-catch block
I have a table containing data which I do not want to wrap onto
I want to wrap(re-project) one variable in this netcdf file. D:\ gdalwarp -t_srs EPSG:4326
I am a new settle in Obj-C. I want to wrap a OpenCV class
I want to wrap an existing click event in some extra code. Basically I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.