Take the 2-minute tour ×

Trying to compile the following OpenCV code:

#include <opencv/cv.h>

int main(int argc,char *argv[])

    cv::Range img_rowrange, img_colrange, patch_rowrange, patch_colrange;



  cv::Mat img(10,10,CV_8UC1,cv::Scalar(1.0));
  cv::Mat patch(10,10,CV_8UC1,cv::Scalar(2.0));
  cv::Mat mask(10,10,CV_8UC1,cv::Scalar(3.0));


  return 0;

It compiles and works well in MSVS2010 under Windows, but with g++ and MacOS I get the following error:

gpp_cv_fail.cpp:22: error: no matching function for call to ‘cv::Mat::copyTo(cv::Mat, cv::Mat)’ /usr/local/include/opencv2/core/core.hpp:1641: note: candidates are: void cv::Mat::copyTo(const cv::_OutputArray&) const /usr/local/include/opencv2/core/core.hpp:1643: note: void cv::Mat::copyTo(const cv::_OutputArray&, const cv::_InputArray&) const

Is there any workaround for it? As I understand, it happens due to handling of temporary objects and references in C++, but I cannot find any suitable solution for it (i.e. allocating the patch and mask in heap doesn't solve it)

P.S. What I was going to do is to apply one image over another using a mask, this is just the bit of code showing the problem.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

On the offending line, the statement img(img_rowrange,img_colrange) creates a new cv::Mat. The compiler does not let you do it while passing it as argument because as you are not assigning it to a variable, you will never be able to retrieve the data.

So try to do something like this:

cv::Mat img_dst = img(img_rowrange,img_colrange);
patch(patch_rowrange,patch_colrange).copyTo(img_dst, mask(patch_rowrange,patch_colrange));
share|improve this answer

Your Answer


