4

Many functions in OPENCV are using InputArray and OutputArray as function arguments. For example, the Hough transform function in OPENCV:

void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )

Insider the function, we have to use InputArray function getMat to give it to a true input array type. For example, Mat image= _image.getMat(). Similarly, we have to use copyTo function to transform the true output array to OutputArray format. For example, Mat(lines).copyTo(_lines).

My question is why OPENCV designs its function signature in this way. Take the hough function for example, if we use the following function signature:

void HoughLines(Mat &image, std::vector<Vec2f> &lines, double rho, double theta, int threshold, double srn=0, double stn=0 )

I expect that it would be better as by doing so it will eliminate additional unnecessary copy operations.

feelfree
  • 11,175
  • 20
  • 96
  • 167
  • 1
    Related: http://stackoverflow.com/q/31820088/5008845 – Miki May 18 '16 at 11:24
  • 2
    _I expect that it would be better as by doing so it will eliminate additional unnecessary copy operations._ Actually there are no copies involved. – Miki May 18 '16 at 11:27

1 Answers1

8

From (http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=inputarray#InputArray)

When you see in the reference manual or in OpenCV source code a function that takes InputArray, it means that you can actually pass Mat, Matx, vector etc. (see above the complete list).

The cv Arrays are only proxy classes. You can use cv::Mat variables as Input/Output-Arrays (and you don't have to wrap them yourself).

Lks
  • 126
  • 5