OpenCV中Mat資料結構使用舉例

2021-07-02 14:23:14 字數 2075 閱讀 9865

mat mtx(img1);//iplimage *-> mat,新的mat型別與原來的iplimage型別共享影象資料,轉換只是建立乙個mat矩陣頭// or : mat mtx = img1;

cvmat oldmat = mtx;//mat-> cvmat //只是建立矩陣頭,而沒有複製資料,oldmat不用手動釋放

cv_assert((oldmat.cols == img1->width) && (oldmat.rows == img1->height) && (oldmat.data.ptr == (uchar *)img1->imagedata) && (oldmat.step == img1->widthstep));

imshow(strwindowname, mtx);

waitkey(0);

cvnamedwindow(strwindowname.c_str(), 0);

cvshowimage(strwindowname.c_str(), &oldmat);

cvwaitkey(0);

iplimage img2 = mtx;//mat->iplimage //只是建立影象頭,而沒有複製資料,img2不用手動釋放

cvshowimage(strwindowname.c_str(), &img2);

cvwaitkey(0);

mat mat3(&oldmat);//cvmat->mat

imshow(strwindowname, mat3);

waitkey(0);

cvdestroywindow(strwindowname.c_str());

cvreleaseimage(&img1);

//建立 3 x 3 雙精度恒等矩陣-----6

mat m3 = (mat_ (3,3) <<1,0,0, 0,1,0, 0,0,1);

//訪問陣列元素-----7

m2.at(0, 0) += 10.f;

double sum = 0;//計算元素和,方法一

for (int i=0; iconst

double *mi = m2.ptr(i) ;

for (int j=0; jstd::max(mi[j], 0.);}}

cout

<0;//計算元素和,方法二

int cols =m2.cols, rows = m2.rows ;

if (m2.iscontinuous())

for (int i=0; iconst

double *mi = m2.ptr (i);

for (int j=0; jstd::max(mi[j], 0.);}}

cout

<0;//計算元素和,方法三

matconstiterator_ it = m2.begin(), it_end = m2.end();

for(; it != it_end; ++it)

cout

0;}參考文獻:

1、 2、

3、 4、

5、 6、

7、 8、

OpenCV中Mat資料結構使用舉例

mat mtx img1 iplimage mat,新的mat型別與原來的iplimage型別共享影象資料,轉換只是建立乙個mat矩陣頭 or mat mtx img1 cvmat oldmat mtx mat cvmat 只是建立矩陣頭,而沒有複製資料,oldmat不用手動釋放 cv assert...

再談OpenCV資料結構Mat詳解

我記得開始接觸opencv就是因為乙個演算法裡面需要2維動態陣列,那時候看core這部分也算是走馬觀花吧,隨著使用的增多,對mat這個結構越來越喜愛,也覺得有必要溫故而知新,於是這次再看看mat。mat最大的優勢跟stl很相似,都是對記憶體進行動態的管理,不需要之前使用者手動的管理記憶體,對於一些大...

Opencv中資料結構Mat的相關屬性

the class mat represents an n dimensional dense numerical single channel or multi channel array.it can be used to store real or complex valued vectors...