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 7641573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:57:31+00:00 2026-05-31T08:57:31+00:00

I’m working on a Computer Vision system with OpenCV in C++. I wrote a

  • 0

I’m working on a Computer Vision system with OpenCV in C++. I wrote a small GUI for it by using Boost::Python and PyQT4. Since I don’t want to introduce QT to the C++ project, I need a way to expose Mat::data (an unsigned char * member) to Python in order to create a QImage there.

First I tried it like this:

    class_<cv::Mat>("Mat", init<>())
          .add_property("data_", make_getter(&Mat::data))

but then I got this in Python: “TypeError: No to_python (by-value) converter found for C++ type: unsigned char*”

I couldn’t write a converter for it because a PyBuf of course needs to know its size.

So my next approach was trying to create a proxy object like this:

struct uchar_array {
  uchar *data;
  size_t size;
  bool copied;
  static const bool debug = true;

  // copy from byte array
  uchar_array(uchar *ptr, size_t size, bool copy) {
      this->size = size;
      this->copied = copy;
      if(copied) {
          data = new uchar[size];
          memcpy(data, ptr, size);
      } else {
          data = ptr;
      }
      if(debug) LOG_ERR("init %d bytes in @%p, mem @%p", size, this, data);
  }

  PyObject *py_ptr() {
      if(debug) LOG_ERR("py_ptr");
      return boost::python::incref(PyBuffer_FromMemory(data, size));
  }

  ~uchar_array() {
      if(debug)  LOG_ERR("~uchar_array @%p", this);
      if(copied) {
          if(debug) LOG_ERR("free @%p, mem @%p", this, data);
          delete [] data;
      }
  }
};

And exposing this via a non-member method:

uchar_array *getMatData(Mat &mat) {
  size_t size = mat.rows * mat.cols * mat.elemSize();
  uchar_array *arr = new uchar_array(mat.data, size, true);
  return arr;
}


class_<cv::Mat>("Mat", init<>())
        .def("data", getMatData, with_custodian_and_ward_postcall<1, 0, return_value_policy<manage_new_object> >())
class_<uchar_array, shared_ptr<uchar_array> >("uchar_array", no_init)
        .def("ptr", &uchar_array::py_ptr);

This works and gets me the buffer into Python, but there are two problems with this approach:

  1. I now have to use mat.data().ptr(), it would be nicer to just do mat.data
  2. When doing mat.data().ptr(), it seems the temporary uchar_array gets destructed immediately after calling ptr(), thus freeing the memory while I still want to use it

I did several experiments with custodian_and_ward and other stuff but got to a point where I stopped to understand this.

So, could anyone please tell me: What’s the preferred way to export an unsigned char * to a PyBuf? In two variants, if possible: allocated for Python so should be freed by Python or as internal pointer where C++ frees it.

  • 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-05-31T08:57:33+00:00Added an answer on May 31, 2026 at 8:57 am

    char* buffers are not really python friendly. On my project (which is not performance sensitive) I would use a std::vector or std::string, depending on what it was intended to contain. Both of these are nicely python friendly.

    If you are not able to alter the underlying data structure, you can use add_property and a couple of getter and setter functions to convert data to a more convenient structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.