Mat类创建二维、三维矩阵并访问以及opencv矩阵运算

 一、opencv宽高对应关系:

Mat.rows = Mat.size().height = 高
Mat.cols = Mat.size().width = 宽

int sz_1[2] = { 200, 400 }; // {高,宽}  {Mat.rows,Mat.cols}
Mat m = cv::Mat(2, sz_1, CV_8UC1,Scalar::all(255));
or
Mat m = cv::Mat::ones(2, sz_1, CV_8UC1);
or
Mat m = cv::Mat::zeros(200, 400, CV_8UC1); // 高:200   宽:400

 二、opencv矩阵创建以及元素级访问

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int main(){
	Mat img = imread("123.jpg");
	Mat gray;
	cvtColor(img, gray, COLOR_BGR2GRAY);

	// 创建单通道图片
	int sz[2] = { gray.rows, gray.cols };  // {高,宽}
        // 或者使用:Mat fgCount = Mat(2, sz, CV_8UC1, Scalar::all(0));
	Mat fgCount = cv::Mat::zeros(2, sz, CV_8UC1);
	cv::imshow("fgCount", fgCount);
	cv::waitKey(2000);
	fgCount.at(20, 20) = 20;
	fgCount.at(21, 21) = 21;
	std::cout << "hahhahh------------------" << std::endl;
	std::cout << int(fgCount.at(20, 20)) << std::endl;
	std::cout << int(fgCount.at(21, 21)) << std::endl;

	// 创建三维矩阵
	int samples_size[3];
	int height = gray.size().height;
	int width = gray.size().width;
	samples_size[0] = 20;
	samples_size[1] = gray.rows;
	samples_size[2] = gray.cols;
	Mat m_3 = cv::Mat::zeros(3, samples_size, CV_8UC1);
	std::cout << "Yes" < Vec20f;
	Mat m_2 = cv::Mat::zeros(gray.size().width, gray.size().height, CV_64FC(20));
	std::cout << "Yes" << std::endl;
	cout << "图像的维度:" << m_2.dims << endl;
	cout << "图像的通道数:" << m_2.channels() << endl;
	m_2.at(20, 20)[11] = 123.33;
	cout << m_2.at(20, 20)[11] << endl;

        // 遍历图片
        Mat src = imread("haha.png");
	Mat img = imread("haha.png");
	Mat img_2 = imread("haha.png");
	int nl = img.rows; //行数
	int nc = img.cols * img.channels(); //每行元素的总元素数量
	int div = 64;
	int cols = img.cols;
	// 采用指针
	double start_time = (double)getTickCount();
	for (int j = 0; j < nl; j++)
	{
		uchar* data = img.ptr(j);
		for (int i = 0; i < cols; i++)
		{
			data[i* img.channels()] = 151;
		}               
	}
	double end_time = (double)getTickCount();
	double fps = (end_time - start_time) / getTickCount();
	cout << "采用指针所消耗时间:" << fps << endl;

	// 采用内置at方法
	start_time = (double)getTickCount();
	for (int j = 0; j < nl; j++)
	{
		for (int i = 0; i < cols; i++)
		{
			img_2.at(j, i)[1] = 151;
		}
	}
	end_time = (double)getTickCount();
	fps = (end_time - start_time) / getTickCount();
	cout << "采用内置at方法消耗时间:" <

Mat类创建二维、三维矩阵并访问以及opencv矩阵运算_第1张图片

 初始化Mat对象:

Mat grad_x = (Mat_(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);
Mat grad_y = (Mat_(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
cout << grad_x << endl;
cout << grad_y << endl;

 

Mat类创建二维、三维矩阵并访问以及opencv矩阵运算_第2张图片

三、opencv矩阵运算

现在opencv的Mat类矩阵运算操作已经和numpy很像了,一些案例如下:

void matOP() {
	Mat mat_1(4, 9, CV_16SC1);
	Mat mat_2(4, 9, CV_16SC1);
	Mat mat_3(4, 9, CV_16SC1);

	RNG rng(unsigned int(time(NULL)));
	rng.fill(mat_1, RNG::UNIFORM, -4, 4, true);
	rng.fill(mat_2, RNG::UNIFORM, 0, 10, true);

	mat_3 = mat_1 + mat_2;

	cout << "mat_1:" << endl;
	cout << mat_1 << endl;
	cout << "mat_2:" << endl;
	cout << mat_2 << endl;
	cout << "mat_3:" << endl;
	cout << mat_3 << endl;

	Mat big_zero = Mat::zeros(4, 9, CV_16SC1);
	big_zero = mat_3 >= 0;
	cout << "big_zero:" << endl;
	cout << big_zero << endl;

	Mat ret_1 = Mat::zeros(4, 9, CV_16SC1);
	bitwise_and(mat_3, mat_3, ret_1, big_zero);
	cout << "ret_1:" << endl;
	cout << ret_1;

	int a;
	cin >> a;
}

结果截图:

Mat类创建二维、三维矩阵并访问以及opencv矩阵运算_第3张图片

四、opencv产生随机数

比RNG方便,如下(每次产生的随机是一样的):

void rand_test() {
	Mat R = Mat(3, 2, CV_16SC3);
	// randu(dst, low, high);dst – 输出数组或矩阵 ;low – 区间下界(闭区间); high - 区间上界(开区间)
	randu(R, -4, 5);                         // 返回均匀分布的随机数,填入数组或矩阵
	cout << R << endl;

	// randn(dst, mean, stddev);dst – 输出数组或矩阵; mean – 均值; stddev - 标准差
	randn(R, -4, 4);                        // 返回高斯分布的随机数,填入数组或矩阵
	cout << R << endl;

	/*randShuffle(InputOutputArray dst,     输入输出数组(一维)
					double iterFactor = 1., 决定交换数值的行列的位置的一个系数...
					RNG* rng = 0)          (可选)随机数产生器,0表示使用默认的随机数产生器,即seed = -1。rng决定了打乱的方法*/
	unsigned int seed = time(NULL);
	RNG rng(seed);
	randShuffle(R, 1, &rng);                // 将原数组(矩阵)打乱
	cout << R << endl;
}

结果:

Mat类创建二维、三维矩阵并访问以及opencv矩阵运算_第4张图片

 

你可能感兴趣的:(Cplusplus,opencv)