title: Modifying Mat attribute in class with mouse callback
body: <p>Going to explain using bullets to make it easy to read (hopefully):</p>
  
  <ul>
  <li>I'm writing a program which needs to be able to draw on top of images, using the mouse.</li>
  <li>The way I have organized the program is that each image is stored in it's own instance of a class. The instance includes a cv::Mat attribute where the image is saved, and a blank cv::Mat (I refer to as the canvas) where I want whatever is drawn to be saved. The canvas is the same size and type as the image cv::Mat.</li>
  <li>I've written a mouse callback function to draw rectangles, however I get an error (which I believe is to do with getting the stored canvas from the image).</li>
  </ul>
  
  <p>OpenCV Error: Assertion failed (0 &lt;= _dims &amp;&amp; _dims &lt;= CV_MAX_DIM) in setSize, file /tmp/opencv-0tcS7S/opencv-2.4.9/modules/core/src/matrix.cpp, line 89
  libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-0tcS7S/opencv-2.4.9/modules/core/src/matrix.cpp:89: error: (-215) 0 &lt;= _dims &amp;&amp; _dims &lt;= CV_MAX_DIM in function setSize</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void draw(int event, int x, int y, int flags, void* params){
  
  ImgData* input = (ImgData*)params;  //Convert the struct passed in as void to a ImageData struct.
  
  if(event == EVENT_LBUTTONDOWN){
  
      printf("Left mouse button clicked.\n");
      input-&gt;ipt = Point(x,y);                        //Store the initial point, this point is saved in memory.
      printf("Original position = (%i,%i)\n",x,y);
  
  }else if(event == EVENT_LBUTTONUP){
  
      cv::Mat temp_canvas;
      input-&gt;getCanvas().copyTo(temp_canvas);
  
      printf("Left mouse button released.\n");
      input-&gt;fpt = Point(x,y);                        //Final Point.
  
      cv::rectangle(temp_canvas, input-&gt;ipt, input-&gt;fpt, Scalar(200,0,0));
      input-&gt;setCanvas(temp_canvas);
  
  }
  
  }
  </code></pre>
  
  <p>The way I'm trying to do it is to copy the canvas from the object instance, draw the rectangle on this copy, then overwrite the old canvas with this modified canvas.</p>
  
  <p>Any help with this or explanation as to why it is happening will be greatly appreciated. </p>
  
  <p>Thank you</p>