title: OpenCV: OutputArray usage without copy
body: <p>I need to extend kernel from one channel to more channels. For example from</p>
  
  <pre><code>0  1 0
  1 -4 1
  0  1 0
  </code></pre>
  
  <p>to </p>
  
  <pre><code>0 0 0  1  1  1 0 0 0
  1 1 1 -4 -4 -4 1 1 1
  0 0 0  1  1  1 0 0 0
  </code></pre>
  
  <p>following standard three channels <code>cv::Mat</code>.</p>
  
  <p>I have following code:</p>
  
  <pre><code>void createKernel(InputArray _A, InputArray _B, OutputArray _kernel, const int chn)
  {
      Mat A = _A.getMat();
      Mat B = _B.getMat();
      Mat kernel;
      Mat kernelOneChannel = A * B;
      std::vector&lt;Mat&gt; channels;
  
      for (int i = 0; i &lt; chn; i++)
      {
          channels.push_back(kernelOneChannel);
      }
  
      merge(channels, kernel);    
      kernel.copyTo(_kernel);
  }
  </code></pre>
  
  <p>One channel kernel is copied to <code>std:vector</code> as many times based on the <code>chn</code>. After that is one multi channel <code>cv::Mat</code> created.</p>
  
  <p>My question is about the last line <code>kernel.copyTo(_kernel)</code>. In many examples what I have seen, this is the way how to deal with <code>Outputarray</code>. Is this <code>copyTo</code> really needed? It seems to me like waste of memory and time to copy already computed <code>kernel</code> to the <code>_kernel</code>. Is there any solution without this data copy from one structure to another?</p>
  
  <p>My question is strictly related to OpenCV and mentioned structures.</p>
  
  <p>Thanks in advance.</p>