歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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表達式。

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

例一

  1. #include <cv.h>   
  2. #include <stdio.h>   
  3. #pragma comment( lib, "cv.lib" )   
  4. #pragma comment( lib, "cxcore.lib" )   
  5. void ecvOutputMatrix(CvMat *mat)  
  6. {  
  7.     for(int i=0;i<mat->rows;i++)  
  8.     {  
  9.         for(int j=0;j<mat->cols;j++)  
  10.         {  
  11.             int type=cvGetElemType(mat);  
  12.             switch(type)  
  13.             {  
  14.             case CV_8UC1:  
  15.                 printf("%d\t",CV_MAT_ELEM(*mat,uchar,i,j));  
  16.                 break;  
  17.             case CV_8UC2:  
  18.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*2),  
  19.                                     CV_MAT_ELEM(*mat,uchar,i,j*2+1));  
  20.                 break;  
  21.             case CV_8UC3:  
  22.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*3),  
  23.                                     CV_MAT_ELEM(*mat,uchar,i,j*3+1),  
  24.                                     CV_MAT_ELEM(*mat,uchar,i,j*3+2));  
  25.                 break;  
  26.             case CV_8UC4:  
  27.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,uchar,i,j*4),  
  28.                                     CV_MAT_ELEM(*mat,uchar,i,j*4+1),  
  29.                                     CV_MAT_ELEM(*mat,uchar,i,j*4+2),  
  30.                                     CV_MAT_ELEM(*mat,uchar,i,j*4+3));  
  31.                 break;  
  32.   
  33.             case CV_8SC1:  
  34.                 printf("%d\t",CV_MAT_ELEM(*mat,signed char,i,j));  
  35.                 break;  
  36.             case CV_8SC2:  
  37.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*2),  
  38.                                     CV_MAT_ELEM(*mat,signed char,i,j*2+1));  
  39.                 break;  
  40.             case CV_8SC3:  
  41.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*3),  
  42.                                     CV_MAT_ELEM(*mat,signed char,i,j*3+1),  
  43.                                     CV_MAT_ELEM(*mat,signed char,i,j*3+2));  
  44.                 break;  
  45.             case CV_8SC4:  
  46.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,signed char,i,j*4),  
  47.                                     CV_MAT_ELEM(*mat,signed char,i,j*4+1),  
  48.                                     CV_MAT_ELEM(*mat,signed char,i,j*4+2),  
  49.                                     CV_MAT_ELEM(*mat,signed char,i,j*4+3));  
  50.                 break;  
  51.   
  52.             case CV_16UC1:  
  53.                 printf("%d\t",CV_MAT_ELEM(*mat,unsigned short,i,j));  
  54.                 break;  
  55.             case CV_16UC2:  
  56.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*2),  
  57.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*2+1));  
  58.                 break;  
  59.             case CV_16UC3:  
  60.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*3),  
  61.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*3+1),  
  62.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*3+2));  
  63.                 break;  
  64.             case CV_16UC4:  
  65.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,unsigned short,i,j*4),  
  66.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*4+1),  
  67.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*4+2),  
  68.                                     CV_MAT_ELEM(*mat,unsigned short,i,j*4+3));  
  69.                 break;  
  70.   
  71.             case CV_16SC1:  
  72.                 printf("%d\t",CV_MAT_ELEM(*mat,short,i,j));  
  73.                 break;  
  74.             case CV_16SC2:  
  75.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*2),  
  76.                                     CV_MAT_ELEM(*mat,short,i,j*2+1));  
  77.                 break;  
  78.             case CV_16SC3:  
  79.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*3),  
  80.                                     CV_MAT_ELEM(*mat,short,i,j*3+1),  
  81.                                     CV_MAT_ELEM(*mat,short,i,j*3+2));  
  82.                 break;  
  83.             case CV_16SC4:  
  84.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,short,i,j*4),  
  85.                                     CV_MAT_ELEM(*mat,short,i,j*4+1),  
  86.                                     CV_MAT_ELEM(*mat,short,i,j*4+2),  
  87.                                     CV_MAT_ELEM(*mat,short,i,j*4+3));  
  88.                 break;  
  89.   
  90.             case CV_32SC1:  
  91.                 printf("%d\t",CV_MAT_ELEM(*mat,int,i,j));  
  92.                 break;  
  93.             case CV_32SC2:  
  94.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*2),  
  95.                                     CV_MAT_ELEM(*mat,int,i,j*2+1));  
  96.                 break;  
  97.             case CV_32SC3:  
  98.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*3),  
  99.                                     CV_MAT_ELEM(*mat,int,i,j*3+1),  
  100.                                     CV_MAT_ELEM(*mat,int,i,j*3+2));  
  101.                 break;  
  102.             case CV_32SC4:  
  103.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,int,i,j*4),  
  104.                                     CV_MAT_ELEM(*mat,int,i,j*4+1),  
  105.                                     CV_MAT_ELEM(*mat,int,i,j*4+2),  
  106.                                     CV_MAT_ELEM(*mat,int,i,j*4+3));  
  107.                 break;  
  108.   
  109.             case CV_32FC1:  
  110.                 printf("%lf\t",CV_MAT_ELEM(*mat,float,i,j));  
  111.                 break;  
  112.             case CV_32FC2:  
  113.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*2),  
  114.                                     CV_MAT_ELEM(*mat,float,i,j*2+1));  
  115.                 break;  
  116.             case CV_32FC3:  
  117.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*3),  
  118.                                     CV_MAT_ELEM(*mat,float,i,j*3+1),  
  119.                                     CV_MAT_ELEM(*mat,float,i,j*3+2));  
  120.                 break;  
  121.             case CV_32FC4:  
  122.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,float,i,j*4),  
  123.                                     CV_MAT_ELEM(*mat,float,i,j*4+1),  
  124.                                     CV_MAT_ELEM(*mat,float,i,j*4+2),  
  125.                                     CV_MAT_ELEM(*mat,float,i,j*4+3));  
  126.                 break;  
  127.   
  128.             case CV_64FC1:  
  129.                 printf("%lf\t",CV_MAT_ELEM(*mat,double,i,j));  
  130.                 break;  
  131.             case CV_64FC2:  
  132.                 printf("(%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*2),  
  133.                                     CV_MAT_ELEM(*mat,double,i,j*2+1));  
  134.                 break;  
  135.             case CV_64FC3:  
  136.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*3),  
  137.                                     CV_MAT_ELEM(*mat,double,i,j*3+1),  
  138.                                     CV_MAT_ELEM(*mat,double,i,j*3+2));  
  139.                 break;  
  140.             case CV_64FC4:  
  141.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM(*mat,double,i,j*4),  
  142.                                     CV_MAT_ELEM(*mat,double,i,j*4+1),  
  143.                                     CV_MAT_ELEM(*mat,double,i,j*4+2),  
  144.                                     CV_MAT_ELEM(*mat,double,i,j*4+3));  
  145.                 break;  
  146.             }  
  147.         }  
  148.         putchar('\n');  
  149.     }  
  150. }  
  151. int main()  
  152. {  
  153.     int m[]={  
  154.             255, 255, 255,  
  155.             255, 255, 255,  
  156.             128, 200,1  
  157.             };  
  158.   
  159.     CvMat M=cvMat(1,3,CV_32SC3,m);  
  160.     ecvOutputMatrix(&M);  
  161.     cvReleaseData(&M);  
  162.   
  163.     return 0;  
  164. }  
