Point_
typedef Point_<int> Point2i; typedef Point2i Point; typedef Point_<float> Point2f; typedef Point_<double> Point2d;
Point3_
typedef Point3_<int> Point3i; typedef Point3_<float> Point3f; typedef Point3_<double> Point3d;
Size_
typedef Size_<int> Size2i; typedef Size2i Size; typedef Size_<float> Size2f;
Rect_
typedef Rect_<int> Rect;
Matx
typedef Matx<float,1,2> Matx12f; typedef Matx<double,1,2> Matx12d; ...
Vec
typedef Vec<uchar, 2> Vec2b; typedef Vec<short,3> Vec3s; typedef Vec<int, 4> Vec4i; typddef Vec<float,6> Vec6i; ...
Scalar_
typedef Scalar_<double> Scalar;
Range
class Range { public: ... int start, end; };
ex:取A的全部行,和 1到180列
Mat A = imread("b.jpg", CV_LOAD_IMAGE_COLOR); Mat B = A(Range::all(), Range(1, 180));
Mat
创建复杂的矩阵
// make a 7x7 complex matrix filled with 1+3j. Mat M(7,7,CV_32FC2,Scalar(1,3)); // and now turn M to a 100x60 15-channel 8-bit matrix. // The old content will be deallocated M.create(100,60,CV_8UC(15));
多维数组
// create a 100x100x100 8-bit array int sz[] = {100, 100, 100}; Mat bigCube(3, sz, CV_8U, Scalar::all(0));
矩阵行操作
// add the 5-th row, multiplied by 3 to the 3rd row M.row(3) = M.row(3) + M.row(5)*3;
矩阵列操作
// now copy the 7-th column to the 1-st column // M.col(1) = M.col(7); // this will not work Mat M1 = M.col(1); M.col(7).copyTo(M1);
locateROI使用
Mat A = Mat::eye(10, 10, CV_32S); // extracts A columns, 1 (inclusive) to 3 (exclusive). Mat B = A(Range::all(), Range(1, 3)); // extracts B rows, 5 (inclusive) to 9 (exclusive). // that is, C ~ A(Range(5, 9), Range(1, 3)) Mat C = B(Range(5, 9), Range::all()); Size size; Point ofs; C.locateROI(size, ofs); // size will be (width=10,height=10) and the ofs will be (x=1, y=5)
矩阵元素访问:
指针加[ ]
int sum = 0; Mat M = Mat::eye(20, 20, CV_8UC1); for (int i=0; i < M.rows; i++) { const uchar* mi = M.ptr<uchar>(i); for (int j=0; j < M.cols; j++) { sum += mi[j]; } }
迭代
MatConstIterator_<uchar> it = M.begin<uchar>(), it_end = M.end<uchar>(); for (; it != it_end; ++it) sum += *it;