1 Mat的声明:
using namespace cv;
Mat m=Mat(rows, cols, type);
Mat m=Mat(Size(width,height), type);
type指矩阵中元素的类型,可以使CV_8U(无符号单字节像素),CV_8S(有符号单字节像素),CV_8UC3(3通道每个通道是无符号单字节),CV_16SC3(3通道,每个通道是16位有符号),CV_32F(单通道,32位浮点数)等等。。
如Mat m=Mat(240, 320, CV_8U,cv::Scalar(100)); //cv::Scalar(100)表示将单通道图像每个像素值初始化设为100
2 opencv 图像row、col坐标对应关系
声明:本内容转载自https://blog.csdn.net/u010189457/article/details/71553436?utm_source=itdadao&utm_medium=referral,只为阅读方便。
行列与坐标系对应关系
行rows:Y (height)
列cols:X (width)
注意!注意!注意!
在Mat类型变量访问时下标是反着写的,即:按照(y, x)的关系形式访问,下面通过代码展示来说明这一点
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat mat_src = Mat::eye(3, 4, CV_8UC1);
cout << "mat_src :" << endl;
cout << mat_src << endl;
cout << endl;
cout << "Rows : " << mat_src.rows << endl;
cout << "Cols : " << mat_src.cols << endl;
//注: mat_src.at<float>(y, x), 下标关系为: y-x
mat_src.at<float>(0, 2) = 2;
mat_src.at<float>(2, 0) = 4;
cout << endl;
cout << "mat_src :" << endl;
cout << mat_src << endl;
return 0;
}
执行结果: