step的几个类别区分:
- Mat::Mat(); //default
- Mat::Mat(int rows, int cols, int type);
- Mat::Mat(Size size, int type);
- Mat::Mat(int rows, int cols, int type, const Scalar& s);
- Mat::Mat(Size size, int type, const Scalar& s);
- Mat::Mat(const Mat& m);
- //参数说明:
- //int rows:高
- //int cols:宽
- //int type:参见"Mat类型定义"
- //Size size:矩阵尺寸,注意宽和高的顺序:Size(cols, rows)
- //const Scalar& s:用于初始化矩阵元素的数值
- //const Mat& m:拷贝m的矩阵头给新的Mat对象,但是不复制数据!相当于创建了m的一个引用对象
-
- //例子1:创建100*90的矩阵,矩阵元素为3通道32位浮点型
- cv::Mat M(100, 90, CV_32FC3);
- //例子2:使用一维或多维数组来初始化矩阵,
- double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
- cv::Mat M = cv::Mat(3, 3, CV_64F, m);
-
- //2.使用create函数:
- Mat a = create(10, 9, CV_16U); //创建10*9的矩阵,矩阵元素为16位无符号整型
- //create的一个特殊用法:如果初始化的时候没有传入size的参数,或者后面需要改变size的参数,可以使用create来调整
- // make 7x7 complex matrix filled with 1+3j.
- cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
- // and now turn M to 100x60 15-channel 8-bit matrix.
- // The old content will be deallocated:隐式使用release()释放
- M.create(100,60,CV_8UC(15));
- //第一种方法
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- image.at
(h , w)[0] = 255 ; - image.at
(h , w)[1] = 0 ; - image.at
(h , w)[2] = 0 ; - }
- }
- imshow("color1" , image) ;
- //方法二
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- Vec3b &bgr = image.at
(h , w) ; - bgr.val[0] = 0 ;
- bgr.val[1] = 255 ;
- bgr.val[2] = 0 ;
- }
- }
- //三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
- //第一种方法
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- uchar *ptr = image.ptr
(h , w) ; - ptr[0] = 255 ;
- ptr[1] = 0 ;
- ptr[2] = 0 ;
- }
- }
- imshow("color1" , image) ;
- //第二种方法
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- Vec3b *ptr = image.ptr
(h , w) ; - ptr->val[0] = 0 ;
- ptr->val[1] = 255 ;
- ptr->val[2] = 0 ;
- }
- }
-
- //单通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
- //第一种方法
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- uchar *ptr = image.ptr(h) ;
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- ptr[w] = 128 ;
- }
- }
-
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols / 2 ; ++ w)
- {
- uchar *ptr = image.ptr
(h , w) ; - *ptr = 255 ;
- }
- }
- //三通道图像
- Mat_
::iterator it = image.begin() ; - Mat_
::iterator itend = image.end() ; -
- for(;it != itend ; ++ it)
- {
- (*it)[0] = 255 ;
- (*it)[1] = 0 ;
- (*it)[2] = 0 ;
- }
-
- //单通道图像
- image = imread("forest.jpg" , 0) ;
- Mat_
::iterator it1 = image.begin() ; - Mat_
::iterator itend1 = image.end() ; - for (;it1 != itend1 ; ++ it1)
- {
- (*it1) = 128 ;
- }
- data = image.data ;
- for(int h = 0 ; h < image.rows ; ++ h)
- {
- for(int w = 0 ; w < image.cols/2 ; ++ w)
- {
- *data ++ = 128 ;
- }
- }
- for(int i = 0 ; i < 100 ; ++ i)
- {
- image.row(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i行数据
- image.col(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i列数据
- }
- //单通道多通道都适用
- int nRows = image.rows ;
- int nCols = image.cols * image.channels() ;
-
- if(image.isContinuous())
- {
- nCols = nRows * nCols ;
- nRows = 1 ;
- }
-
- for(int h = 0 ; h < nRows ; ++ h)
- {
- uchar *ptr = image.ptr
(h) ; - for(int w = 0 ; w < nCols ; ++ w)
- {
- *ptr ++ = 128 ;
- }
- }
当使用at以及对数据进行运算时候需要指定类型。cv::Mat 类的对象有一个成员函数type()用来返回矩阵元素的数据类型

访问:
- cv::Vec3b vec3b = img.at
(0,0); - uchar vec3b0 = img.at
(0,0)[0]; - uchar vec3b1 = img.at
(0,0)[1]; - uchar vec3b2 = img.at
(0,0)[2]; -
定义:
- typedef Vec
2> Vec2b; - typedef Vec
3> Vec3b; - typedef Vec
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;