Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>typedef struct elem_ { float f1; float f2; } elem; elem data[9] = { 0.0f }; CvMat mat = cvMat(3, 3, CV_32FC2, data ); float f1 = CV_MAT_ELEM(mat, elem, row, col).f1; float f2 = CV_MAT_ELEM(mat, elem, row, col).f2; CV_MAT_ELEM(mat, elem, row, col).f1 = 1212.0f; CV_MAT_ELEM(mat, elem, row, col).f2 = 326.0f; </code></pre> <p><strong>Update : for OpenCV2.0</strong></p> <h3>1. choose one type to represent the element<br /></h3> <p>Mat (or CvMat) has 3 dimensions: row, col, channel.<br /> We can access one element (or pixel) in the matrix by specifying the row and col.<br /></p> <p><code>CV_32FC2</code> means the element is 32bit floating point value with 2 channels.<br /> So elem in above code is one acceptable representation of <code>CV_32FC2</code>.<br /></p> <p>You can use other representations you like. For example :<br /></p> <pre><code>typedef struct elem_ { float val[2]; } elem; typedef struct elem_ { float x;float y; } elem; </code></pre> <p>OpenCV2.0 adds some new types to represent the element in the matrix,like :<br /></p> <pre><code>template&lt;typename _Tp, int cn&gt; class CV_EXPORTS Vec // cxcore.hpp (208) </code></pre> <p>So we can use <code>Vec&lt;float,2&gt;</code> to represent <code>CV_32FC2</code>, or use :<br /></p> <pre><code>typedef Vec&lt;float, 2&gt; Vec2f; // cxcore.hpp (254) </code></pre> <p>See the source code to get more type that can represent your element.<br /> Here we use <code>Vec2f</code></p> <h3>2. access the element</h3> <p>The easiest and efficiant way to access the element in the Mat class is Mat::at.<br /> It has 4 overloads : <pre><code>template&lt;typename _Tp&gt; _Tp&amp; at(int y, int x); // cxcore.hpp (868) template&lt;typename _Tp&gt; const _Tp&amp; at(int y, int x) const; // cxcore.hpp (870) template&lt;typename _Tp&gt; _Tp&amp; at(Point pt); // cxcore.hpp (869) template&lt;typename _Tp&gt; const _Tp&amp; at(Point pt) const; // cxcore.hpp (871) // defineded in cxmat.hpp (454-468) // we can access the element like this : Mat m( Size(3,3) , CV_32FC2 ); Vec2f&amp; elem = m.at&lt;Vec2f&gt;( row , col ); // or m.at&lt;Vec2f&gt;( Point(col,row) ); elem[0] = 1212.0f; elem[1] = 326.0f; float c1 = m.at&lt;Vec2f&gt;( row , col )[0]; // or m.at&lt;Vec2f&gt;( Point(col,row) ); float c2 = m.at&lt;Vec2f&gt;( row , col )[1]; m.at&lt;Vec2f&gt;( row, col )[0] = 1986.0f; m.at&lt;Vec2f&gt;( row, col )[1] = 326.0f; </code></pre> <h3>3. interact with old interface</h3> <p>Mat provides 2 conversion functions: <br /></p> <pre><code>// converts header to CvMat; no data is copied // cxcore.hpp (829) operator CvMat() const; // defined in cxmat.hpp // converts header to IplImage; no data is copied operator IplImage() const; // we can interact a Mat object with old interface : Mat new_matrix( ... ); CvMat old_matrix = new_matrix; // be careful about its lifetime CV_MAT_ELEM(old_mat, elem, row, col).f1 = 1212.0f; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload