Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 993
justHelloWorld Avatar asked Apr 02 '26 07:04

justHelloWorld


1 Answers

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);
like image 182
Photon Avatar answered Apr 03 '26 20:04

Photon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!