【OpenCV】關於Mat_類的一個問題,opencvmat

來源:互聯網
上載者:User

【OpenCV】關於Mat_類的一個問題,opencvmat

* 未經許可,謝絕轉載!請尊重原創! 【OpenCV】關於Mat_類的一個問題 *
對於Mat_類,就從文檔裡面複製點內容做個簡單的介紹吧。
The class Mat_<Tp> is a “thin” template wrapper on top of the Mat class. It does not have any extra data fields.While Mat is sufficient in most cases, Mat can be more convenient if you use a lot of element access operations and if you know matrix type at the compilation time.
這個類其實就是Mat的一個子類,平時我自己主要用來操作當資料已知且量較小時的輸入,見下面的例子。
這次在看以前寫的代碼的時候,不經意間發現了下面代碼中要展示的問題,這也是本文要說的。

#include <iostream>#include <opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    // 方式一    Mat matrix1 = (Mat_<int>(2, 3) << 0, 1, 2,                                      3, 4, 5);    cout << matrix1 << endl << endl;    // 方式二    Mat matrix2 = *(Mat_<int>(2, 3) << 0, 1, 2,                                       3, 4, 5);    cout << matrix2 << endl;    return 0;}

在My Code中幾乎都是用的第一種方式,除了有一行代碼用的是第二種方式。其實我也沒弄明白當時咋個就加了個*。
上面的兩種方式產生的結果完全相同。大家可以自己試一下結果。
為什麼會產生上面的問題?根本原因就是操作符重載了。下面直接從源碼中給出解釋。

// 下面的代碼源自OpenCV2.4.9// 上面例子中的操作基本都是由MatCommaInitializer_類來完成的template<typename _Tp> class MatCommaInitializer_{public:    //! the constructor, created by "matrix << firstValue" operator, where matrix is cv::Mat    MatCommaInitializer_(Mat_<_Tp>* _m);    //! the operator that takes the next value and put it to the matrix    template<typename T2> MatCommaInitializer_<_Tp>& operator , (T2 v);    //! another form of conversion operator    Mat_<_Tp> operator *() const;    operator Mat_<_Tp>() const;protected:    MatIterator_<_Tp> it;};// 對應例子中的方式一template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const{    CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );    return Mat_<_Tp>(*this->it.m);}// 對應例子中的方式二template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const{    CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );    return Mat_<_Tp>(*this->it.m);}

從上面的代碼可以看出,這兩種重載其實做了完全一模一樣的事情。
相信絕大部分人在用到Mat_這個類的時候都會使用第一種方式,既然偶然的機會讓我碰到了這個問題,還是在此做個記錄,希望可以幫到大家。
對上第二種方式,在OpenCV3.0.0中已經不複存在了,還是看源碼吧。

// 下面的代碼源自OpenCV3.0.0// MatCommaInitializer_template<typename _Tp> class MatCommaInitializer_{public:    //! the constructor, created by "matrix << firstValue" operator, where matrix is cv::Mat    MatCommaInitializer_(Mat_<_Tp>* _m);    //! the operator that takes the next value and put it to the matrix    template<typename T2> MatCommaInitializer_<_Tp>& operator , (T2 v);    //! another form of conversion operator    operator Mat_<_Tp>() const;protected:    MatIterator_<_Tp> it;};// 對應例子中的第一種方式,第二種方式已經被刪除template<typename _Tp> inlineMatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const{    CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );    return Mat_<_Tp>(*this->it.m);}

從上面OpenCV3.0.0中的源碼可以看出,對*的重載已經被刪除了。
* 未經許可,謝絕轉載!請尊重原創! 【OpenCV】關於Mat_類的一個問題 *

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.