【OpenCv】Mat中常用的一些

来源:互联网 发布:华莱士有多高 知乎 编辑:程序博客网 时间:2024/06/07 14:12

//原文:http://blog.csdn.net/moc062066/article/details/6949894

//cv::Mat中获取图像中某一点的值是比较麻烦的,一一来探秘


//预备知识
/*
  /*\typedef
access individual elements using [] operator etc.
Shorter aliases for the most popular specializations of Vec<T,n>

typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;

typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;

typedef Vec<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;    
    
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;

typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;

typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;

----------------------imread,第二个参数------------------------
    enum
    {
        // 8bit, color or not
        IMREAD_UNCHANGED  =-1,
        // 8bit, gray
        IMREAD_GRAYSCALE  =0,
        // ?, color
        IMREAD_COLOR      =1,//默认
        // any depth, ?
        IMREAD_ANYDEPTH   =2,
        // ?, any color
        IMREAD_ANYCOLOR   =4
    };

------------------------Mat 常用信息------------------------------
    //! returns element type, similar to CV_MAT_TYPE(cvmat->type)

    int type() const;    //0,1,2,。。。,6


    //! returns element type, similar to CV_MAT_DEPTH(cvmat->type)

    int depth() const;

    常见的有:

      CV_8U   :
     CV_8S   :
     CV_16U  :
     CV_16S  :
     CV_32S  :
     CV_32F  :
     CV_64F  :

    
    //! returns element type, similar to CV_MAT_CN(cvmat->type)
    int channels() const; //1,2,3
    
    //! the matrix dimensionality, >= 2
    int dims;

    //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
    int rows, cols;
    
    //! pointer to the data
    uchar* data;

------------------------------------
#define CV_8U   0
#define CV_8S   1
#define CV_16U  2
#define CV_16S  3
#define CV_32S  4
#define CV_32F  5
#define CV_64F  6
#define CV_USRTYPE1 7

CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]

[cpp] view plaincopy
  1. string show_Mat_type( Mat mat_input ){  
  2.       
  3.     string mat_type_name = "";  
  4.       
  5.     //detect depth  
  6.     switch ( mat_input.depth() ) {  
  7.     case CV_8U   :  
  8.             mat_type_name = "CV_8U";  
  9.             break;  
  10.   
  11.     case CV_8S   :  
  12.             mat_type_name = "CV_8S";  
  13.             break;  
  14.   
  15.     case CV_16U  :  
  16.             mat_type_name = "CV_16U";  
  17.             break;  
  18.   
  19.     case CV_16S  :  
  20.             mat_type_name = "CV_16S";  
  21.             break;  
  22.   
  23.     case CV_32S  :  
  24.             mat_type_name = "CV_32S";  
  25.             break;  
  26.   
  27.     case CV_32F  :  
  28.             mat_type_name = "CV_32F";  
  29.             break;  
  30.   
  31.     case CV_64F  :  
  32.             mat_type_name = "CV_64F";  
  33.             break;  
  34.               
  35.     default:  
  36.         cout << "switch ( mat_input.depth() ) error!" << endl ;  
  37.     }  
  38.       
  39.     //detect channels  
  40.     switch( mat_input.channels() ) {  
  41.     case 1 :  
  42.         mat_type_name += "C1";  
  43.         break;  
  44.           
  45.     case 2 :  
  46.         mat_type_name += "C2";  
  47.         break;  
  48.           
  49.     case 3 :  
  50.         mat_type_name += "C3";  
  51.         break;  
  52.           
  53.     default:  
  54.         cout << "switch( mat_input.channels() ) error!" << endl ;  
  55.     }  
  56.       
  57.     //uchar char short  
  58.     switch( mat_input.type() % 8 ) {  
  59.     case 0:  
  60.       //cout<<  int( ((uchar*)mat.data + Cols*i+Chns*j )[k] )<<" ";  
  61.       mat_type_name += " uchar";  
  62.       break;  
  63.         
  64.     case 1:  
  65.       //cout<<  int( (  (char*)mat.data + Cols*i+Chns*j)[k] )<<" ";     
  66.         mat_type_name += " char";  
  67.       break;  
  68.         
  69.     case 2:  
  70.       //cout<<  (  (unsigned short*)mat.data + Cols*i+Chns*j)[k] <<" ";  
  71.         mat_type_name += " ushort";  
  72.       break;  
  73.         
  74.     case 3:  
  75.       //cout<<  (  (short*)mat.data + Cols*i+Chns*j)[k] <<" ";     
  76.         mat_type_name += " short";  
  77.       break;  
  78.         
  79.     case 4:  
  80.       //cout<<  (  (int*)mat.data + Cols*i+Chns*j)[k] <<" ";     
  81.         mat_type_name += " int";  
  82.       break;  
  83.         
  84.     case 5:  
  85.       //cout<<  (  (float*)mat.data + Cols*i+Chns*j)[k] <<" ";  
  86.         mat_type_name += " float";  
  87.       break;  
  88.         
  89.     case 6:  
  90.       //cout<<  (  (double*)mat.data + Cols*i+Chns*j)[k] <<" ";  
  91.         mat_type_name += " double";  
  92.       break;  
  93.         
  94.     default:  
  95.        break;  
  96.     }  
  97.     cout << "\n" << mat_type_name << endl ;  
  98.     return mat_type_name;  
  99. }  


/****************************************在另一篇文章里看到的************************************、

说到数据的存储,这一直就是一个值得关注的问题,

Mat_<uchar>对应的是CV_8U

Mat_<char>对应的是CV_8S,

Mat_<int>对应的是CV_32S,

Mat_<float>对应的是CV_32F,

Mat_<double>对应的是CV_64F,

对应的数据深度如下:  

CV_8U - 8-bit unsigned integers ( 0..255 )  

 CV_8S - 8-bit signed integers ( -128..127 )  

 CV_16U - 16-bit unsigned integers ( 0..65535 )  

CV_16S - 16-bit signed integers ( -32768..32767 )  

CV_32S - 32-bit signed integers ( -2147483648..2147483647 ) 

CV_32F - 32-bit ?oating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )  

CV_64F - 64-bit ?oating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )  

这里还需要注意一个问题,很多OpenCV的函数支持的数据深度只有8位和32位的,所以要少使用CV_64F,但是vs的编译器又会把float数据自动变成double型,有些不太爽。

原创粉丝点击