Ask Your Question

faster X/Y matrix creation

asked 2013-07-23 07:43:55 -0600


Is there more efficient way in OpenCV to create a MxN CV_32FC2 matrix than this?

  // create XY 2D array
  // (((0, 0), (1, 0), (2, 0), ...),
  //  ((0, 1), (1, 1), (2, 1), ...),
  // ...)
  cv::Mat xy(img_size, CV_32FC2);
  float *pxy = (float*);
  for (int y = 0; y < img_size.height; y++)
    for (int x = 0; x < img_size.width; x++)
      *pxy++ = x;
      *pxy++ = y;

I need it as a base for cv::perspectiveTransform() (see this post). Any speedup is welcome.


edit retag flag offensive close merge delete



just curious, read the blogpost, so how long does the whole procedure take ? i got the impression, that all of it is pretty much constant, and might need run only once .

berak gravatar imageberak ( 2013-07-23 08:39:23 -0600 )edit

The whole perspective_to_maps() function takes less then 2 seconds on BeagleBone Black for a 1280x720 matrix. The shooting target project is very sensitive to short response times (shooters are very impatient people) and the camera position might be variable in the future. So I'm looking for every bit of time which might be spared.

smallbulb gravatar imagesmallbulb ( 2013-07-24 02:48:13 -0600 )edit

hmm the camera position /direction does not matter, the only input to your function are the persp. matrix and the img size.

berak gravatar imageberak ( 2013-07-24 03:43:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-07-23 08:57:44 -0600

Notas gravatar image

If you use Visual Studio 2010 or higher, you can use the PPL-library. You then have access to parallel loops, in this case you should take a look at parallel_for. That way, you can parallelize the initialization of the matrix. If you don't use VS, there are other libraries around that provide parallelization (Intel TBB etc.)

If you use parallelization, you have to change the way you set the matrix values, since it's random in which order the elements get accessed.

edit flag offensive delete link more


Parallelization will not give any more performance because it will be run on a single core ARM chip.

smallbulb gravatar imagesmallbulb ( 2013-07-24 02:40:16 -0600 )edit

Then how do you expect to make matrix initialization faster? It is a process that has to be done for every element. But judging from the other comments, you don't have to create the matrix every time you call perspective_to_maps. Just create it before you (repeatedly) call the method and pass it as an argument, since that matrix never gets changed.

Notas gravatar imageNotas ( 2013-07-24 04:16:21 -0600 )edit

Perhaps some optimized/vectorized matrix OpenCV operation might get the result faster. First I tried to create vectors X = [(0, 0), (1, 0), ..., (x-1, 0)] and Y = trans([(0, 0), (0, 1), ..., (0, y-1)]), then extend them to the image resolution by border copying and then add those two matrices together which gave me the result but it was much much slower then the raw loop solution.

smallbulb gravatar imagesmallbulb ( 2013-07-24 08:21:36 -0600 )edit

Question Tools


Asked: 2013-07-23 07:43:55 -0600

Seen: 260 times

Last updated: Jul 23 '13