Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Afaik this method doesn't exist, but I guess cv::repeat (http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=repeat#repeat) is what you are searching.

I don't know much about matlab, so maybe I got the function wrong, but I tried to kinda re-build the example shown at http://www.mathworks.de/de/help/matlab/ref/meshgrid.html:

#include <opencv2/core/core.hpp>
#include <iostream>

static void meshgrid(const cv::Mat &xgv, const cv::Mat &ygv,
              cv::Mat1i &X, cv::Mat1i &Y)
{
  cv::repeat(xgv.reshape(1,1), ygv.total(), 1, X);
  cv::repeat(ygv.reshape(1,1).t(), 1, xgv.total(), Y);
}

// helper function (maybe that goes somehow easier)
static void meshgridTest(const cv::Range &xgv, const cv::Range &ygv,
                         cv::Mat1i &X, cv::Mat1i &Y)
{
  std::vector<int> t_x, t_y;
  for (int i = xgv.start; i <= xgv.end; i++) t_x.push_back(i);
  for (int i = ygv.start; i <= ygv.end; i++) t_y.push_back(i);
  meshgrid(cv::Mat(t_x), cv::Mat(t_y), X, Y);
}

// Small test-main    
int main(int argc, char** argv) {
    cv::Mat1i X, Y;
    meshgridTest(cv::Range(1,3), cv::Range(10, 14), X, Y); 
    std::cerr << X << std::endl;
    std::cerr << Y << std::endl;
    return 0;
}

Output

[1, 2, 3;
  1, 2, 3;
  1, 2, 3;
  1, 2, 3;
  1, 2, 3]
[10, 10, 10;
  11, 11, 11;
  12, 12, 12;
  13, 13, 13;
  14, 14, 14]