OPENCV Study Notes (2): The initialization of the mat matrix

Source: Internet
Author: User
Tags manual scalar
OPENCV Study Notes (2): The initialization of the mat matrixCategory: OpenCV c++/vs2008 2012-05-24 18:42 3896 People read review (0) Favorite report float programming C

When studying the initialization of the mat matrix, it was found that it could not be initialized directly using an array, as in the case of the mATX lightweight matrix, but rather by using an internal variable type: scalar. The constructor functions in the reference manual are as follows: (1) Mat::mat () (2) Mat::mat (int rows, int cols, int type) (3) Mat::mat (size size, int type) (4) Mat::mat (i NT rows, int cols, int type, constscalar&s) (5) Mat::mat (size size, int type, constscalar&s) (6) Mat::mat (const mat& m) (7) Mat::mat (int rows, int cols, int type, void* data, size_t Step=auto_step) (8) Mat::m At (size size, int type, void* data, size_t Step=auto_step) (9) Mat::mat (const mat& m, const range& RowRange, const range& colrange) Mat::mat (const mat& m, const rect& ROI) (one) Mat::mat (const cvmat* m, BOOL Copydata=fal SE) mat::mat (const iplimage* IMG, bool copydata=false) template<typename T, int n> explicit Mat::mat (const Vec<t, n>& VEC, bool copydata=true) template<typename T, int m, int n> explicit Mat::mat (const matx& Lt T, M, n>& VEC, bool copydata=true) (Template<typename t> Explicit Mat::mat (const vector<t>& VEC , bool Copydata=false) (+) Mat::mat (const matexpr& expr) (+) Mat::mat (int ndims, const int* sizes, int type) Ma T::mat (int ndims, const int* sizes, int type, constscalar&s) (+) Mat::mat (int ndims, const int* sizes, int type, void* data, const size_t* steps=0) (+) Mat::mat (const mat& m, Const range* Ranges)

It seems like such a matrix makes us feel uncomfortable, but then read the reference manual and you'll find an example:

Mat H (+, cv_64f);
for (int i = 0; i < h.rows; i++)
for (int j = 0; J < H.cols; J + +)
H.at<double> (i,j) =1./(i+j+1);

With this example, we try to initialize the mat type.

First improvement: (directly copied from the above example)

Initializes a matrix with a constant

void Initmat (mat& m,float t)
{
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
M.at<float> (i,j) =t;
}

Main program:

int _tmain (int argc, _tchar* argv[])
{
The initialization in the mat matrix can be done using Mat::at ()
Mat M0 (3,3,cv_32f);
Initmat (m0,3);
}

The second type of improvement:

Initializes a matrix with a one-dimensional array

void Initmat (mat& m,float* num)
{
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
M.at<float> (i,j) =* (NUM+I*M.ROWS+J);
}

Main program:

int _tmain (int argc, _tchar* argv[])
{
The initialization in the mat matrix can be done using Mat::at ()
Float m0[]={,
6,5,4,
7,8,9};

Mat M0 (3,3,cv_32f);
Initmat (M0,M0);
}

The third type of improvement:

You want to use a two-dimensional array to initialize

void Initmat (mat& m,float (*P) [3])
{
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
M.at<float> (I,J) =* (* (p+i) +j);
}

Main program:

int _tmain (int argc, _tchar* argv[])
{
The initialization in the mat matrix can be done using Mat::at ()
Float m[][3]={,
6,5,4,
7,8,9};

Mat M0 (3,3,cv_32f);
Initmat (M0,M);
}

However, note that when passing a two-dimensional array, the limitation of the third method is that it is necessary to know the number of elements in each dimension, and you can actually convert the array name A of a two-dimensional array, such as a[2][3], directly into a float pointer, and use it as an array. That

Main program:

int _tmain (int argc, _tchar* argv[])
{
The initialization in the mat matrix can be done using Mat::at ()
Float m[][3]={,
6,5,4,
7,8,9};

Mat M0 (3,3,cv_32f);
Initmat (M0, (float*) m);
}

Note:

The corresponding relationship between the array and the pointer when the parameter is passed:
Argument parameters
Array of arrays: Char a[8][10] char (*P) [10]
Array of pointers: Char *a[10] Char **p
Array pointer (row pointer): char (*A) [8] char (a) [8]
Pointer to: Char **a char**a

The resulting run screenshot is as follows:

Note: For the parameter passing problem of multidimensional array, see "c Expert programming, Page225" in detail .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.