How do I convert an cv::Mat to float pointer?

asked 2016-07-21 02:06:21 -0600

lovaj gravatar image

I want to represent the matrix of cv::SIFT descriptors cv::Mat descriptors as float*. This is because I want to use GMM from VLFeat (if you any better solution, please let me know). This is the code that I came up with:

if(!prova.isContinous()){
    std::err<<"Descriptor matrix not continuous!"<<std::endl;
    return 1;
}
float *data = new float[desc.total()];
std::memcpy(data,prova.data,prova.total() * sizeof(float));

Trying this code with a toy matrix:

cv::Mat prova = (cv::Mat_<float>(3,3) << 0.1, 0.3, 2.1, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8);

And print the values:

for(int i=0;i<prova.total();i++)
    std::cout<<*(data+i)<<std::endl;

Works fine, but since we're dealing with pointers (and it's always a dangerous matter) I would like an opinion.

NOTE: just for giving a context, this is how I'm going to use data:

VlGMM* gmm = vl_gmm_new(VL_TYPE_FLOAT, desc.cols, 2) ;
vl_gmm_cluster (gmm, data, desc.rows);
edit retag flag offensive close merge delete

Comments

1

as long, as your 'prova' Mat stays in scope: float *fptr = prova.ptr<float>(0); (and checking for isContinuous() is a good idea already !)

berak gravatar imageberak ( 2016-07-21 02:45:08 -0600 )edit