Wrong values in a Mat to vector<float> conversion

asked 2020-03-07 12:44:15 -0600

marcostrinca gravatar image

Hello.

I've been working in a C++ application which stores homography in a Mat matrix and I need to send this data to javascript using emscripten. That's why I'm doing this conversion process, from Mat to vector<float>. My only problem is the results from homography are totally different inside the vector. Is it somehow related to the cast process?

I've read some other posts and tried solutions like this:

//convert from CV::MAT to float*
Mat dst;
src.convertTo(dst, CV_32F);
float *data = dst.ptr<float>();

But then the javascript receives only the pointer address as an int. I still believe the best way is to convert the Mat to a float array or vector and send the data to my javascript function, but I got really odd numbers doing the conversion as you can see below:

std::vector<float> d; // vector to store the values
std::cout << "result_matrix: " << result_matrix << std::endl; // this matrix is my homography 3 x 3 

// set the vector with the Mat data converting to float
if (result_matrix.isContinuous()) {
    d.assign((float*)result_matrix.data, (float*)result_matrix.data + result_matrix.total());
}

// loop to print the values to compare with the original data
for (size_t i = 0; i < d.size(); i++) {
    std::cout << "converted data: " << d[i] << std::endl;
}


 result_matrix: [0.01563049386302596, -0.5622236989488044, 270.6183349435609, 0.03463755312658196, -0.3184187844468752, 150.4658828621841, 0.0001506408937758191, -0.002112295769558154, 1]

converted data: -3.94928e-10
converted data: 1.12504
converted data: -1.58611e-30
converted data: -1.76556
converted data: -4.06372e-08
converted data: 3.76428
converted data: 4.85436e+26
converted data: 1.26355
converted data: 5.28439e-11

Can you guys share some info about this, please? Thanks.

edit retag flag offensive close merge delete

Comments

1

I don't know, but for Mats, isContinuous should not be assumed, and thus, you may see garbage - and you don't check for "else".

mvuori gravatar imagemvuori ( 2020-03-07 13:04:10 -0600 )edit