title: C++ conversion from NumPy array to Mat (OpenCV)
body: <p>I am writing a thin wrapper around ArUco augmented reality library (which is based on OpenCV). An interface I am trying to build is very simple:</p>
  
  <ul>
  <li>Python passes image to C++ code;</li>
  <li>C++ code detects markers and returns their locations and other info to Python as tuple of dicts.</li>
  </ul>
  
  <p>However, I couldn't figure out how to represent an image in Python to pass it to C++. For GUI and camera management I am going to use PyQt, so initially it is going to be QImage, but I can't simply pass it to OpenCV (or I can?). At first, I tried to use nested tuples to represent row, column and color of each pixel, so I ended up with this sample code:</p>
  
  <pre><code>using namespace cv;
  namespace py = boost::python;
  
  void display(py::tuple pix)
  {
      /*
          Receive image from Python and display it.
      */
      Mat img(py::len(pix), py::len(pix[0]), CV_8UC3, Scalar(0, 0, 255));
      for (int y = 0; y &lt; py::len(pix); y++)
          for (int x = 0; x &lt; py::len(pix[y]); x++)
          {
              Vec3b rgb;
              for (int i = 0; i &lt; 3; i++)
                  rgb[i] = py::extract&lt;int&gt;(pix[y][x][i]);
              img.at&lt;Vec3b&gt;(Point(x, y)) = rgb;
          }
      imshow("Image", img);
      waitKey(0);
  }
  
  BOOST_PYTHON_MODULE(aruco)
  {
      py::def("display", display);
  }
  </code></pre>
  
  <p>It turned out to be painfully slow (a few seconds for a single frame), so I went googling and found solution that should be much faster: use NumPy arrays, so the code would look something like that:</p>
  
  <pre><code>void display(py::object array)
  {
      Mat img;
      // ... some magic here to convert NumPy array to Mat ...
      imshow("Image", img);
      waitKey(0);
  }
  </code></pre>
  
  <p>However, I have no idea how to convert NumPy Array (which in C++ level is just a Python Object) to OpenCV Mat. I would appreciate any help here.</p>
  
  <p>Alternatively, maybe NumPy is not really needed, so I could just pass QImage Python object directly to C++ layer? Or maybe there is a different approach to this problem? Any advice is appreciated!</p>