OpenCV学习(12)
作者:互联网
基本图像容器Mat的用法(3):
opencv中的格式化输出方法;
OpenCV提供了风格各异的格式化输出方法,这里将对这些方法一一进行演示和列举。
首先是下面代码中将要使用的r矩阵的定义。需要注意,我们可以通过用randu()函数产生的随机值来填充矩阵,需要给定一个上限和下限来确保随机值在期望的范围内。
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
初始化完r矩阵,下面便开始对输出风格的讲解。
①风格一,opencv默认风格
代码如下:
#include<opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>//opencv highgui 头文件
#include<opencv2/imgproc/imgproc.hpp>//opencv 图像处理头文件
#include <opencv2/core/utils/logger.hpp>
#include<iostream>
using namespace std;
using namespace cv;//包含cv命名空间
int main()
{
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
cout << "r (opencv默认风格) = " << endl << " " << r <<";" << endl << endl;
return 0;
}
运行结果:
②风格二,Python 风格
代码如下(opencv3版):
#include<opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>//opencv highgui 头文件
#include<opencv2/imgproc/imgproc.hpp>//opencv 图像处理头文件
#include <opencv2/core/utils/logger.hpp>
#include<iostream>
using namespace std;
using namespace cv;//包含cv命名空间
int main()
{
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
cout << "r (Python风格) = " << endl << " " << format(r,Formatter::FMT_PYTHON) << ";" << endl << endl;
return 0;
}
运行结果:
③风格三,逗号分隔风格
代码如下(opencv3版):
#include<opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>//opencv highgui 头文件
#include<opencv2/imgproc/imgproc.hpp>//opencv 图像处理头文件
#include <opencv2/core/utils/logger.hpp>
#include<iostream>
using namespace std;
using namespace cv;//包含cv命名空间
int main()
{
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
cout << "r (逗号分隔风格) = " << endl << " " << format(r,Formatter::FMT_CSV) << ";" << endl << endl;
return 0;
}
运行结果:
④风格四,Numpy
代码如下(opencv3版):
#include<opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>//opencv highgui 头文件
#include<opencv2/imgproc/imgproc.hpp>//opencv 图像处理头文件
#include <opencv2/core/utils/logger.hpp>
#include<iostream>
using namespace std;
using namespace cv;//包含cv命名空间
int main()
{
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
cout << "r (Numpy风格) = " << endl << " " << format(r,Formatter::FMT_NUMPY) << ";" << endl << endl;
return 0;
}
运行结果:
⑤风格五,c语言风格
代码如下(opencv3版):
#include<opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>//opencv highgui 头文件
#include<opencv2/imgproc/imgproc.hpp>//opencv 图像处理头文件
#include <opencv2/core/utils/logger.hpp>
#include<iostream>
using namespace std;
using namespace cv;//包含cv命名空间
int main()
{
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
Mat r = Mat(10, 3, CV_8UC3);
randu(r,Scalar::all(0),Scalar::all(255));
cout << "r (c语言风格) = " << endl << " " << format(r,Formatter::FMT_C) << ";" << endl << endl;
return 0;
}
运行结果:
标签:12,Mat,opencv,学习,OpenCV,Scalar,using,include,cv 来源: https://blog.csdn.net/m0_53123717/article/details/122514157