c++opencvfreak

Convert a row of cv::Mat to an int


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?


Solution

  • 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));
    }