0

I am making a change to my OpenCV-python project and I have a function that outputs some data using a CV::OutputArray as follows:

bool myFunction(cv::OutputArray myOutputArray)
{
    //get some data for output..blah blah

    cv::Mat(1, dataLength, CV_MAKETYPE(CV_8U, 1), data).copyTo(myOutputArray);
}

When I view the contents of myOutputArray in my debugger I see that its a one dimensional ndarray as follows: enter image description here

So for me to access each element I need to do e.g:

    first_number = my_data[0][0]
    second_number = my_data[0][1]
    third_number = my_data[0][2]
    and so on and so on

How can I mofidy my code so that its just a plain array thats returned? So I can access the data simply as follows:

    first_number = my_data[0]
    second_number = my_data[1]
    third_number = my_data[3]
Community
  • 1
  • 1
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
  • 1
    how do you write your C++ code so it's accessible from python? you as a user are *not* supposed to use `OutputArray` in your code at all. those things are a consequence of some design decisions within OpenCV. in C++ it is supposed to be a cv::Mat or maybe a std::vector of things. in python, a cv::Mat is mapped to a numpy array. the shape is always (height, width, numchannels). – Christoph Rackwitz Oct 04 '21 at 03:00
  • I am actually modifying OpenCV to pass out some custom data, so I have to use it. – Harry Boy Oct 04 '21 at 03:03
  • 1
    anyway, there's no mechanism within OpenCV and its python bindings that would allow to return python lists, or numpy arrays that have any other shape than 3-dimensional (height, width, channels). just stick with the numpy arrays that are mapped from cv::Mat – Christoph Rackwitz Oct 04 '21 at 03:04

0 Answers0