作者:
出处:/** =====================================================================================** Filename: array2.c** Description: Show How to traverse a Array to set and get the elements** Version: 1.0* Created: 01/08/2009 09:19:55 PM* Revision: none* Compiler: gcc** Author: Futuredaemon (BUPT), gnuhpc@gmail.com* Company: BUPT_UNITED** =====================================================================================*/#pragma comment( lib, "cxcore.lib" )#include "cv.h"#includeint main(){CvMat* mat = cvCreateMat(3,3,CV_32FC1);cvZero(mat);//将矩阵置0int i;//获得矩阵元素(0,0)的指针float *p = (float*)cvPtr2D(mat, 0, 0);//为矩阵赋值for( i = 0; i < 9; i++){*p = (float)i;p++;}//打印矩阵的值p = (float*)cvPtr2D(mat, 0, 0);for(i = 0; i < 9; i++){printf("%f/t",*p);p++;if((i+1) % 3 == 0)printf("/n");}return 0;}
一定要注意了,这个例子其实是不对的!因为我们说过,分配矩阵内存空间时,是以四字节为最小单位的,这就很有可能有不到四个字节而取成四个字节的情况,所以,如果用矩阵首地址从头到尾指下去访问数据,就很有可能访问到不是数据的字节上去!这一点请务必牢记!!
综上所述,如果要直接访问矩阵中数据,请记住使用step的方案。作者:
出处: