1. 程式人生 > >有關Mat通道的資料結構的發現

有關Mat通道的資料結構的發現

OpenCV Mat Channel的地址問題

最近因為做數字影象處理大實驗的原因學習了一下warpPerspective函式的原始碼,在閱讀原始碼時碰到了一些困難,其中一個就是關於Mat不同通道的資料是如何組織的。於是我做了一個小實驗,下面是實驗的程式碼:

#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>

using
namespace std; using namespace cv; int main() { int M[18]; for (int i = 0; i < 18; i++) { M[i] = i; } Mat matA(3, 3, CV_32SC2, M);// matA中資料的地址即是M中資料的地址,改變M中的資料就會改變matA中的資料 cout << "channel1: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout <<
matA.at<Vec2i>(i, j)[0] << ' '; } cout << endl; } cout << "channel2: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << matA.at<Vec2i>(i, j)[1] << ' '; } cout << endl; } cout << "test3: "
<< endl; for (int i = 0; i < 9; i++) { *(M + i) += 100; } cout << "channel1_after: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << matA.at<Vec2i>(i, j)[0] << ' '; } cout << endl; } cout << "channel2_after: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << matA.at<Vec2i>(i, j)[1] << ' '; } cout << endl; } return 0; }

程式碼的輸出結果是:
在這裡插入圖片描述初始化一個數組M[18]使他的值依次為0~17,再用M初始化一個row=3,col=3,channel=2的矩陣,輸出矩陣每一個通道里儲存的9個數,結果一通道為{0,2,4,6,8,10,12,14,16},二通道為{1,3,5,7,9,11,13,15,17},把陣列M前9個數每個加100,再列印矩陣的每個通道的值,發現通道一為{100,102,104,106,108,10,12,14,16},二通道為{101,103,105,107,9,11,13,15,17}
這說明Mat內部對待通道的儲存方式是這樣的
在這裡插入圖片描述
而不是這樣的
在這裡插入圖片描述