OpenCV中打印CvMat的元素

这里定义一个函数ecvOutputMatrix,用于打印CvMat的元素。下面有两个例子。

例一在Debug时有误,而在Release时没有问题,这由CV_MAT_ELEM的定义可见,其中有assert表达式,使得CV_MAT_ELEM在Debug情况下只能应用于单通道矩阵,实际上在Release情况下CV_MAT_ELEM还是可以应用于多通道矩阵的。

鉴于CV_MAT_ELEM的缺点,在例二中仿照CV_MAT_ELEM定义了三个宏,分别用于访问二三四通道矩阵中的元素。注意所定义的宏并没有保证row,col和cha小于矩阵的行数,列数和通道数,读者可以自行添加相应的assert表达式。

#define CV_MAT_ELEM_2(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[2*col+cha])    #define CV_MAT_ELEM_3(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[3*col+cha])    #define CV_MAT_ELEM_4(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[4*col+cha])  

例一

#include <cv.h>    #include <stdio.h>    #pragma comment( lib, "cv.lib" )    #pragma comment( lib, "cxcore.lib" )    void ecvOutputMatrix(CvMat *mat)   {       for(int i=0;i<mat->rows;i++)       {           for(int j=0;j<mat->cols;j++)           {               int type=cvGetElemType(mat);               switch(type)               {               case CV_8UC1:                   printf("%d\t",CV_MAT_ELEM(*mat,uchar,i,j));                   break;               case CV_8UC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*2),                                       CV_MAT_ELEM(*mat,uchar,i,j*2+1));                   break;               case CV_8UC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*3),                                       CV_MAT_ELEM(*mat,uchar,i,j*3+1),                                       CV_MAT_ELEM(*mat,uchar,i,j*3+2));                   break;               case CV_8UC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*4),                                       CV_MAT_ELEM(*mat,uchar,i,j*4+1),                                       CV_MAT_ELEM(*mat,uchar,i,j*4+2),                                       CV_MAT_ELEM(*mat,uchar,i,j*4+3));                   break;                  case CV_8SC1:                   printf("%d\t",CV_MAT_ELEM(*mat,signed char,i,j));                   break;               case CV_8SC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*2),                                       CV_MAT_ELEM(*mat,signed char,i,j*2+1));                   break;               case CV_8SC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*3),                                       CV_MAT_ELEM(*mat,signed char,i,j*3+1),                                       CV_MAT_ELEM(*mat,signed char,i,j*3+2));                   break;               case CV_8SC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*4),                                       CV_MAT_ELEM(*mat,signed char,i,j*4+1),                                       CV_MAT_ELEM(*mat,signed char,i,j*4+2),                                       CV_MAT_ELEM(*mat,signed char,i,j*4+3));                   break;                  case CV_16UC1:                   printf("%d\t",CV_MAT_ELEM(*mat,unsigned short,i,j));                   break;               case CV_16UC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*2),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*2+1));                   break;               case CV_16UC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*3),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*3+1),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*3+2));                   break;               case CV_16UC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*4),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*4+1),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*4+2),                                       CV_MAT_ELEM(*mat,unsigned short,i,j*4+3));                   break;                  case CV_16SC1:                   printf("%d\t",CV_MAT_ELEM(*mat,short,i,j));                   break;               case CV_16SC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*2),                                       CV_MAT_ELEM(*mat,short,i,j*2+1));                   break;               case CV_16SC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*3),                                       CV_MAT_ELEM(*mat,short,i,j*3+1),                                       CV_MAT_ELEM(*mat,short,i,j*3+2));                   break;               case CV_16SC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*4),                                       CV_MAT_ELEM(*mat,short,i,j*4+1),                                       CV_MAT_ELEM(*mat,short,i,j*4+2),                                       CV_MAT_ELEM(*mat,short,i,j*4+3));                   break;                  case CV_32SC1:                   printf("%d\t",CV_MAT_ELEM(*mat,int,i,j));                   break;               case CV_32SC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*2),                                       CV_MAT_ELEM(*mat,int,i,j*2+1));                   break;               case CV_32SC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*3),                                       CV_MAT_ELEM(*mat,int,i,j*3+1),                                       CV_MAT_ELEM(*mat,int,i,j*3+2));                   break;               case CV_32SC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*4),                                       CV_MAT_ELEM(*mat,int,i,j*4+1),                                       CV_MAT_ELEM(*mat,int,i,j*4+2),                                       CV_MAT_ELEM(*mat,int,i,j*4+3));                   break;                  case CV_32FC1:                   printf("%lf\t",CV_MAT_ELEM(*mat,float,i,j));                   break;               case CV_32FC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*2),                                       CV_MAT_ELEM(*mat,float,i,j*2+1));                   break;               case CV_32FC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*3),                                       CV_MAT_ELEM(*mat,float,i,j*3+1),                                       CV_MAT_ELEM(*mat,float,i,j*3+2));                   break;               case CV_32FC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*4),                                       CV_MAT_ELEM(*mat,float,i,j*4+1),                                       CV_MAT_ELEM(*mat,float,i,j*4+2),                                       CV_MAT_ELEM(*mat,float,i,j*4+3));                   break;                  case CV_64FC1:                   printf("%lf\t",CV_MAT_ELEM(*mat,double,i,j));                   break;               case CV_64FC2:                   printf("(%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*2),                                       CV_MAT_ELEM(*mat,double,i,j*2+1));                   break;               case CV_64FC3:                   printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*3),                                       CV_MAT_ELEM(*mat,double,i,j*3+1),                                       CV_MAT_ELEM(*mat,double,i,j*3+2));                   break;               case CV_64FC4:                   printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*4),                                       CV_MAT_ELEM(*mat,double,i,j*4+1),                                       CV_MAT_ELEM(*mat,double,i,j*4+2),                                       CV_MAT_ELEM(*mat,double,i,j*4+3));                   break;               }           }           putchar('\n');       }   }   int main()   {       int m[]={               255, 255, 255,               255, 255, 255,               128, 200,1               };          CvMat M=cvMat(1,3,CV_32SC3,m);       ecvOutputMatrix(&M);       cvReleaseData(&M);          return 0;   }  

例二

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5700f8768a398fa5a8235a3d51e5d76e.html