justHelloWorld
justHelloWorld

Reputation: 6842

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

Upvotes: 3

Views: 8297

Answers (2)

michael scheinfeild
michael scheinfeild

Reputation: 1

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

Upvotes: -1

Photon
Photon

Reputation: 3222

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

Upvotes: 7

Related Questions