OpenCV.Man.Note (1)
来源:互联网 发布:mysql limit实现 编辑:程序博客网 时间:2024/04/27 23:30
Mat - The Basic Image Container
(1)Mat简介:
The matrix header size is constant.(图像头信息大小是固定的)
Each Mat object has its own header, the copy operators will only copy the headers and the pointer to the large matrix, not the data
itself.(copy算子仅仅复制matrix header,并不复制matrix数据)
Mat A, C; // creates just the header parts
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we’ll know the method used (allocate matrix)
Mat B(A); // Use the copy constructor
C = A; // Assignment operator
All the above objects, in the end, point to the same single data matrix. (上述程序中Mat对象A, B, C均指向同一块matrix数据)
Their headers are different, however, and making a modification using any of them will affect all the other ones as well.
The real interesting part is that you can create headers which refer to only a subsection of the full data. For example, to create
a region of interest (ROI) in an image you just create a new header with the new boundaries:(可以创建不同的matrix header,来指向图像matrix数据的某一部分,比如ROI区域)
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
Sometimes you will want to copy the matrix itself too, so OpenCV provides the clone() and copyTo() functions.(需要复制matrix数据的时候,可以用clone和copyTo方法来实现,以下程序中,Mat对象F和G就复制了A的matrix数据)
Mat F = A.clone();
Mat G;
A.copyTo(G);
Summary:
• Output image allocation for OpenCV functions is automatic (unless specified otherwise).
• You do not need to think about memory management with OpenCVs C++ interface.
• The assignment operator and the copy constructor only copies the header.
• The underlying matrix of an image may be copied using the clone() and copyTo() functions.
(2)Mat对象的初始化:
create and manipulate multidimensional matrices. You can create a Mat object in multiple ways:
• Mat() Constructor
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;
• Use C/C++ arrays and initialize via constructor
int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC(1), Scalar::all(0));
• Create a header for an already existing IplImage pointer:
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;
• MATLAB style initializer: zeros(), ones(), eye(). Specify size and data type to use:
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;
• For small matrices you may use comma separated initializers:
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl << endl;
• Create a new header for an existing Mat object and clone() or copyTo() it.
Mat RowClone = C.row(1).clone();
cout << "RowClone = " << endl << " " << RowClone << endl << endl;
NOTE: You can fill out a matrix with random values using the randu() function. You need to give the lower and upper
value for the random values:
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));
(3)Output of other common items
OpenCV offers support for output of other common OpenCV data structures too via the << operator:
• 2D Point
Point2f P(5, 1);
cout << "Point (2D) = " << P << endl << endl;
• 3D Point
Point3f P3f(2, 6, 7);
cout << "Point (3D) = " << P3f << endl << endl;
• std::vector via cv::Mat
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 of points
vector<Point2f> vPoints(20);
for (size_t i = 0; i < vPoints.size(); ++i)
vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
cout << "A vector of 2D Points = " << vPoints << endl << endl;
#include <opencv.hpp>#include <iostream>#include <vector>using namespace cv;using namespace std;int main(){////////////////////////////////////////////////////////Mat - The Basic Image Container/////////////////////////////////////////////////////////*Mat is basically a class with two data parts : (1) the matrix header(2) a pointer to the matrix.*/Mat A, C;// creates just the header partsA = imread("lena512.bmp", CV_LOAD_IMAGE_COLOR);// here we’ll know the method used (allocate matrix)imshow("Image A", A);/*1. the copy operators will only copy the headers and the pointer to the large matrix. NOTnot the matrix data itself */Mat B(A);// Use the copy constructorC = A;// Assignment operator/*2. create a region of interest(ROI) in an image. */Mat D(A, Rect(10, 10, 100, 100));// using a rectangleimshow("Image D", D);Mat E = A(Range::all(), Range(0, 256));// using row and column boundariesimshow("Image E", E);/*3. copy the matrix itself, OpenCV provides the clone() and copyTo() functions. */Mat F = A.clone();Mat G;A.copyTo(G);////////////////////////////////////////////////////////////////Creating a Mat object explicitly//////////////////////////////////////////////////////////////////1. Mat() ConstructorMat M(2, 2, CV_8UC3, Scalar(0, 0, 255));cout << " M = " << endl << " " << M << endl << endl;//2.Use C / C++ arrays and initialize via constructorint sz[3] = { 2, 2, 2 };Mat L(3, sz, CV_8UC(1), Scalar::all(0));//3.Create a header for an already existing IplImage pointer :IplImage* img = cvLoadImage("baboon.jgp", 1);Mat mtx(img); // convert IplImage* -> Mat//4.Create() function:M.create(4, 4, CV_8UC(2));cout << " M = " << endl << " " << M << endl << endl;//5.MATLAB style initializer : zeros(), ones(), eye().Specify size and data type to use :Mat E1 = Mat::eye(4, 4, CV_64F);cout << " E1 = " << endl << " " << E1 << 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;//6.For small matrices you may use comma separated initializers :Mat C1 = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);cout << " C1 = " << endl << " " << C1 << endl << endl;//7.Create a new header for an existing Mat object and clone() or copyTo() it.Mat RowClone = C1.row(1).clone();cout << " RowClone = " << endl << " " << RowClone << endl << endl;//8. NOTE:You can fill out a matrix with random values using the randu() function.//You need to give the lower and upper value for the random values :Mat R = Mat(3, 2, CV_8UC3);randu(R, Scalar::all(0), Scalar::all(255));cout << " R = " << endl << " " << R << endl << endl;////////////////////////////////////////////////////////////////Output of other common items////////////////////////////////////////////////////////////////// OpenCV offers support for output of other common OpenCV data structures too via the << operator://1. 2D PointPoint2f P(5, 1);cout << " Point (2D) = " << P << endl << endl;//2. 3D PointPoint3f P3f(2, 6, 7);cout << " Point (3D) = " << P3f << endl << endl;//3. std::vector via cv::Matvector<float> v;v.push_back((float)CV_PI); v.push_back(2); v.push_back(3.01f);cout << " Vector of floats via Mat = " << endl << Mat(v) << endl << endl;//4. std::vector of pointsvector<Point2f> vPoints(10);for (vector<Point2f>::size_type i = 0; i < vPoints.size(); ++i)vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));cout << " A vector of 2D Points = " << endl << vPoints << endl << endl;waitKey(0);return 0;}
本想看看opencv的tutorial做点笔记,后来看了下《OpenCV3编程入门》这书基本就是对tutorial的翻译,倒也省点事了。
0 0
- OpenCV.Man.Note (1)
- Opencv-ref-man-2.3.1学习笔记(精简版)
- opencv mjpg note
- note:linux opencv
- NOTE 1
- NOTE-1
- note 1
- note 1
- opencv contrib-master ximgproc module doc note
- opencv contrib-master cnn_3dobj module doc note
- be a man(1)
- linux系统管理(1)---man
- man(1)命令
- man
- man
- man
- man
- man
- 今天双十一了,你有什么话想说
- Hillary Clinton's Concession Speech
- 记事本在读取一个文本时如何判断是该用啥字符集来读取解析文本字符
- 23种设计模式_装饰模式
- stack_link
- OpenCV.Man.Note (1)
- scala的协变逆变,上界下界
- 淘宝maven镜像库是个好东西
- 今天发现python windows运行正常,linux下异常
- HDU2001_计算两点间的距离
- 我的前端“先行”之路
- ios 结构体及坐标系统
- linux高并发的socket的最大接入数
- webpack+ES6+react搭建简单开发环境