OpenCV——探究Mat何时为空&&初始化Mat

it2025-04-06  17

文章目录

探究Mat何时为空初始化Mat

探究Mat何时为空

int main() { Mat a; cout << "Mat a: " << endl; if (a.empty()) { cout << " a is null" << endl; } else { cout << " a is not null" << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { uchar aa = p[j]; cout << (int)aa << " "; } cout << endl; } } cout << endl; cout << "Mat(10, 10, CV_8UC1): " << endl; a=Mat(10, 10, CV_8UC1); if (a.empty()) { cout << " a is null" << endl; } else { cout << " a is not null" << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { uchar aa = p[j]; cout << (int)aa << " "; } cout << endl; } } cout << endl; cout << "给a赋值: " << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { p[j] = i; } } if (a.empty()) { cout << " a is null" << endl; } else { cout << " a is not null" << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { uchar aa = p[j]; cout << (int)aa << " "; } cout << endl; } } cout << endl; cout << "Mat(10, 10, CV_8UC1): " << endl; a = Mat(10, 10, CV_8UC1); if (a.empty()) { cout << " a is null" << endl; } else { cout << " a is not null" << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { uchar aa = p[j]; cout << (int)aa << " "; } cout << endl; } } cout << "a.release(): " << endl; cout << endl; a.release(); if (a.empty()) { cout << " a is null" << endl; } else { cout << " a is not null" << endl; for (size_t i = 0; i < 10; i++) { uchar* p = a.ptr<uchar>(i); for (size_t j = 0; j < 10; j++) { uchar aa = p[j]; cout << (int)aa << " "; } cout << endl; } } return 0; } Mat a: a is null Mat(10, 10, CV_8UC1): a is not null 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 给a赋值: a is not null 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 Mat(10, 10, CV_8UC1): a is not null 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 a.release(): a is null

初始化Mat

int main() { /*-----------------------初始化---------------------------*/ cout << "*---------------------------通过构造函数来初始化------------------*" << endl; Mat M0(3, 3, CV_8UC1, cv::Scalar::all(255)); cout << "M0 = " << endl << M0 << endl; Mat M1(3, 3, CV_8UC1, cv::Scalar(0)); cout << "M1 = " << endl << M1 << endl; cout << "*---------------------------通过create函数来初始化------------------*" << endl; Mat M2; M2.create(4, 4, CV_8UC1);//矩阵中的数据为随机值 cout << "M2 = " << endl << M2 <<endl; cout << "*---------------------------通过opencv提供的类matlab的函数创建------------------*" << endl; Mat M3 = Mat::eye(4, 4, CV_64F); cout << "M3 = " << endl << M3 << endl; Mat M4 = Mat::ones(4, 4, CV_64F); cout << "M4 = " << endl << M4 << endl; Mat M5 = Mat::zeros(4, 4, CV_64F); cout << "M5 = " << endl << M5 << endl; cout << "*---------------------------通过自定义矩阵Mat创建------------------*" << endl; Mat M6 = (Mat_<double>(3, 3) << 0, -1, 0, -1, 0, 0, 0, 0, 1); cout << "M6 = " << endl << M6 << endl; return 0; } *---------------------------通过构造函数来初始化------------------* M0 = [255, 255, 255; 255, 255, 255; 255, 255, 255] M1 = [ 0, 0, 0; 0, 0, 0; 0, 0, 0] *---------------------------通过create函数来初始化------------------* M2 = [205, 205, 205, 205; 205, 205, 205, 205; 205, 205, 205, 205; 205, 205, 205, 205] *---------------------------通过opencv提供的类matlab的函数创建------------------* M3 = [1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1] M4 = [1, 1, 1, 1; 1, 1, 1, 1; 1, 1, 1, 1; 1, 1, 1, 1] M5 = [0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0] *---------------------------通过自定义矩阵Mat创建------------------* M6 = [0, -1, 0; -1, 0, 0; 0, 0, 1]
最新回复(0)