创建Mat 对象
读取、修改、保存图像 已经讲解了如何使用函数 imwrite() 将一个矩阵写入图像文件中。但是为了debug,更加方便的方式是看实际值。为此,你可以通过 Mat 的运算符 Mat 不但是一个很赞的图像容器类,它同时也是一个通用的矩阵类,所以可以用来创建和操作多维矩阵。创建一个Mat对象有多种方法:矩阵。创建一个Mat对象有多种方法:Mat() 构造函数对于二维多通道图像,首先要定义其尺寸,即行数
教程 读取、修改、保存图像 已经讲解了如何使用函数 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 constructor
Mat 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.
更多推荐
所有评论(0)