0
votes

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;

  img_rowrange.start=3;
  img_rowrange.end=6;
  img_colrange.start=2;
  img_colrange.end=5;

  patch_rowrange.start=0;
  patch_rowrange.end=3;
  patch_colrange.start=1;
  patch_colrange.end=4;

  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));

  patch(patch_rowrange,patch_colrange).copyTo(img(img_rowrange,img_colrange),mask(patch_rowrange,patch_colrange));


  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.

1

1 Answers

1
votes

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));