DataType : 將C++數據類型轉換爲對應的opencv數據類型算法
enum { CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6 };
// allocates a 30x40 floating-point matrix // CV_32F Mat A(30, 40, DataType<float>::type); Mat B = Mat_<std::complex<double> >(3, 3); // the statement below will print 6, 2 /*, that is depth == CV_64F, channels == 2*/ CV_64FC2 cout << B.depth() << ", " << B.channels() << endl;
Point_ 二維點座標(x,y)express
typedef Point_<int> Point2i; typedef Point2i Point; typedef Point_<float> Point2f; typedef Point_<double> Point2d;
Point3_ 3維點座標(x,y,z)數組
typedef Point3_<int> Point3i; typedef Point3_<float> Point3f; typedef Point3_<double> Point3d;
Size_ 尺寸(width, height)框架
typedef Size_<int> Size2i; typedef Size2i Size; typedef Size_<float> Size2f;
Rect_ 矩形區域(x,y,width,height) ,(x,y)左上角座標, 範圍[x, x + width), [y, y + height)less
rect = rect ± point //矩形偏移(shifting a rectangle by a certain offset) rect = rect ± size //改變大小(expanding or shrinking a rectangle by a certain amount) rect += point, rect -= point, rect += size, rect -= size //(augmenting operations) rect = rect1 & rect2 //矩形交集(rectangle intersection) rect = rect1 | rect2 //包含r1r2的最小矩形(minimum area rectangle containing rect2 and rect3 ) rect &= rect1, rect |= rect1 //(and the corresponding augmenting operations) rect == rect1, rect != rect1 //(rectangle comparison)
RotatedRect 旋轉矩形函數
RotatedRect::RotatedRect(const Point2f& center, const Size2f& size, float angle)// 中心點(不是左上角座標),尺寸,旋轉角度 RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
Matx 小矩陣spa
template<typename_Tp, int m, int n> class Matx {...}; typedef Matx<float, 1, 2> Matx12f; typedef Matx<double, 1, 2> Matx12d; ... typedef Matx<float, 1, 6> Matx16f; typedef Matx<double, 1, 6> Matx16d; typedef Matx<float, 2, 1> Matx21f; typedef Matx<double, 2, 1> Matx21d; ... typedef Matx<float, 6, 1> Matx61f; typedef Matx<double, 6, 1> Matx61d; typedef Matx<float, 2, 2> Matx22f; typedef Matx<double, 2, 2> Matx22d; ... typedef Matx<float, 6, 6> Matx66f; typedef Matx<double, 6, 6> Matx66d; Matx33f m(1, 2, 3, 4, 5, 6, 7, 8, 9); cout << sum(Mat(m*m.t())) << endl;//Matx轉化爲Mat
Vec 短向量,基於Matx3d
template<typename_Tp, int n> class Vec : public Matx<_Tp, n, 1> {...}; 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<int, 2> Vec2i; typedef Vec<int, 3> Vec3i; typedef Vec<int, 4> Vec4i; 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;
Scalar_ 四維向量指針
template<typename_Tp> class Scalar_: public Vec<_Tp, 4> { ... }; typedef Scalar_<double> Scalar;
Range 範圍,(start, end)code
Mat m(300,300,CV32F); Mat part = m(Range::all(), Range(20, 200)); // 至關於matlab的m(:, 20 : 199)
對於自定義的函數,能夠用以下方法來支持Range
void my_function(..., const Range& r, ....) { if(r == Range::all()) { // process all the data, 使用所有數據 } else { // process [r.start, r.end),根據r中定義, 處理數據 start : end - 1 } }
Mat 矩陣結構
M.step[i] >= M.step[i+1]*M.sizes[i+1]; //這裏大因而由於數據空間可能有空白
addr(M(i(0),...,i(M.dims−1))) = M.data + M.step[0] ∗ i(0)+ M.step[1] ∗ i(1)+ ... + M.step[M.dims − 1] ∗ i(M.dims−1)
建立數組:
// 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));
建立特殊矩陣:
屬性相關:
矩陣操做:
賦值相關:
InputArray
OutputArray
//Do not explicitly create InputArray, OutputArray instances
void myAffineTransform(InputArray_src, OutputArray_dst, InputArray_m) { // get Mat headers for input arrays. This is O(1) operation, // unless_src and/or_m are matrix expressions. Mat src =_src.getMat(), m =_m.getMat(); CV_Assert( src.type() == CV_32FC2 && m.type() == CV_32F && m.size() == Size(3, 2) ); // [re]create the output array so that it has the proper size and type. // In case of Mat it calls Mat::create, in case of STL vector it calls vector::resize. _dst.create(src.size(), src.type()); Mat dst =_dst.getMat(); for( int i = 0; i < src.rows; i++ ) for( int j = 0; j < src.cols; j++ ) { Point2f pt = src.at<Point2f>(i, j); dst.at<Point2f>(i, j) = Point2f(m.at<float>(0, 0)*pt.x + m.at<float>(0, 1)*pt.y + m.at<float>(0, 2), m.at<float>(1, 0)*pt.x + m.at<float>(1, 1)*pt.y + m.at<float>(1, 2)); } }
SparseMat 稀疏矩陣Algorithm 實現一個算法的框架