# Revision history [back]

### Best practices for writing image processing functions?

I've been thinking for some time, which is the best method to declare an image processing function?

The function should take an input image and return an output image.

I can think of the following possibilities:

1. using InputArray & OutputArray:

void foo(InputArray in,OutputArray out)
{
Mat inMat=in.getMat();
out.createSameSize(in,CV_8U);
Mat outMat=out.getMat();
// processing code...
}

2. Using two Mat variables as parameters:

void foo(Mat in,Mat out)
{
out.create(in.size(),CV_8U);
// processing code...
}


BTW, in this case which parameters should be declared with &? (i.e. Mat &in)

3. One Mat as parameter and return a new Mat:

Mat foo(Mat in)
{
Mat out(in.size(),CV_8U);
// processing code...
return out;
}


In classical C++ code, the third method should be normally preferred, but I think the other two can be more optimal in certain cases (i.e. using foo(img,img) vs. img=foo(img) or when the out matrix was already created)?

Concerning the first two solutions, I think they are mostly equivalent, however OpenCV uses systematically InputArray and OutputArray as parameters. Is there a good reason for this, or is it's for better readability? I am also aware that InputArray and OutputArray are more general, they can accept other parameters then Mat variables, too.

 2 retagged pklab 4274 ●7 ●30 ●60

### Best practices for writing image processing functions?

I've been thinking for some time, which is the best method to declare an image processing function?

The function should take an input image and return an output image.

I can think of the following possibilities:

1. using InputArray & OutputArray:

void foo(InputArray in,OutputArray out)
{
Mat inMat=in.getMat();
out.createSameSize(in,CV_8U);
Mat outMat=out.getMat();
// processing code...
}

2. Using two Mat variables as parameters:

void foo(Mat in,Mat out)
{
out.create(in.size(),CV_8U);
// processing code...
}


BTW, in this case which parameters should be declared with &? (i.e. Mat &in)

3. One Mat as parameter and return a new Mat:

Mat foo(Mat in)
{
Mat out(in.size(),CV_8U);
// processing code...
return out;
}


In classical C++ code, the third method should be normally preferred, but I think the other two can be more optimal in certain cases (i.e. using foo(img,img) vs. img=foo(img) or when the out matrix was already created)?

Concerning the first two solutions, I think they are mostly equivalent, however OpenCV uses systematically InputArray and OutputArray as parameters. Is there a good reason for this, or is it's for better readability? I am also aware that InputArray and OutputArray are more general, they can accept other parameters then Mat variables, too.

 3 retagged sturkmen 6707 ●3 ●47 ●79 https://github.com/stu...

### Best practices for writing image processing functions?

I've been thinking for some time, which is the best method to declare an image processing function?

The function should take an input image and return an output image.

I can think of the following possibilities:

1. using InputArray & OutputArray:

void foo(InputArray in,OutputArray out)
{
Mat inMat=in.getMat();
out.createSameSize(in,CV_8U);
Mat outMat=out.getMat();
// processing code...
}

2. Using two Mat variables as parameters:

void foo(Mat in,Mat out)
{
out.create(in.size(),CV_8U);
// processing code...
}


BTW, in this case which parameters should be declared with &? (i.e. Mat &in)

3. One Mat as parameter and return a new Mat:

Mat foo(Mat in)
{
Mat out(in.size(),CV_8U);
// processing code...
return out;
}


In classical C++ code, the third method should be normally preferred, but I think the other two can be more optimal in certain cases (i.e. using foo(img,img) vs. img=foo(img) or when the out matrix was already created)?

Concerning the first two solutions, I think they are mostly equivalent, however OpenCV uses systematically InputArray and OutputArray as parameters. Is there a good reason for this, or is it's for better readability? I am also aware that InputArray and OutputArray are more general, they can accept other parameters then Mat variables, too.