openCV学习之CvMat矩阵访问

    今天看到openCV CvMat这个章节,作者介绍了三种方法即:简单的方法、麻烦的方法、恰当的方法,开始有点晕晕的,后来看了些博客算是理解到了。首先最容易的是使用CV_MAT_ELEM存取矩阵,或者使用另一个类似的宏CV_MAT_ELEM_PTR,CV_MAT_ELEM_PTR是返回指向这个数据元素的指针,也是二者的主要区别。但是,在计划顺序访问矩阵的数据元素时,这种方法效率最低。

      第二种麻烦的方法是使用:

uchar* cvPtr1D( const CvArr* arr, int idx0, int* type=NULL );
uchar* cvPtr2D( const CvArr* arr, int idx0, int idx1, int* type=NULL );
uchar* cvPtr3D( const CvArr* arr, int idx0, int idx1, int idx2, int* type=NULL );
uchar* cvPtrND( const CvArr* arr, int* idx, int* type=NULL, int create_node=1, unsigned* precalc_hashval=NULL );

      很明显,如果是一维数组(矩阵),那就可以使用cvPtr1D,用参数idx0来指向要获得的第idx0个元素,返回值为指向该元素的指针.如果是二维数组(矩阵),就可以使用cvPtr2D,idx0idx1来指向相应的元素。

      同时openCV提供了CvGet*D家族来访问数据:

     CvScalar cvGet1D( const CvArr* arr, int idx0);
CvScalar cvGe
t2D( const CvArr* arr, int idx0, int idex1);

       ......

      但是CvGet*D家族返回的是CvScalar类型(你必须要知道什么是CvScalar类型),会有很大的空间的浪费。再者,在多通道的矩阵中,通道是连续的,利用指针加偏移量我们可以轻松的访问下一通道或者下一像素的值,所以最好选用cvPtr*D家族。

      第三者,恰当的方法,即定义自己的指针,并为了使数据产生正确的偏移,使用行数据长度step来配合使用。step是矩阵中行的长度,单位是字节。其值等于  :行数据个数 * 数据占用字节数。

void arrayAbs(){
	CvMat *mat = cvCreateMat(3, 5, CV_8UC1);
	CvMat *matone;
	matone = cvCreateMat(3, 5, CV_32FC1);
	float value = 0.0;
	int i = 0, j = 0;
	for (i = 0; i < 3; i++){
		for (j = 0; j < 4; j++){
			value -= 4.0;
			CV_MAT_ELEM(*mat, float, i, j) = value;
			CV_MAT_ELEM(*matone, float, i, j) = i * 4 + j;
		}
	}

	int width = cvGetDimSize(mat, 0);
	int height = cvGetDimSize(mat, 1);
	int temp[2] = { 0 };
	cvGetDims(mat, temp);
	cout << "Width:" << temp[0] << " Height:" << temp[1] << endl;
	cout << "Width:" << width << " Height:" << height << endl;
	cout << "step1:" << mat->step << endl;
	cout << "step2:" << matone->step << endl;
}


代码:

void matQuery(){

	const int row = 3;
	const int col = 4;
	/**Method one:    单通道矩阵   **/
	CvMat* mat = cvCreateMat(row,col,CV_32FC1);
	//set Value
	for (int i = 0; i < row; i++){
		for (int j = 0; j < col; j++){
			*( (float*)CV_MAT_ELEM_PTR(*mat, i, j) ) = i*col + j;
		}
	}
	//some funciton to get Mat information
	int width = cvGetDimSize(mat, 0);
	int height = cvGetDimSize(mat, 1);
	int temp[2] = { 0 };
	cvGetDims(mat, temp);
	cout << "Width:" << temp[0] << " Height:" << temp[1] << endl;
	cout << "Width:" << width << " Height:" << height << endl;
	cout << "Step:" << mat->step << endl;;//step 是矩阵中行的长度,单位为字节. CV_32FC1占四个字节,step=4*col=16


	// Get Value 
	for (int i = 0; i < row; i++){
		for (int j = 0; j < col; j++){
			printf("%.2f  ", CV_MAT_ELEM(*mat, float, i, j) );
		}
		printf("\n");
	}
	//Get Value
	printf("use pointer to go throungh data.\n");
	float* p = (float*) cvPtr2D(mat, 0, 0);//return a pointer
	for (int i = 0; i < row*col; i++){
		//矩阵元素通道为1时。可以使用p++来访问下一个矩阵元素,如果是三通道,则按rgb rgb rgb的形式存储,因此使用指针需要加上相应的通道数
		printf("%.2f ",*p++);
	}
	printf("\n");

	//Get Value 
	printf("use Step and pointer to go throungh data.\n");
	for (int i = 0; i < mat->rows; i++){
		 float* ptr = (  float* )( mat->data.ptr + i * mat->step);
		for (int j = 0; j < mat->cols; j++){
			printf("%.2f  ", *(ptr++));
		}
		printf("\n");
	}
	


}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值