作者:
出处:#include "cv.h"#include "highgui.h"#includevoid PrintMat(CvMat *A);//显示矩阵void GenRandn(CvMat *arr,int seed);//生成正态分布的随机矩阵void GenRand(CvMat *arr,int seed);//生成[0,1]均匀分布的随机矩阵static int cmp_func(const void * _a,const void * _b,void * userdata);//比较函数void Test_Multiply();//测试矩阵乘法void Test_cvGetRawData();//将缓存数据填入CvMat数组中void Test_DCT();//计算DCT变换void Test_Rand();//生成随机数void Test_SeqSort();//二维序列排序int main(void){ Test_Multiply(); Test_cvGetRawData(); Test_DCT(); Test_Rand(); Test_SeqSort(); return 0;}/*矩阵乘法*/void Test_Multiply(){ double a[]={1,2,3,4, 5,6,7,8, 9,10,11,12}; double b[]={1,5,9, 2,6,10, 3,7,11, 4,8,12}; double c[9]; CvMat Ma,Mb,Mc; printf("/n=== Test Multiply ===/n"); cvInitMatHeader(&Ma,3,4,CV_64FC1,a,CV_AUTOSTEP); cvInitMatHeader(&Mb,4,3,CV_64FC1,b,CV_AUTOSTEP); cvInitMatHeader(&Mc,3,3,CV_64FC1,c,CV_AUTOSTEP); cvMatMulAdd(&Ma,&Mb,0,&Mc); PrintMat(&Ma); PrintMat(&Mb); PrintMat(&Mc);}void Test_cvGetRawData(){ float *data; int step; float a[]={ 1,2,3,4, -5,6,7,8, 9,-10,-11,12 }; CvMat array; CvSize size; int x,y; printf("/n ===Test get raw dara! ===/n"); cvInitMatHeader(&array,3,4,CV_32FC1,a,CV_AUTOSTEP); cvGetRawData(&array,(uchar **)&data,&step,&size);//得到矩阵的信息 step/=sizeof(data[0]); printf("/nCvMat = "); PrintMat(&array); printf("/nData = "); for (y=0;y rows;i++) { printf("/n"); switch(CV_MAT_DEPTH(A->type)) { case CV_32F: case CV_64F: for (j=0;j cols;j++) printf("%9.3f",(float)cvGetReal2D(A,i,j)); break; case CV_8U: case CV_16U: for (j=0;j cols;j++) printf("%6d",(int)cvGetReal2D(A,i,j)); break; default: break; } } printf("/n");}void Test_SeqSort(){ CvMemStorage *storage = cvCreateMemStorage(0); CvSeq *seq=cvCreateSeq(CV_32SC2,sizeof(CvSeq),sizeof(CvPoint),storage); int i; printf("/n=== Test Sequence sorting! === "); for (i=0;i<10;i++) { CvPoint pt; pt.x= rand()%1000; // 得到1000以内的随机数 pt.y= rand()%1000; cvSeqPush(seq,&pt);//添加元素到序列末尾 } printf("/nOriginal point set:/n"); for (i=0;i total;i++) { CvPoint *pt =(CvPoint *)cvGetSeqElem(seq,i); printf("(%d,%d)/n",pt->x,pt->y); } cvSeqSort(seq,cmp_func,0); printf("/nAfter sorting: /n"); for (i=0;i total;i++) { CvPoint *pt =(CvPoint *)cvGetSeqElem(seq,i); printf("(%d,%d)/n",pt->x,pt->y); } cvClearSeq(seq); cvReleaseMemStorage(&storage);}static int cmp_func(const void * _a,const void * _b,void * userdata){ CvPoint *a=(CvPoint *)_a; CvPoint *b=(CvPoint *)_b; int y_diff=a->y-b->y; int x_diff=a->x-b->x; return y_diff?y_diff:x_diff;}