Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

When you are creating a new object for the vector descriptors, you are creating an object with the address of the row variable and not it's actual value. To store the value, if it is this what you want, the code should look like this.

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

I also change the vector<int*> descriptors to vector<int> descriptors.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top