OpenCV——创建Mat对象、格式化输出、常用数据结构和函数(point,vector、Scalar、Size、Rect、cvtColor)
转载至:https://www.cnblogs.com/long5683/p/9643692.html
实践代码:
//---------------------------------【头文件、命名空间包含部分】--------------------------- // 描述:包含程序所使用的头文件和命名空间 //----------------------------------------------------------------------------------------------- #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace std; using namespace cv; //--------------------------------------【main( )函数】----------------------------------------- // 描述:控制台应用程序的入口函数,我们的程序从这里开始执行 //----------------------------------------------------------------------------------------------- int main(int, char**) { //改变控制台的前景色和背景色 system("color 8F"); Mat I = Mat::eye(4, 4, CV_64F); I.at<double>(1, 1) = CV_PI; cout << "\nI = " << I << ";\n" << endl; Mat r = Mat(10, 3, CV_8UC3); randu(r, Scalar::all(0), Scalar::all(255)); //此段代码的OpenCV2版为: //cout << "r (OpenCV默认风格) = " << r << ";" << endl << endl; //cout << "r (Python风格) = " << format(r,"python") << ";" << endl << endl; //cout << "r (Numpy风格) = " << format(r,"numpy") << ";" << endl << endl; //cout << "r (逗号分隔风格) = " << format(r,"csv") << ";" << endl<< endl; //cout << "r (C语言风格) = " << format(r,"C") << ";" << endl << endl; //此段代码的OpenCV3版为: cout << "r (OpenCV默认风格) = " << r << ";" << endl << endl; cout << "r (Python风格) = " << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl; cout << "r (Numpy风格) = " << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl; cout << "r (逗号分隔风格) = " << format(r, Formatter::FMT_CSV) << ";" << endl << endl; cout << "r (C语言风格) = " << format(r, Formatter::FMT_C) << ";" << endl << endl; Point2f p(6, 2); cout << "【2维点】p = " << p << ";\n" << endl; Point3f p3f(8, 2, 0); cout << "【3维点】p3f = " << p3f << ";\n" << endl; vector<float> v; v.push_back(3); v.push_back(5); v.push_back(7); cout << "【基于Mat的vector】shortvec = " << Mat(v) << ";\n" << endl; vector<Point2f> points(20); for (size_t i = 0; i < points.size(); ++i) points[i] = Point2f((float)(i * 5), (float)(i % 7)); cout << "【二维点向量】points = " << points << ";"; getchar();//按任意键退出 return 0; }