title: OpenCV How to deal with Mat as pointer to speed up the code?
body: <p>I am trying to use pointer with cv::Mat, but I don't quite understand it.</p>
  
  <p>When I try this:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace cv;
  using namespace std;
  
  int main()
  {
      Mat src = imread("image.png");
      Mat img;
      Mat temp;
  
      img = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
      temp = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
  
      temp = img(Range(10, 20), Range(40, 60));
      temp.setTo(255);
  
      imshow("img", img);
      waitKey();
      return 0;
  }
  </code></pre>
  
  <p>It works and there is no problem. However, when I change it to:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace cv;
  using namespace std;
  
  int main()
  {
      Mat src = imread("image.png");
      Mat* img;
      Mat* temp;
  
      *img = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
      *temp = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
  
      temp = img(Range(10, 20), Range(40, 60));
      temp.setTo(255);
  
      imshow("img", *img);
      waitKey();
      return 0;
  }
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>expression preceding parentheses of apparent call must have
    (pointer-to-) function type</p>
  </blockquote>
  
  <p>at</p>
  
  <pre class="lang-c prettyprint-override"><code>temp = img(Range(10, 20), Range(40, 60));
  </code></pre>
  
  <p>and the error:</p>
  
  <blockquote>
    <p>expression must have class type</p>
  </blockquote>
  
  <p>at</p>
  
  <pre class="lang-c prettyprint-override"><code>temp.setTo(255);
  </code></pre>
  
  <p>What is the general rule in dealing with <strong>Mat</strong>s as pointers to speed up the code?</p>
  
  <p>I know for example, in function arguments we use <code>&amp;</code> for input Mats and <code>*</code> for output Mats. But is there a general rule how to define and use Mats inside the functions?</p>
  
  <p>Please tell me if there are other things wrong with this code, since I am a beginner. Thank you!</p>