2017-10-13 71 views
0

在許多教程e.g. this one中,顯示瞭如何用逗號分隔列表初始化一個opencv Mat。但是,當我嘗試用多維Mat來做這件事時,我感到很奇怪。多維cv的逗號分隔初始值設定:: Mat

#include "opencv2/core/core.hpp" 
#include <iostream> 

int main() { 
    cv::Mat vect = (cv::Mat_<double>(2, 2, CV_8UC3) << 1,2,3,4,5,6,7,8,9,10,11,12); 
    std::cout << "vect = " << std::endl << " " << cv::format(vect,"python") << std::endl; 
    return 12345; 
} 

輸出:

vect = 
[[1, 2], 
    [3, 4]] 

可以在一個明確初始化多維墊?

編輯:此外,我在其他方式初始化時遇到麻煩。

int main() { 
    int data[2][2][3] = { 
     { 
      {1,2,3}, 
      {4,5,6} 
     }, 
     { 
      {7,8,9}, 
      {10,11,12} 
     } 
    }; 
    cv::Mat vect = cv::Mat(2, 2, CV_8UC3, data); 
    std::cout << "vect = " << std::endl << " " << cv::format(vect,"python") << std::endl; 
    return 54321; 
} 

輸出:

vect = 
[[[1, 0, 0], [0, 2, 0]], 
    [[0, 0, 3], [0, 0, 0]]] 

因此,在[0][0][1]在我的輸入數組元素在墊[0][1][1]結束?這裏發生了什麼......

回答

1

對於模板Mat_沒有重載功能,需要Mat_(int rows, int cols, int type),source here

cv::Mat vect = (cv::Mat_<double>(3,4) << 1,2,3,4,5,6,7,8,9,10,11,12); 
std::cout << "vect = " << std::endl << " " <<cv::format(vect,Formatter::FMT_PYTHON) << std::endl; 

輸出:

vect = 
[[1, 2, 3, 4], 
[5, 6, 7, 8], 
[9, 10, 11, 12]] 

對於非模板墊你不必給多維數組數據指針參數,所述Mat::data可以是連續的一維數據的指針。 Mat構造函數將負責處理參數中提供的通道,行和列。

uchar data[] = {1,2,3,4,5,6,7,8,9,10,11,12}; 
Mat vect(2,2,CV_8UC3,data); 
std::cout << "vect = " << std::endl << " " << cv::format(vect,Formatter::FMT_PYTHON) << std::endl; 

輸出:

vect = 
[[[ 1, 2, 3], [ 4, 5, 6]], 
[[ 7, 8, 9], [ 10, 11, 12]]]