OpenCV基本数据类型

来源:互联网 发布:linux常用查看命令 编辑:程序博客网 时间:2024/04/29 09:59

Point_

[cpp] view plaincopy
  1. typedef Point_<int> Point2i;  
  2. typedef Point2i Point;  
  3. typedef Point_<float> Point2f;  
  4. typedef Point_<double> Point2d;  

Point3_

[cpp] view plaincopy
  1. typedef Point3_<int> Point3i;  
  2. typedef Point3_<float> Point3f;  
  3. typedef Point3_<double> Point3d;  

Size_

[html] view plaincopy
  1. typedef Size_<int> Size2i;  
  2. typedef Size2i Size;  
  3. typedef Size_<float> Size2f;  

Rect_

[cpp] view plaincopy
  1. typedef Rect_<int> Rect;  

Matx

[cpp] view plaincopy
  1. typedef Matx<float,1,2> Matx12f;  
  2. typedef Matx<double,1,2> Matx12d;  
  3. ...  

Vec

[cpp] view plaincopy
  1. typedef Vec<uchar, 2> Vec2b;  
  2. typedef Vec<short,3> Vec3s;  
  3. typedef Vec<int, 4> Vec4i;  
  4. typddef Vec<float,6> Vec6i;  
  5. ...  

Scalar_

[cpp] view plaincopy
  1. typedef Scalar_<double> Scalar;  

Range

[cpp] view plaincopy
  1. class Range  
  2. {  
  3. public:  
  4. ...  
  5. int start, end;  
  6. };  

ex:取A的全部行,和 1到179列
[cpp] view plaincopy
  1. Mat A = imread("b.jpg", CV_LOAD_IMAGE_COLOR);  
  2. Mat B = A(Range::all(), Range(1, 180));  

Mat

创建复杂的矩阵

[cpp] view plaincopy
  1. // make a 7x7 complex matrix filled with 1+3j.  
  2. Mat M(7,7,CV_32FC2,Scalar(1,3));  
  3. // and now turn M to a 100x60 15-channel 8-bit matrix.  
  4. // The old content will be deallocated  
  5. M.create(100,60,CV_8UC(15));  
多维数组

[cpp] view plaincopy
  1. // create a 100x100x100 8-bit array  
  2. int sz[] = {100, 100, 100};  
  3. Mat bigCube(3, sz, CV_8U, Scalar::all(0));  
矩阵行操作

[cpp] view plaincopy
  1. // add the 5-th row, multiplied by 3 to the 3rd row  
  2. M.row(3) = M.row(3) + M.row(5)*3;  

矩阵列操作

[cpp] view plaincopy
  1. // now copy the 7-th column to the 1-st column  
  2. // M.col(1) = M.col(7); // this will not work  
  3. Mat M1 = M.col(1);  
  4. M.col(7).copyTo(M1);  

locateROI使用

[cpp] view plaincopy
  1. Mat A = Mat::eye(10, 10, CV_32S);  
  2. // extracts A columns, 1 (inclusive) to 3 (exclusive).  
  3. Mat B = A(Range::all(), Range(1, 3));  
  4. // extracts B rows, 5 (inclusive) to 9 (exclusive).  
  5. // that is, C ~ A(Range(5, 9), Range(1, 3))  
  6. Mat C = B(Range(5, 9), Range::all());  
  7. Size size; Point ofs;  
  8. C.locateROI(size, ofs);  
  9. // size will be (width=10,height=10) and the ofs will be (x=1, y=5)  

矩阵元素访问:

指针加[ ] 

[cpp] view plaincopy
  1. int sum = 0;  
  2.     Mat M = Mat::eye(20, 20, CV_8UC1);  
  3.     for (int i=0; i < M.rows; i++)  
  4.     {  
  5.         const uchar* mi = M.ptr<uchar>(i);  
  6.         for (int j=0; j < M.cols; j++)  
  7.         {  
  8.             sum += mi[j];  
  9.         }  
  10.     }  

迭代

[cpp] view plaincopy
  1. MatConstIterator_<uchar> it = M.begin<uchar>(), it_end = M.end<uchar>();  
  2. for (; it != it_end; ++it)  
  3.     sum += *it;  

from: http://blog.csdn.net/abcjennifer/article/details/7629349
1 0