penelope
penelope

Reputation: 8418

Converting a row of cv::Mat to std::vector

I have a fairly simple question: how to take one row of cv::Mat and get all the data in std::vector? The cv::Mat contains doubles (it can be any simple datatype for the purpose of the question).

Going through OpenCV documentation is just very confusing, unless I bookmark the page I can not find a documentation page twice by Googling, there's just to much of it and not easy to navigate.

I have found the cv::Mat::at(..) to access the Matrix element, but I remember from C OpenCV that there were at least 3 different ways to access elements, all of them used for different purposes... Can't remember what was used for which :/

So, while copying the Matrix element-by-element will surely work, I am looking for a way that is more efficient and, if possible, a bit more elegant than a for loop for each row.

Upvotes: 10

Views: 31670

Answers (5)

user3102903
user3102903

Reputation: 33

I think this works,

an example :

Mat Input(480, 720, CV_64F, Scalar(100));

cropping the 1st row of the matrix:

Rect roi(Point(0, 0), Size(720, 1));

then:

std::vector<std::vector<double> > vector_of_rows;

vector_of_rows.push_back(Input(roi));

Upvotes: 0

smttsp
smttsp

Reputation: 4191

You can also use cv::Rect

m(cv::Rect(0, 0, 1, m.cols)) 

will give you first row.

matrix(cv::Rect(x0, y0, len_x, len_y);

means that you will get sub_matrix from matrix whose upper left corner is (x0,y0) and size is (len_x, len_y). (row,col)

Upvotes: 2

Andrey Kamaev
Andrey Kamaev

Reputation: 30122

It should be as simple as:

m.row(row_idx).copyTo(v);

Where m is cv::Mat having CV_64F depth and v is std::vector<double>

Upvotes: 34

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67147

Data in OpenCV matrices is laid out in row-major order, so that each row is guaranteed to be contiguous. That means that you can interpret the data in a row as a plain C array. The following example comes directly from the documentation:

// compute sum of positive matrix elements
// (assuming that M is double-precision matrix)
double sum=0;
for(int i = 0; i < M.rows; i++)
{
    const double* Mi = M.ptr<double>(i);
    for(int j = 0; j < M.cols; j++)
        sum += std::max(Mi[j], 0.);
}

Therefore the most efficient way is to pass the plain pointer to std::vector:

// Pointer to the i-th row
const double* p = mat.ptr<double>(i);

// Copy data to a vector.  Note that (p + mat.cols) points to the
// end of the row.
std::vector<double> vec(p, p + mat.cols);

This is certainly faster than using the iterators returned by begin() and end(), since those involve extra computation to support gaps between rows.

Upvotes: 17

Xeo
Xeo

Reputation: 131799

From the documentation at here, you can get a specific row through cv::Mat::row, which will return a new cv::Mat, over which you can iterator with cv::Mat::begin and cv::Mat::end. As such, the following should work:

cv::Mat m/*= initialize */;
// ... do whatever...
cv::Mat first_row(m.row(0));
std::vector<double> v(first_row.begin<double>(), first_row.end<double>());

Note that I don't know any OpenCV, but googling "OpenCV mat" led directly to the basic types documentation and according to that, this should work fine.

The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, including std::sort() .

This is also from the documentiation, so you could actually do this without a copy:

cv::Mat m/*= initialize */;
// ... do whatever...
// first row          begin              end
std::vector<double> v(m.begin<double>(), m.begin<double>() + m.size().width);

To access more than the first row, I'd recommend the first snippet, since it will be a lot cleaner that way and there doesn't seem to be any heavy copying since the data types seem to be reference-counted.

Upvotes: 6

Related Questions