Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a row of cv::Mat to an int

Tags:

c++

opencv

freak

I have a matrix of descriptors from FREAK description extraction where each row is a descriptor of 64 elements.

I need to create a vector <int*> from this matrix due to the system requirements. I tried this so far:

Mat _descriptors;
std::vector<int*> descriptors;
int row;
for (int i=0; i<_descriptors.rows;i++)
{
    row =(int) _descriptors.row(i).data;
    descriptors.push_back( & row );
}

Is this correct or is there a better way for this?

like image 803
Jav_Rock Avatar asked Jan 11 '13 12:01

Jav_Rock


1 Answers

All the values in descriptors will point to the variable row on the stack with this code.

Looking at the definition of a opencv Mat, row returns by value:

// returns a new matrix header for the specified row
Mat row(int y) const;

Accessing the data in _descriptors directly and stepping with provided stride member variable step should work however:

Mat _descriptors;
std::vector<int*> descriptors;
for (int i=0; i<_descriptors.rows;i++)
{
    descriptors.push_back((int*)(_descriptors.data + i * _descriptors.step));
}
like image 152
Andreas Brinck Avatar answered Sep 30 '22 11:09

Andreas Brinck