当前位置:   article > 正文

2.cv::Mat初始化及其他类型转Mat_cv mat初始化

cv mat初始化

Mat的构造函数

 	Mat () 
 	Mat (int rows, int cols, int type)
 	Mat (Size size, int type)
 	Mat (int rows, int cols, int type, const Scalar &s)
 	Mat (Size size, int type, const Scalar &s)
 	Mat (int ndims, const int *sizes, int type)
 	Mat (const std::vector< int > &sizes, int type)
 	Mat (int ndims, const int *sizes, int type, const Scalar &s)
 	Mat (const std::vector< int > &sizes, int type, const Scalar &s)
 	Mat (const Mat &m)
 	Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
 	Mat (Size size, int type, void *data, size_t step=AUTO_STEP)
 	Mat (int ndims, const int *sizes, int type, void *data, const size_t *steps=0)
 	Mat (const std::vector< int > &sizes, int type, void *data, const size_t *steps=0)
 	Mat (const Mat &m, const Range &rowRange, const Range &colRange=Range::all())
 	Mat (const Mat &m, const Rect &roi)
 	Mat (const Mat &m, const Range *ranges)
 	Mat (const Mat &m, const std::vector< Range > &ranges)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

mat数值类型

typetype
CV_8UC1 表示8位无符号整型1通道
CV_8UC2
CV_8UC3
CV_8UC4
CV_8UC(n)表示8位无符号整型n通道
CV_8SC1表示8位整型1通道
CV_8SC2
CV_8SC3
CV_8SC4
CV_8SC(n)表示8位整型n通道
CV_16UC1表示16位无符号整型1通道
CV_16UC2
CV_16UC3
CV_16UC4
CV_16UC(n)表示16位无符号整型n通道
CV_16SC1表示16位整型1通道
CV_16SC2
CV_16SC3
CV_16SC4
CV_16SC(n)表示16位整型n通道
CV_32SC1表示32位整型1通道
CV_32SC2
CV_32SC3
CV_32SC4
CV_32SC(n)表示32位整型n通道
CV_32FC1表示32位浮点型1通道
CV_32FC2
CV_32FC3
CV_32FC4
CV_32FC(n)表示32位浮点型n通道
CV_64FC1表示64位浮点型1通道
CV_64FC2
CV_64FC3
CV_64FC4
CV_64FC(n)表示64位浮点型n通道

例:Mat初始化

#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"

using namespace std;
int main() {
	
	//自定义Mat初始化
	cv::Mat a1 = (cv::Mat_<int>(3, 3) <<1, 2, 3, 4, 5, 6, 7, 8, 9);
	cv::Mat b1 = cv::Mat::Mat(2, 2, CV_16FC1, cv::Scalar(0.5));
	cv::Mat b2 = cv::Mat::Mat(2, 2, CV_16FC3, cv::Scalar(2.5,1.5,0.5));
	cv::Mat c1(3, 3, CV_8UC3, cv::Scalar(0, 0, 255));

	//数组类型转Mat
	int sz[2][1] = { 1, 3 };
	cv::Mat d1(2, 1, CV_8UC1, sz);

	//vector类型转Mat
	std::vector<std::vector<double> > v1 = { { 7, 5, 16, 8 },{ 1, 2, 3, 4 } };
	cv::Mat mat1(v1.size(), v1.at(0).size(), CV_64FC1);
	for (int i = 0; i < mat1.rows; ++i)
		for (int j = 0; j < mat1.cols; ++j)
			mat1.at<double>(i, j) = v1.at(i).at(j);
	
	// 类似matlib初始化
	cv::Mat e1 = cv::Mat::zeros(2, 3, CV_8UC1);
	cv::Mat e2 = cv::Mat::ones(4, 4, CV_16FC1);
	cv::Mat e3 = cv::Mat::eye(4, 4, CV_16FC1);

	// 提取主对角线元素
	cv::Mat f1 = a1.diag(0);
;
	cout << "a1:\n" << a1 << endl;
	cout << "b1:\n" << b1 << endl;
	cout << "b2:\n" << b2 << endl;
	cout << "c1:\n" << c1 << endl;
	cout << "d1:\n" << d1 << endl;
	cout << "mat1:\n" << mat1 << endl;
	cout << "e1:\n" << e1 << endl;
	cout << "e2:\n" << e2 << endl;
	cout << "e3:\n" << e3 << endl;
	cout << "f1:\n" << f1 << endl;

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

结果:

a1:
[1, 2, 3;
 4, 5, 6;
 7, 8, 9]
b1:
[0.5, 0.5;
 0.5, 0.5]
b2:
[2.5, 1.5, 0.5, 2.5, 1.5, 0.5;
 2.5, 1.5, 0.5, 2.5, 1.5, 0.5]
c1:
[  0,   0, 255,   0,   0, 255,   0,   0, 255;
   0,   0, 255,   0,   0, 255,   0,   0, 255;
   0,   0, 255,   0,   0, 255,   0,   0, 255]
d1:
[  1;
   0]
mat1:
[7, 5, 16, 8;
 1, 2, 3, 4]
e1:
[  0,   0,   0;
   0,   0,   0]
e2:
[1, 1, 1, 1;
 1, 1, 1, 1;
 1, 1, 1, 1;
 1, 1, 1, 1]
e3:
[1, 0, 0, 0;
 0, 1, 0, 0;
 0, 0, 1, 0;
 0, 0, 0, 1]
f1:
[1;
 5;
 9]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

例:

#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;
int main() {
	
	cv::Mat img = cv::imread("building.jpeg",1);
	if (img.empty()) {
	cout << "...." << endl;
	return -1;
	}
	cv::Rect rect(100, 10, 200, 100);//(x,y=(100,10),w=200,h=100
	cv::Mat roi = cv::Mat(img, rect);
	cv::Mat img_rect = img.clone();
	cv::rectangle(img_rect, rect, cv::Scalar(0, 255, 0), 2);
	cv::imshow("original image with rectangle", img_rect);
	cv::imshow("roi",roi);
	cv::waitKey();
	

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/886851
推荐阅读
相关标签
  

闽ICP备14008679号