日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > OpenCV基礎篇之Mat數據結構

OpenCV基礎篇之Mat數據結構

來源:程序員人生   發布時間:2014-10-19 08:00:00 閱讀次數:2939次

程序及分析

/* * 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);

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

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

    mat

  3. Mat還有幾個常用的成員函數,在之后的文章中將會使用到:

    //! returns true iff the matrix data is continuous // (i.e. when there are no gaps between successive rows). // similar to CV_IS_MAT_CONT(cvmat->type) bool isContinuous() const;

    這了解上面的函數作用前,得了解下OpenCV中存儲像素的方法,如下,灰度圖(單通道)存儲按行列存儲,

    single_channel

    三通道RGB存儲方式如下,每列含有三個通道,

    rgb_channel

    為了加快訪問的速度,openCV往往會在內存中將像素數據連續地存儲成一行,isContinus()函數的作用就是用于判斷是否連續存儲成一行。存儲成一行有什么好處呢?給定這行的頭指針p,則只要使用p++操作就能逐個訪問數據。

    因此當判斷存放在一行的時候,可以通過數據指針++很容易遍歷圖像像素:

    long nRows = M.rows * M.channels(); // channels()也是Mat中一個常用的函數,用于獲取通道數(RGB=3,灰度=1) long nCols = M.cols; uchar *p = M.data; // 數據指針 if(M.isContinuous()) { nCols *= nRows; for (long i=0; i < nCols; i++) { *p++ = ...; // 像素賦值或讀取操作 } }

    請注意以上幾個常用的Mat成員遍歷和函數:

    M.row; // 返回圖像行數 M.nCols; // 返回圖像列數 M.channels(); //返回通道數 M.isContinuous(); // 返回bool類型表示是否連續存儲
  4. 更多關于Mat的信息請參考安裝目錄下的include/opencv2/core.hpp文件

效果

result

左邊是矩陣的一些操作輸出結果,右邊的圖是通過IplImage *結構讀入,轉換為Mat后顯示結果。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 中文字幕精品久久久久 | 最新国产精品精品视频 | 天天干天天草 | 可以在线看的av | 久久久久久久久国产 | 欧美成人在线免费 | 91久久国产综合久久蜜月精品 | 亚洲第一视频网站 | 国产精品精品视频一区二区三区 | 国产午夜精品一区二区三区嫩草 | 中文字幕免费在线观看 | 色婷婷色丁香 | 亚洲精品18 | 亚洲免费在线观看视频 | 日韩小视频 | 91亚洲国产 | 国产一区二区三区 在线 | 亚洲欧美日韩电影 | 亚洲精品一区二区三区不 | 黄色片在线播放 | 久久国产成人精品av | 日韩av中文字幕在线 | 黄色小视频在线看 | 日韩欧美综合在线视频 | 91亚洲精品乱码久久久久久蜜桃 | 精品久久久久久亚洲精品 | a级毛片免费全部播放 | 精品一区在线 | 成年人视频免费在线观看 | 亚洲视频二| 天天操夜夜草 | 91夜夜蜜桃臀一区二区三区 | 亚洲成人精品一区二区 | 51ⅴ精品国产91久久久久久 | 精品一区二区三区成人精品 | 日本综合在线 | 极品视频在线 | 国产日韩在线视频 | 日韩小视频 | 欧美精品国产一区二区 | 国产精品一区二区久久久 |