Mat类的构造函数有20多种,详见https://docs.opencv.org/4.1.1/d3/d63/classcv_1_1Mat.html#af1d014cecd1510cdf580bf2ed7e5aafc;
现列出几种常用构造函数以及方法:
1.Mat(int rows,int cols ,int type);
parameters:
rows | Number of rows in a 2D array. |
cols | Number of columns in a 2D array. |
type | Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
2.Mat(Size size ,int type);
Parameters :
size | 2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the number of columns go in the reverse order. |
type | Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
3. Mat(int rows,int cols ,int type,const Scalar & s);
Parameters:
rows | Number of rows in a 2D array. |
cols | Number of columns in a 2D array. |
type | Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
s | An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use the assignment operator Mat::operator=(const Scalar& value) . |
4.Mat(Size size ,int type,const Scalar & s);
Parameters:
size | 2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the number of columns go in the reverse order. |
type | Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
s | An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use the assignment operator Mat::operator=(const Scalar& value) . |
Mat img;//创建无初始化矩阵
Mat img1(200,100,CV_8UC1);//200行,100列(长200,宽100)
Mat img2(Size(200,100),CV_8UC3,Scalar(0,255,0));//长100,宽200
Mat img3(200,100,CV_8UC3,Scalar(0,255,0));//创建200行,100列的8位三通道矩阵
Mat img4(200,100,CV_8UC1,Scalar(255));//创建单通道矩阵
方法:
参考:https://docs.opencv.org/4.1.1/d3/d63/classcv_1_1Mat.html#a33fd5d125b4c302b0c9aa86980791a77;
Mat img1 = imread("E:\\项目\\OPENCV\\Mat\\1.jpg", 1);
Mat img2(img1);
Mat img3 = img1;
Mat img4 = img1.clone();
Mat img5;
img1.copyTo(img5); cvtColor(img1, img1, COLOR_BGR2HSV);//BGR图转为HSV图
// cvtColor(img1, img1, COLOR_BGR2GRAY);
imshow("img1", img1);
imshow("img2", img2);
imshow("img3", img3);//image1/2/3跟随原图变化
imshow("img4", img4);
imshow("img5", img5);//image4/5不会跟随原图变化
waitKey(0); 二、绘图函数 1.line(Mat& Outputarray,Point startPoint,Point endPoint,const Scalar& color,int thickness=1, int lineType=8, int shift=0) Outputarray: 要绘制线段的图像。 startPoint: 线段的起点。 endPoint: 线段的终点。 color: 线段的颜色,通过一个Scalar对象定义。 thickness: 线条的宽度。 lineType: 线段的类型。可以取值8, 4, 和CV_AA, 分别代表8邻接连接线,4邻接 连接线和反锯齿连接线。默认值为8邻接。为了获得更好地效果可以选用CV_AA(采用了高斯滤波)。 shift: 坐标点小数点位数。 2. Circle(Mat& img, CvPoint center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0) img绘制圆的图像 center为画圆的圆心坐标 radius为圆的半径 color为设定圆的颜色,规则根据B(蓝)G(绿)R(红) thickness 如果是正数,表示组成圆的线条的粗细程度。否则,表示圆是否被填充 line_type 线条的类型。默认是8 shift 圆心坐标点和半径值的小数点位数 3. Rectangle( Mat& img, img, Point pt1, Point pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 ) img图像. pt1矩形的一个顶点。 pt2矩形对角线上的另一个顶点 color线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。 thickness组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。 line_type线条的类型。 见Line的描述shift坐标点的小数点位数。 4.ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0)
-
第一个参数,InputOutputArray img,要绘制椭圆的图像;
-
第二个参数,const RotatedRect& box,通过RotatedRect选择椭圆表示,这意味着函数在旋转矩形中画一个椭圆;
-
第三个参数,Point center,椭圆的中心点;
-
第四个参数,Size axes,椭圆主轴尺寸的一半;
-
第五个参数,double angle,椭圆旋转角,以度为单位;
-
第六个参数,double startAngle,椭圆弧的起始角,以度为单位;
-
第七个参数,double endAngle,椭圆弧的结束角,以度为单位;
-
第八个参数,const Scalar& color,绘制椭圆线的颜色;
-
第九个参数,int thickness = 1,线段的粗细;
-
第十个参数,int lineType = LINE_8,线段的类型;
-
线的类型:
FILLED,填充;
LINE_4,4连通的线条;
LINE_8 ,8连通的线条;
LINE_AA ,抗锯齿线条;
imshow("原图", image);
RNG rng = theRNG();
line(image, Point(20, 20), Point(100, 100), Scalar(0,0,0), 5, 8);
Scalar color(rng.uniform(0, 250), rng.uniform(0, 250), rng.uniform(0, 250));
circle(image, Point(60, 60), 60, color, 1, 8);
rectangle(image, Point(20, 20), Point(100, 100), color, 1, 8);
imshow("绘图", image);