简体   繁体   中英

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

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 have 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;

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

You can use the Mat::ptr method to get the float pointer to the beginning of any row. Specifically, the beginning of the matrix is:

const float* buffer=desc_mat.ptr<float>(0);

the issue that can happen if stride not equal to number of columns*4
4= size of float, you can get holes in the data so the only way to be sure is to copy pixel by pixel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM