# Opencv代码小结之Mat三维创建和Mat逐点逐行赋值

1、创建零/一矩阵

	Mat mzeros = Mat::zeros(1, ix.rows, CV_32F);
Mat mones = Mat::ones(1, ix.rows, CV_32F);

2、创建空矩阵

	Mat ix,iy;
<span style="white-space:pre">	</span>ix.create(300, 9, CV_32F);
iy.create(300, 9, CV_32F);
<span style="white-space:pre">	</span>Mat tempModel(rows ,cols<span style="font-family: Arial, Helvetica, sans-serif;">, CV_32F);</span>

3、创建三维矩阵

	int sz[] = { 3, 4, T };
Mat rt = Mat(3, sz, CV_32F, Scalar::all(0));

4、Mat访问指定元素

<span style="white-space:pre">	</span>ix.at<float>(n, k);

5、深拷贝

<span style="white-space:pre">	</span>out_ix = ix.col(1).clone();//拷贝第一列，其中out_ix.cols = 1, out_ix.rows = ix.rows

<span style="white-space:pre">	</span>out_ix1 = ix.clone();

<span style="white-space:pre">	</span>ix.copyTo(out_ix2); //把ix的内容拷贝到out_ix2


6、Mat转置

<span style="white-space:pre">	</span>B = A.t();

7、逐行拼接

	Mat tempModel(4, ix.rows, CV_32F);
<span style="white-space:pre">	</span>Mat submat = tempModel.rowRange(0, 1);
mix.copyTo(submat);
submat = tempModel.rowRange(1, 2);
miy.copyTo(submat);
submat = tempModel.rowRange(2, 3);
mzeros.copyTo(submat);
submat = tempModel.rowRange(3, 4);
mones.copyTo(submat);


1、在进行逐行赋值时，运用代码如下：

//1按行 无语法错，但不知道逻辑对不对...
<span style="white-space:pre">	</span>out_iy.row(n2) = iy.row(n).clone(); //不正确
//2逐个
	int k = ix.cols;
	while (k--){//此方法赋值还是不正确！out还是零！
out_ix.at<float>(n2, k) = ix.at<float>(n, k);
}

03-18 1.2万

08-24 8930

04-12 5610

11-13 977

04-11 1875

10-14 7454

03-29 1808

05-14 6222

07-11 4123

09-13 1952