2016-08-18 84 views
1

documentation定義了一個cv::Mat括號操作員在cv::Rect類型感興趣的矩形區域作爲一個參數:cv :: Mat括號運算符是否複製或引用感興趣的區域?

Mat cv::Mat::operator() (const Rect & roi) const

但文檔不解釋操作的語義。它是否複製感興趣的區域?或者它是否在創建的新Mat中引用它?

如果我改變原來的墊,新的也會改變嗎?

我的猜測是它複製,因爲在大多數情況下,roi不是連續的內存塊。但文檔沒有明確說明,所以我只是想確保它不會牽扯到最終耦合兩個Mats的特殊內存技巧。

回答

2

這是一個參考,除非您明確複製數據。


爲此,可以使用一個小例子看看:

#include <opencv2/opencv.hpp> 
#include <iostream> 
using namespace cv; 
using namespace std; 

int main() 
{ 
    Rect r(0,0,2,2); 
    Mat1b mat(3,3, uchar(0)); 
    cout << mat << endl; 

    // mat 
    // 0 0 0 
    // 0 0 0 
    // 0 0 0 

    Mat1b submat_reference1(mat(r)); 
    submat_reference1(0,0) = 1; 
    cout << mat << endl; 
    cout << submat_reference1 << endl; 

    // mat 
    // 1 0 0 
    // 0 0 0 
    // 0 0 0 

    // submat_reference1 
    // 1 0 
    // 0 0 

    Mat1b submat_reference2 = mat(r); 
    submat_reference2(0, 0) = 2; 
    cout << mat << endl; 
    cout << submat_reference1 << endl; 
    cout << submat_reference2<< endl; 
    // mat 
    // 2 0 0 
    // 0 0 0 
    // 0 0 0 

    // submat_reference1 = submat_reference2 
    // 2 0 
    // 0 0 


    Mat1b submat_deepcopy = mat(r).clone(); 
    submat_deepcopy(0,0) = 3; 
    cout << mat << endl; 
    cout << submat_reference1 << endl; 
    cout << submat_reference2 << endl; 
    cout << submat_deepcopy << endl; 

    // mat  
    // 2 0 0 
    // 0 0 0 
    // 0 0 0 

    // submat_reference1 = submat_reference2 
    // 2 0 
    // 0 0 

    // submat_deepcopy 
    // 3 0 
    // 0 0 

    return 0; 
} 
相關問題