创建Mat 对象
来源:互联网 发布:天猫超市满99减50知乎 编辑:程序博客网 时间:2024/06/05 16:22
教程 读取、修改、保存图像 已经讲解了如何使用函数 imwrite() 将一个矩阵写入图像文件中。但是为了debug,更加方便的方式是看实际值。为此,你可以通过 Mat 的运算符 << 来实现,但要记住这只对二维矩阵有效。
Mat 不但是一个很赞的图像容器类,它同时也是一个通用的矩阵类,所以可以用来创建和操作多维矩阵。创建一个Mat对象有多种方法:
矩阵。创建一个Mat对象有多种方法:Mat() 构造函数
Mat M(2,2, CV_8UC3, Scalar(0,0,255)); cout << "M = " << endl << " " << M << endl << endl;
对于二维多通道图像,首先要定义其尺寸,即行数和列数。
然后,需要指定存储元素的数据类型以及每个矩阵点的通道数。为此,依据下面的规则有多种定义
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]比如 CV_8UC3 表示使用8位的 unsigned char 型,每个像素由三个元素组成三通道。预先定义的通道数可以多达四个。 Scalar 是个short型vector。指定这个能够使用指定的定制化值来初始化矩阵。当然,如果你需要更多通道数,你可以使用大写的宏并把通道数放在小括号中。
在 C\C++ 中通过构造函数进行初始化
int sz[3] = {2,2,2}; Mat L(3,sz, CV_8UC(1), Scalar::all(0));
上面的例子演示了如何创建一个超过两维的矩阵:指定维数,然后传递一个指向一个数组的指针,这个数组包含每个维度的尺寸;其余的相同
为已存在IplImage指针创建信息头:
IplImage* img = cvLoadImage("greatwave.png", 1);Mat mtx(img); // convert IplImage* -> Mat
Create() function: 函数
M.create(4,4, CV_8UC(2)); cout << "M = "<< endl << " " << M << endl << endl;
Create() function: 函数
M.create(4,4, CV_8UC(2)); cout << "M = "<< endl << " " << M << endl << endl;
MATLAB形式的初始化方式: zeros(), ones(), :eyes() 。使用以下方式指定尺寸和数据类型:
Mat E = Mat::eye(4, 4, CV_64F); cout << "E = " << endl << " " << E << endl << endl; Mat O = Mat::ones(2, 2, CV_32F); cout << "O = " << endl << " " << O << endl << endl; Mat Z = Mat::zeros(3,3, CV_8UC1); cout << "Z = " << endl << " " << Z << endl << endl;
对于小矩阵你可以用逗号分隔的初始化函数:
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cout << "C = " << endl << " " << C << endl << endl;
使用 clone() 或者 copyTo() 为一个存在的 Mat 对象创建一个新的信息头。
Mat RowClone = C.row(1).clone(); cout << "RowClone = " << endl << " " << RowClone << endl << endl;
Note
调用函数 randu() 来对一个矩阵使用随机数填充,需要指定随机数的上界和下界:
Mat R = Mat(3, 2, CV_8UC3); randu(R, Scalar::all(0), Scalar::all(255));
从上面的例子中可以看到默认格式,除此之外,OpenCV还支持以下的输出习惯
默认方式
cout << "R (default) = " << endl << R << endl << endl;
Python
cout << "R (python) = " << endl << format(R,"python") << endl << endl;
以逗号分隔的数值 (CSV)
cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl;
Numpy
cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl;
C语言
cout << "R (c) = " << endl << format(R,"C" ) << endl << endl;
打印其它常用项目
OpenCV支持使用运算符<<来打印其它常用OpenCV数据结构。
2维点
Point2f P(5, 1); cout << "Point (2D) = " << P << endl << endl;
3维点
Point3f P3f(2, 6, 7); cout << "Point (3D) = " << P3f << endl << endl;
基于cv::Mat的std::vector
vector<float> v; v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f); cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
std::vector点
vector<Point2f> vPoints(20); for (size_t E = 0; E < vPoints.size(); ++E) vPoints[E] = Point2f((float)(E * 5), (float)(E % 7)); cout << "A vector of 2D Points = " << vPoints << endl << endl;
测试代码:
#include "stdafx.h"#include "opencv2\core\core.hpp"#include <iostream>using namespace std;using namespace cv;void help(){ cout<< "\n--------------------------------------------------------------------------" << endl << "This program shows how to create matrices(cv::Mat) in OpenCV and its serial" << endl << "out capabilities" << endl << "That is, cv::Mat M(...); M.create and cout << M. " << endl << "Shows how output can be formated to OpenCV, python, numpy, csv and C styles." << endl << "Usage:" << endl << "./cvout_sample" << endl << "--------------------------------------------------------------------------" << endl << endl;}int _tmain(int argc, _TCHAR* argv[]){ help(); // create by using the constructorMat M1(2,2, CV_8UC1, Scalar(255,200,122)); cout<< "CV8UC1_M1 = " << endl << " " << M1 << endl << endl;Mat M2(2,2, CV_8UC2, Scalar(255,200,122)); cout<< "CV8UC2_M2 = " << endl << " " << M2 << endl << endl;Mat M(2,2, CV_8UC3, Scalar(255,200,122)); cout<< "CV8UC3_M3 = " << endl << " " << M << endl << endl; // create by using the create function() M.create(4,4, CV_8UC(2)); cout << "M = "<< endl << " " << M << endl << endl; // create multidimensional matrices int sz[3] = {2,2,2}; Mat L(3,sz, CV_8UC(1), Scalar::all(0)); // Cannot print via operator << // Create using MATLAB style eye, ones or zero matrix Mat E = Mat::eye(4, 4, CV_64F); cout << "E = " << endl << " " << E << endl << endl; Mat O = Mat::ones(2, 2, CV_32F); cout << "O = " << endl << " " << O << endl << endl; Mat Z = Mat::zeros(3,3, CV_8UC1); cout << "Z = " << endl << " " << Z << endl << endl; // create a 3x3 double-precision identity matrix Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cout << "C = " << endl << " " << C << endl << endl; Mat RowClone = C.row(1).clone(); cout << "RowClone = " << endl << " " << RowClone << endl << endl; // Fill a matrix with random values Mat R = Mat(3, 2, CV_8UC3); randu(R, Scalar::all(0), Scalar::all(255)); // Demonstrate the output formating options cout << "R (default) = " << endl << R << endl << endl; cout << "R (python) = " << endl << format(R,"python") << endl << endl; cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl; cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl; cout << "R (c) = " << endl << format(R,"C" ) << endl << endl; Point2f P(5, 1); cout << "Point (2D) = " << P << endl << endl; Point3f P3f(2, 6, 7); cout << "Point (3D) = " << P3f << endl << endl; vector<float> v; v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f); cout << "Vector of floats via Mat = " << Mat(v) << endl << endl; vector<Point2f> vPoints(20); for (size_t E = 0; E < vPoints.size(); ++E) vPoints[E] = Point2f((float)(E * 5), (float)(E % 7)); cout<< "A vector of 2D Points = " << vPoints << endl << endl; return 0;}
输出结果:
关于Image Engineering & Computer Vision的更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
- 创建Mat 对象
- 创建Mat 对象
- 构造函数方法创建Mat对象
- OpenCV--Mat对象创建及数据类型
- 三、Mat对象的-访问与创建
- 创建MAT
- Opencv显示创建Mat对象的七种方式
- 显示创建Mat对象的七种方法
- Opencv显示创建Mat对象的七种方式
- Opencv显示创建Mat对象的七种方式
- opencv Mat创建注意事项
- opencv Mat创建图像
- OpenCv学习笔记(五)--创建Mat矩阵对象常用的几种方式
- 【OpenCV笔记 04】OpenCV中显式创建Mat对象的5种方法
- Opencv3编程入门学习笔记(二)之显式创建Mat对象
- 像素的存储方法、显式创建Mat对象的七种方法
- OpenCv学习笔记(四)--Mat基本图像容器Mat对象信息头,矩阵体的创建,深复制,浅复制详解
- 三、Mat对象的-属性
- Firebug Lite让前台页面调试更轻松
- poj1316
- C++中结构体与类的区别(struct与class的区别)
- 用Js的eval解析JSON
- Background Execution and Multitasking
- 创建Mat 对象
- SOA技术概况以及其EJB技术实现
- ARM汇编之寄存器
- c#实现记录每个线程的log日志
- 在二元树中找出和为某一值的所有路径
- Combox获取输入值text的方法
- vm虚拟机linux磁盘空间不足,手动扩大(本人操作可行) .
- 机器学习相关数据库
- 类型转换(DWORD)(*m_pStroreMap)[(void*)pOb]