歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV基礎篇之Mat數據結構

OpenCV基礎篇之Mat數據結構

日期:2017/3/1 9:41:18   编辑:Linux編程

OpenCV基礎篇之Mat數據結構

程序及分析

/*
* FileName : MatObj.cpp
* Author : xiahouzuoxin @163.com
* Version : v1.0
* Date : Thu 15 May 2014 09:12:45 PM CST
* Brief :
*
* Copyright (C) MICL,USTB
*/
#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

int main(void)
{
/*
* Create Mat
*/
Mat M(2,2,CV_8UC3, Scalar(0,0,255));
cout << "M=" << endl << " " << M << endl << endl;

/*
* Matlab style
*/
Mat E = Mat::eye(4,4,CV_64F);
cout << "E=" << endl << " " << E << endl << endl;
E = Mat::ones(4,4,CV_64F);
cout << "E=" << endl << " " << E << endl << endl;
E = Mat::zeros(4,4,CV_64F);
cout << "E=" << endl << " " << E << endl << endl;


/*
* Convert IplImage to Mat
*/
IplImage *img = cvLoadImage("../test_imgs/Lena.jpg");
Mat L(img);
namedWindow("Lena.jpg", CV_WINDOW_AUTOSIZE);
imshow("Lena.jpg", L);
waitKey(0);

/*
* Init Mat with separated data
*/
Mat C = (Mat_<int>(3,3) << 0,1,2,3,4,5,6,7,8);
cout << "C=" << endl << " " << C << endl << endl;


return 0;
}
1.
Mat是OpenCV最基本的數據結構,Mat即矩陣(Matrix)的縮寫,Mat數據結構主要包含2部分:Header和Pointer。Header中主要包含矩陣的大小,存儲方式,存儲地址等信息;Pointer中存儲指向像素值的指針。我們在讀取圖片的時候就是將圖片定義為Mat類型,其重載的構造函數一大堆,
class CV_EXPORTS Mat
{
public:
//! default constructor
Mat();
//! constructs 2D matrix of the specified size and type
// (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
Mat(int _rows, int _cols, int _type);
Mat(Size _size, int _type);
//! constucts 2D matrix and fills it with the specified value _s.
Mat(int _rows, int _cols, int _type, const Scalar& _s);
Mat(Size _size, int _type, const Scalar& _s);

//! constructs n-dimensional matrix
Mat(int _ndims, const int* _sizes, int _type);
Mat(int _ndims, const int* _sizes, int _type, const Scalar& _s);

//! copy constructor
Mat(const Mat& m);
//! constructor for matrix headers pointing to user-allocated data
Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);
Mat(int _ndims, const int* _sizes, int _type, void* _data, const size_t* _steps=0);

//! creates a matrix header for a part of the bigger matrix
Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
Mat(const Mat& m, const Rect& roi);
Mat(const Mat& m, const Range* ranges);
//! converts old-style CvMat to the new matrix; the data is not copied by default
Mat(const CvMat* m, bool copyData=false);
//! converts old-style CvMatND to the new matrix; the data is not copied by default
Mat(const CvMatND* m, bool copyData=false);
//! converts old-style IplImage to the new matrix; the data is not copied by default
Mat(const IplImage* img, bool copyData=false);

......
}

要了解如何初始化Mat結構,就應該了解它的構造函數,比如程序中的第一初始化方式調用額就是
Mat(int _rows, int _cols, int _type, const Scalar& _s);

這個構造函數。

IplImage*是C語言操作OpenCV的數據結構,在當時C操縱OpenCV的時候,地位等同於Mat,OpenCV為其提供了一個接口,很方便的直接將IplImage轉化為Mat,即使用構造函數
Mat(const IplImage* img, bool copyData=false);

上面程序中的第二種方法就是使用的這個構造函數。

--------------------------------------分割線 --------------------------------------

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm

--------------------------------------分割線 --------------------------------------

2.
關於Mat數據復制:前面說過Mat包括頭和數據指針,當使用Mat的構造函數初始化的時候,會將頭和數據指針復制(注意:只是指針復制,指針指向的地址不會復制),若要將數據也復制,則必須使用copyTo或clone函數

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-08/105085p2.htm

Copyright © Linux教程網 All Rights Reserved