Ask Your Question
3

Is there a meshgrid function in OpenCV?

asked 2013-04-16 15:31:22 -0600

de3ug gravatar image

Is there a function in OpenCV that is analogous to the meshgrid function in matlab? If not, what would be the best way of attaining similar functionality?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2013-04-16 17:35:33 -0600

Guanta gravatar image

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]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-04-16 15:31:22 -0600

Seen: 13,623 times

Last updated: Apr 16 '13