I am learning both C++ and OpenCV in parallel. I am reading the following that is a bit unclear why pass-by-value parameters often makes code optimization easier for the compiler.
As a last note, you might have been surprised by the fact that our image modifying function uses a pass-by-value image parameter. This works because when images are copied, they still share the same image data. So, you do not necessarily have to transmit images by references when you want to modify their content. Incidentally, pass-by-value parameters often make code optimization easier for the compiler.
void salt(Mat image, int n)
{
default_random_engine generator;
uniform_int_distribution<int> randomRow(0, image.rows - 1);
uniform_int_distribution<int> randomCol(0, image.cols - 1);
for (int k = 0; k < n; k++)
{
int i = randomCol(generator);
int j = randomRow(generator);
if (image.type() == CV_8UC1)
image.at<uchar>(j, i) = 255;
else if (image.type() == CV_8UC3)
image.at<Vec3b>(j, i) = Vec3b(255, 0, 0);
}
}