Home > My Original Post > 2D dynamic array using Vector and Mat in C++/OpenCV

2D dynamic array using Vector and Mat in C++/OpenCV

In my previous post, a dynamic 2D array can be implemented by malloc function. However, it is kind of tricky and takes many lines to complete. Here is an easy method at the expense of speed in C++.

int m, n//rows and cols

//caution!!! space between" > >" in "...vector<int> >"

vector< vector<int> > array(m, vector<int>(n,0));

In OpenCV library, 2D dynamic array could be initialized by cv::Mat


using namespace cv;

Mat array;

int m,n; //rows and cols

Mat::zeros(m, n, CV_8U); //initialize all elements to 0, 8-bit depth, i.e. unsigned char

It is extremely simple and efficient.

In addition, new function is another C++ implementation which actually simplified version of malloc.

int **array=new int*[row];
for(i=0; i<row; i++)
 array[i]=new int[col];
Advertisements
Categories: My Original Post
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: