OpenCV Tutorials —— Mat

if you pass on an already existing Mat object, which has already allocated the required space for the matrix, this will be reused.

The idea is that each Mat object has its own header, however the matrix may be shared between two instance of them by having their matrix pointers point to the same address. Moreover, the copy operators will only copy the headers and the pointer to the large matrix, not the data itself.

出于效率考虑,复制Mat对象只是赋值Mat 头指针 —— 通过引用计数器来实现 —— 最后使用的对象负责清空Mat内存

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

ABC 指向同一块内存地址,只是各个Mat头不同,所以对其中一个进行更改也会在在其他对象中显现

可以使用Mat头指向图像中的一块子区域,ROI

create a region of interest (ROI) in an image you just create a new header with the new boundaries:

Mat D(A,Rect(10,10,100,100));// using a rectangle

Mat E= A(Range::all(),Range(1,3)); // using row and column boundaries

Creating a Mat object explicity

for debugging purposes it’s much more convenient to see the actual values. You can do this using the << operator of Mat. Be aware that this only works for two dimensional matrices. —— 注意,只适用于二维矩阵

Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;

The Scalar is four element short vector. Specify this and you can initialize all matrix points with a custom value.

 int sz[3] = {2,2,2};
 Mat L(3,sz, CV_8UC(1), Scalar::all(0));
使用C++数组来创建 一个多维数组
Specify its dimension, then pass a pointer containing the size for each dimension and the rest remains the same.
 
Create a header for an already existing IplImage pointer:
IplImage* img = cvLoadImage("greatwave.png", 1);
Mat mtx(img); // convert IplImage* –> Mat
 
MATLAB style initializer: —— Matlab Style ,太残暴了
 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;
 
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));
 
输出格式

In the above examples you could see the default formatting option. OpenCV, however, allows you to format your matrix output:

  • Default

        cout << "R (default) = " << endl <<        R           << endl << endl;
    

  • Python
        cout << "R (python)  = " << endl << format(R,"python") << endl << endl;
    

  • Comma separated values (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;
    

时间: 2024-08-01 10:44:14

OpenCV Tutorials —— Mat的相关文章

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析

欢迎转载,尊重原创,所以转载请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种访问矩阵元素的方法,在指定平台上给出性能比较,分析每种矩阵元素访问方法的代码复杂度,易用性. 一.预备设置 本文假设你已经正确配置了OpenCV的环境,为方便大家实验,在文中也给出了编译源程序的Makefile,其内容如代码段1所示. 采用如代码段2所示的计时函数,这段代码你可以在我之前的博文中找到,abtic()

OpenCV访问Mat对象中数据时发生异常---Mat中的数据访问

7.1和7.1.1由于越狱不成熟,半完美越狱后电脑上无法访问系统越狱目录,如var usr 等等. 今天有些意外地发现,可以在电脑上使用手机的越狱目录我手机 i4 7.1.1 联通 半完美越狱,没装Afc2Add,也没装Appsync 附上  --->我的半完美越狱过程 好了,下面直接正题 一.前提,必须安装ifile! 打开ifile,并转到 /var/mobile/media 目录下,然后点击右上角的 [ 编辑 ]如图: 二.点左下角的 + 号创建,如图: 三.点 [ 类型],选择[符号链接

OpenCV:Mat,IplImage,CvMat类型转换

Mat,cvMat和IplImage这三种类型都可以代表和显示图像,三者区别如下 Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化. 而CvMat和IplImage类型更侧重于“图像”,opencv对其中的图像操作(缩放.单通道提取.图像阈值操作等)进行了优化.IplImage类型与CvMat类型的关系类似于面向对象中的继承关系.实际上,CvMat之上还有一个更抽象的基类----CvArr,这在源代码中会常见.在opencv2.0之前,opencv是完全用C实现的.

使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出

在使用OpenCV开发程序时,如果想查看矩阵数据,比较费劲,而matlab查看数据很方便,有一种方法,是matlab和c++混合编程,可以用matlab访问c++的内存,可惜我不会这种方式,所以我就把数据写到文件里,用matlab读出来,然后用matlab各种高级功能查看数据的值. 1.将Mat的数据写入指定文件 为了方便拿来主义者,我直接把这个函数贴出来,你只要把代码拷贝到自己的代码里,就可以直接用了.如果有问题,赶紧评论,我会尽快看看问题出在哪里. #include <iostream>

OpenCV Tutorials &mdash;&mdash; Interoperability with OpenCV 1

新版本的OpenCV 使用Mat作为基本的图像容器,而代替旧版本的 CvMat 和 IplImage All the OpenCV related stuff is put into the cv namespace to avoid name conflicts with other libraries data structures and functions. Therefore, either you need to prepend the cv:: keyword before eve

OpenCV中Mat,图像二维指针和CxImage类的转换

在做图像处理中,常用的函数接口有OpenCV中的Mat图像类,有时候需要直接用二维指针开辟内存直接存储图像数据,有时候需要用到CxImage类存储图像.本文主要是总结下这三类存储方式之间的图像数据的转换和相应的对应关系. 一.OpenCV的Mat类到图像二值指针的转换 以下为函数代码: unsigned char** MatTopImgData(Mat img) { //获取图像参数 int row = img.rows; int col = img.cols; int band = img.c

opencv数据结构-MAT结构详解

1.定义 OpenCV中的C结构体有 CvMat 和 CvMatND,但后续的应用中指出 CvMat 和 CvMatND 弃用了,在C++封装中用 Mat 代替,另外旧版还有一个 IplImage,同样用 Mat 代替(可以参考博文 OpenCV中的结构体.类与Emgu.CV的对应表).矩阵 (M) 中数据元素的地址计算公式:addr(Mi0,i1,-im-1) = M.data + M.step[0] * i0 + M.step[1] * i1 + - + M.step[m-1] * im-1

OpenCV Tutorials &mdash;&mdash; Scan images

color space reduction divide the color space current value with a new input value to end up with fewer colors. For instance every value between zero and nine takes the new value zero, every value between ten and nineteen the value ten and so on. 减少颜色