例二
  1. #include <cv.h>   
  2. #include <stdio.h>   
  3. #include <highgui.h>   
  4.   
  5. #pragma comment( lib, "cv.lib" )   
  6. #pragma comment( lib, "cxcore.lib" )   
  7. #pragma comment( lib, "highgui.lib" )   
  8.   
  9. #define CV_MAT_ELEM_2(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[2*col+cha])   
  10. #define CV_MAT_ELEM_3(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[3*col+cha])   
  11. #define CV_MAT_ELEM_4(mat,type,row,col,cha)  (((type*)((mat).data.ptr+row*(mat).step))[4*col+cha])   
  12.   
  13. void ecvOutputMatrix(CvMat *mat)  
  14. {  
  15.     for(int i=0;i<mat->rows;i++)  
  16.     {  
  17.         for(int j=0;j<mat->cols;j++)  
  18.         {  
  19.             int type=cvGetElemType(mat);  
  20.             switch(type)  
  21.             {  
  22.             case CV_8UC1:  
  23.                 printf("%d\t",CV_MAT_ELEM(*mat,uchar,i,j));  
  24.                 break;  
  25.             case CV_8UC2:  
  26.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,uchar,i,j,0),  
  27.                                     CV_MAT_ELEM_2(*mat,uchar,i,j,1));  
  28.                 break;  
  29.             case CV_8UC3:  
  30.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,uchar,i,j,0),  
  31.                                     CV_MAT_ELEM_3(*mat,uchar,i,j,1),  
  32.                                     CV_MAT_ELEM_3(*mat,uchar,i,j,2));  
  33.                 break;  
  34.             case CV_8UC4:  
  35.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,uchar,i,j,0),  
  36.                                     CV_MAT_ELEM_4(*mat,uchar,i,j,1),  
  37.                                     CV_MAT_ELEM_4(*mat,uchar,i,j,2),  
  38.                                     CV_MAT_ELEM_4(*mat,uchar,i,j,3));  
  39.                 break;  
  40.   
  41.             case CV_8SC1:  
  42.                 printf("%d\t",CV_MAT_ELEM(*mat,signed char,i,j));  
  43.                 break;  
  44.             case CV_8SC2:  
  45.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,signed char,i,j,0),  
  46.                                     CV_MAT_ELEM_2(*mat,signed char,i,j,1));  
  47.                 break;  
  48.             case CV_8SC3:  
  49.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,signed char,i,j,0),  
  50.                                     CV_MAT_ELEM_3(*mat,signed char,i,j,1),  
  51.                                     CV_MAT_ELEM_3(*mat,signed char,i,j,2));  
  52.                 break;  
  53.             case CV_8SC4:  
  54.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,signed char,i,j,0),  
  55.                                     CV_MAT_ELEM_4(*mat,signed char,i,j,1),  
  56.                                     CV_MAT_ELEM_4(*mat,signed char,i,j,2),  
  57.                                     CV_MAT_ELEM_4(*mat,signed char,i,j,3));  
  58.                 break;  
  59.   
  60.             case CV_16UC1:  
  61.                 printf("%d\t",CV_MAT_ELEM(*mat,unsigned short,i,j));  
  62.                 break;  
  63.             case CV_16UC2:  
  64.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,unsigned short,i,j,0),  
  65.                                     CV_MAT_ELEM_2(*mat,unsigned short,i,j,1));  
  66.                 break;  
  67.             case CV_16UC3:  
  68.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,unsigned short,i,j,0),  
  69.                                     CV_MAT_ELEM_3(*mat,unsigned short,i,j,1),  
  70.                                     CV_MAT_ELEM_3(*mat,unsigned short,i,j,2));  
  71.                 break;  
  72.             case CV_16UC4:  
  73.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,unsigned short,i,j,0),  
  74.                                     CV_MAT_ELEM_4(*mat,unsigned short,i,j,1),  
  75.                                     CV_MAT_ELEM_4(*mat,unsigned short,i,j,2),  
  76.                                     CV_MAT_ELEM_4(*mat,unsigned short,i,j,3));  
  77.                 break;  
  78.   
  79.             case CV_16SC1:  
  80.                 printf("%d\t",CV_MAT_ELEM(*mat,short,i,j));  
  81.                 break;  
  82.             case CV_16SC2:  
  83.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,short,i,j,0),  
  84.                                     CV_MAT_ELEM_2(*mat,short,i,j,1));  
  85.                 break;  
  86.             case CV_16SC3:  
  87.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,short,i,j,0),  
  88.                                     CV_MAT_ELEM_3(*mat,short,i,j,1),  
  89.                                     CV_MAT_ELEM_3(*mat,short,i,j,2));  
  90.                 break;  
  91.             case CV_16SC4:  
  92.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,short,i,j,0),  
  93.                                     CV_MAT_ELEM_4(*mat,short,i,j,1),  
  94.                                     CV_MAT_ELEM_4(*mat,short,i,j,2),  
  95.                                     CV_MAT_ELEM_4(*mat,short,i,j,3));  
  96.                 break;  
  97.   
  98.             case CV_32SC1:  
  99.                 printf("%d\t",CV_MAT_ELEM(*mat,int,i,j));  
  100.                 break;  
  101.             case CV_32SC2:  
  102.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,int,i,j,0),  
  103.                                     CV_MAT_ELEM_2(*mat,int,i,j,1));  
  104.                 break;  
  105.             case CV_32SC3:  
  106.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,int,i,j,0),  
  107.                                     CV_MAT_ELEM_3(*mat,int,i,j,1),  
  108.                                     CV_MAT_ELEM_3(*mat,int,i,j,2));  
  109.                 break;  
  110.             case CV_32SC4:  
  111.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,int,i,j,0),  
  112.                                     CV_MAT_ELEM_4(*mat,int,i,j,1),  
  113.                                     CV_MAT_ELEM_4(*mat,int,i,j,2),  
  114.                                     CV_MAT_ELEM_4(*mat,int,i,j,3));  
  115.                 break;  
  116.   
  117.             case CV_32FC1:  
  118.                 printf("%lf\t",CV_MAT_ELEM(*mat,float,i,j));  
  119.                 break;  
  120.             case CV_32FC2:  
  121.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,float,i,j,0),  
  122.                                     CV_MAT_ELEM_2(*mat,float,i,j,1));  
  123.                 break;  
  124.             case CV_32FC3:  
  125.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,float,i,j,0),  
  126.                                     CV_MAT_ELEM_3(*mat,float,i,j,1),  
  127.                                     CV_MAT_ELEM_3(*mat,float,i,j,2));  
  128.                 break;  
  129.             case CV_32FC4:  
  130.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,float,i,j,0),  
  131.                                     CV_MAT_ELEM_4(*mat,float,i,j,1),  
  132.                                     CV_MAT_ELEM_4(*mat,float,i,j,2),  
  133.                                     CV_MAT_ELEM_4(*mat,float,i,j,3));  
  134.                 break;  
  135.   
  136.             case CV_64FC1:  
  137.                 printf("%lf\t",CV_MAT_ELEM(*mat,double,i,j));  
  138.                 break;  
  139.             case CV_64FC2:  
  140.                 printf("(%d,%d)\t",CV_MAT_ELEM_2(*mat,double,i,j,0),  
  141.                                     CV_MAT_ELEM_2(*mat,double,i,j,1));  
  142.                 break;  
  143.             case CV_64FC3:  
  144.                 printf("(%d,%d,%d)\t",CV_MAT_ELEM_3(*mat,double,i,j,0),  
  145.                                     CV_MAT_ELEM_3(*mat,double,i,j,1),  
  146.                                     CV_MAT_ELEM_3(*mat,double,i,j,2));  
  147.                 break;  
  148.             case CV_64FC4:  
  149.                 printf("(%d,%d,%d,%d)\t",CV_MAT_ELEM_4(*mat,double,i,j,0),  
  150.                                     CV_MAT_ELEM_4(*mat,double,i,j,1),  
  151.                                     CV_MAT_ELEM_4(*mat,double,i,j,2),  
  152.                                     CV_MAT_ELEM_4(*mat,double,i,j,3));  
  153.                 break;  
  154.             }  
  155.         }  
  156.         putchar('\n');  
  157.     }  
  158. }  
  159. int main()  
  160. {  
  161.     int m[]={  
  162.             255, 255, 255,  
  163.             255, 255, 255,  
  164.             128, 200,1  
  165.             };  
  166.   
  167.     CvMat M=cvMat(1,3,CV_32SC3,m);  
  168.     ecvOutputMatrix(&M);  
  169.     cvReleaseData(&M);  
  170.   
  171.     IplImage *image=cvLoadImage("lena.jpg",-1);  
  172.     CvMat mat;  
  173.     cvGetMat(image,&mat);  
  174.     ecvOutputMatrix(&mat);  
  175.     cvReleaseImage(&image);  
  176.   
  177.     return 0;  
  178. }  
Copyright © Linux教程網 All Rights Reserved