Ask Your Question

2D Array of Mat variables

asked 2017-10-11 10:49:18 -0600

Digital Design gravatar image

Hi there, I am going to define my new class and my new class will have a public variable which is a 2D array of Mat. The main problem is that the dimensions of this array is not known in advanced, e.g. a class may need a 35 array and another class may need a 1012 array of Mat elements. How can I define such class? Any idea? I know that it should be fulfilled by pointers, but I failed to implement it so far |)

edit retag flag offensive close merge delete



language used ?

(and again, unlikely, that you have to (or should even) invent a new class for this.)

berak gravatar imageberak ( 2017-10-11 10:54:42 -0600 )edit

I use C++ with MS visual studio

Digital Design gravatar imageDigital Design ( 2017-10-11 11:00:40 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2017-10-11 11:11:14 -0600

berak gravatar image

updated 2017-10-11 11:30:29 -0600

you should definitely NOT invent a new class, but rather use one of the various ways already possible, to achieve that

  1. just use std::vector<Mat>. (probably the most easy one, (and on top of this list for a reason))
  2. put your 3rd dimension into the "channels", e.g.: cvMat(10,10,CV_32FC(17)); // [10x10x17];
  3. use a 3 (or more) dimensional cv::Mat:

    int sz[] = {17, 10, 10};

    cv::Mat(3, sz,CV_32F); // a 3d volume of 17 10x10 planes

(p.s: the main reason i want to steer you away making a new class is:

reinventing the wheel might feel easy (initially), but matching existing expectations (how it should behave) to your creature is terribly hard!)

edit flag offensive delete link more


Dear Break, Many thanks for your answer. But I don´t know the array dimensions and it may vary widely, e.g. [4][6] to [1200][2500]... What to do?

Digital Design gravatar imageDigital Design ( 2017-10-11 11:23:57 -0600 )edit

methods 1 and 3 can be used with sizes obtained at runtime (you only have to replace numbers like 17 with a variable)

berak gravatar imageberak ( 2017-10-11 11:32:30 -0600 )edit

Question Tools

1 follower


Asked: 2017-10-11 10:49:18 -0600

Seen: 618 times

Last updated: Oct 11 '17