Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Weird numbers with cv::Mat

I have this struct:

struct Result{
    Result(const cv::Mat1f &descriptor, const Keypoint &keypoint) :
        descriptor(descriptor), keypoint(keypoint){
      std::cout<<"constructor="<<std::endl<<descriptor<<std::endl;
    }
    const cv::Mat1f descriptor;
    const Keypoint keypoint;
};

Where Keypoint I think is irrelevant for this question (but I'll post it if you ask about it).

I have this function:

void foo(...,vector<Result> &res){
...
std::vector<float> vec;
//fill vec
cv::Mat1f desc(1,128,vec.data());
std::cout<<"desc="<<std::endl<<desc<<std::endl;
res.push_back(Result(desc, Keypoint(...)));
}

Which is called like this:

std::vector<Result> res;
for(int i=0; i<N; i++){
   foo(..., res);
   std::cout<<"res="<<std::endl<<res.back().descriptor<<std::endl;
}

Sometimes, the three std::cout print the same value. But in some cases, the last print (the one inside after calling foo) gives different results:

desc=
[2, 10, 54, 3, 2, 2, 5, 6, 7, 27, 111, 5, 3, 17, 11, 12, 8, 31, 113, 6, 3, 6, 10, 27, 2, 8, 63, 9, 13, 17, 2, 1, 54, 10, 16, 5, 13, 22, 18, 85, 37, 18, 17, 3, 99, 113, 29, 41, 113, 7, 21, 5, 28, 40, 34, 113, 12, 6, 14, 10, 93, 113, 8, 23, 54, 21, 50, 31, 20, 20, 7, 78, 18, 69, 82, 29, 86, 108, 9, 20, 77, 44, 98, 54, 17, 20, 15, 113, 27, 44, 29, 14, 53, 74, 10, 31, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]
constrcutor=
[2, 10, 54, 3, 2, 2, 5, 6, 7, 27, 111, 5, 3, 17, 11, 12, 8, 31, 113, 6, 3, 6, 10, 27, 2, 8, 63, 9, 13, 17, 2, 1, 54, 10, 16, 5, 13, 22, 18, 85, 37, 18, 17, 3, 99, 113, 29, 41, 113, 7, 21, 5, 28, 40, 34, 113, 12, 6, 14, 10, 93, 113, 8, 23, 54, 21, 50, 31, 20, 20, 7, 78, 18, 69, 82, 29, 86, 108, 9, 20, 77, 44, 98, 54, 17, 20, 15, 113, 27, 44, 29, 14, 53, 74, 10, 31, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]
res=
[1.1986551e+10, 0, 8.6976665e+23, 0, 0.00012219016, 1.0372148e-08, 6.3080874e-10, 0, 7, 27, 111, 5, 127.62504, 0, 0, 0, 8.6650199e-38, 0, 8.6650199e-38, 0, 8.6653068e-38, 0, 8.6653068e-38, 0, 0, 0, 0, 0, 8.6650512e-38, 0, 8.6650916e-38, 0, 0, 0, 0, 0, 0, 22.000002, 0, 0, 0, 0, 8.6651656e-38, 0, 0, 0, 8.6652015e-38, 0, 0, 0, 0, 5, 1.6213893e+09, 0, 0, 0, 12, 6, 0, 0, 144173.88, 0, 8.6651544e-38, 0, 0, 21.000174, 50, 31, 20, 20, 7, 78, 18, 69, 0, 0, 144173.88, 0, 8.6651903e-38, 0, 0, 44.000355, 98, 54, 17, 20, 15, 113, 27, 44, 0, 0, 144173.88, 0, 144173.88, 0, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]

And finally, the weirdest thing: if I print all the descriptors after this for, they have all the same values equal to the last descriptor, which is always wrong. So if I print all the descriptor with the following code:

for(size_t i=0; i<res.size(); i++)
  std::cout<<res[i].descriptor<<std::endl;

It gives always the same result for all is, something like:

[0, 0, 0, 0, 0, 0, 4.0742979e-37, 0, 0, 0, 4.0743266e-37, 0, 0, 0, 0, 0, 1.507816e+22, 0, 0, 0, 0, 0, 0, 0, 4.0743105e-37, 0, 4.074289e-37, 0, 0, 0.01574724, 0.015773401, 0.015773401, 0.0198367, 0.0198367, 0.0198367, 0.0198367, 0, 0, 0, 0, 0, 0, 4.0743177e-37, 0, 0, 0.024292165, 0.02440805, 0.02440805, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0, 0, 1.3678705e+18, 0, 1.3678705e+18, 0, 0.034611408, 0.034611408, 0.034611408, 0.034611408, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159]

Weird numbers with cv::Mat

I have this struct:

struct Result{
    Result(const cv::Mat1f &descriptor, const Keypoint &keypoint) :
        descriptor(descriptor), keypoint(keypoint){
      std::cout<<"constructor="<<std::endl<<descriptor<<std::endl;
    }
    const cv::Mat1f descriptor;
    const Keypoint keypoint;
};

Where Keypoint I think is irrelevant for this question (but I'll post it if you ask about it).

I have this function:

void foo(...,vector<Result> &res){
...
std::vector<float> vec;
//fill vec
cv::Mat1f desc(1,128,vec.data());
std::cout<<"desc="<<std::endl<<desc<<std::endl;
res.push_back(Result(desc, Keypoint(...)));
}

Which is called like this:

std::vector<Result> res;
for(int i=0; i<N; i++){
   foo(..., res);
   std::cout<<"res="<<std::endl<<res.back().descriptor<<std::endl;
}

Sometimes, the three std::cout print the same value. But in some cases, the last print (the one inside after calling foo) gives different results:

desc=
[2, 10, 54, 3, 2, 2, 5, 6, 7, 27, 111, 5, 3, 17, 11, 12, 8, 31, 113, 6, 3, 6, 10, 27, 2, 8, 63, 9, 13, 17, 2, 1, 54, 10, 16, 5, 13, 22, 18, 85, 37, 18, 17, 3, 99, 113, 29, 41, 113, 7, 21, 5, 28, 40, 34, 113, 12, 6, 14, 10, 93, 113, 8, 23, 54, 21, 50, 31, 20, 20, 7, 78, 18, 69, 82, 29, 86, 108, 9, 20, 77, 44, 98, 54, 17, 20, 15, 113, 27, 44, 29, 14, 53, 74, 10, 31, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]
constrcutor=
[2, 10, 54, 3, 2, 2, 5, 6, 7, 27, 111, 5, 3, 17, 11, 12, 8, 31, 113, 6, 3, 6, 10, 27, 2, 8, 63, 9, 13, 17, 2, 1, 54, 10, 16, 5, 13, 22, 18, 85, 37, 18, 17, 3, 99, 113, 29, 41, 113, 7, 21, 5, 28, 40, 34, 113, 12, 6, 14, 10, 93, 113, 8, 23, 54, 21, 50, 31, 20, 20, 7, 78, 18, 69, 82, 29, 86, 108, 9, 20, 77, 44, 98, 54, 17, 20, 15, 113, 27, 44, 29, 14, 53, 74, 10, 31, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]
res=
[1.1986551e+10, 0, 8.6976665e+23, 0, 0.00012219016, 1.0372148e-08, 6.3080874e-10, 0, 7, 27, 111, 5, 127.62504, 0, 0, 0, 8.6650199e-38, 0, 8.6650199e-38, 0, 8.6653068e-38, 0, 8.6653068e-38, 0, 0, 0, 0, 0, 8.6650512e-38, 0, 8.6650916e-38, 0, 0, 0, 0, 0, 0, 22.000002, 0, 0, 0, 0, 8.6651656e-38, 0, 0, 0, 8.6652015e-38, 0, 0, 0, 0, 5, 1.6213893e+09, 0, 0, 0, 12, 6, 0, 0, 144173.88, 0, 8.6651544e-38, 0, 0, 21.000174, 50, 31, 20, 20, 7, 78, 18, 69, 0, 0, 144173.88, 0, 8.6651903e-38, 0, 0, 44.000355, 98, 54, 17, 20, 15, 113, 27, 44, 0, 0, 144173.88, 0, 144173.88, 0, 8, 16, 36, 45, 39, 8, 4, 1, 35, 105, 45, 16, 11, 5, 4, 3, 6, 28, 53, 92, 32, 5, 5, 2, 27, 61, 23, 14, 9, 4, 6, 4]

And finally, the weirdest thing: if I print all the descriptors after this for, they have all the same values equal to the last descriptor, which is always wrong. So if I print all the descriptor with the following code:

for(size_t i=0; i<res.size(); i++)
  std::cout<<res[i].descriptor<<std::endl;

It gives always the same result for all is, something like:

[0, 0, 0, 0, 0, 0, 4.0742979e-37, 0, 0, 0, 4.0743266e-37, 0, 0, 0, 0, 0, 1.507816e+22, 0, 0, 0, 0, 0, 0, 0, 4.0743105e-37, 0, 4.074289e-37, 0, 0, 0.01574724, 0.015773401, 0.015773401, 0.0198367, 0.0198367, 0.0198367, 0.0198367, 0, 0, 0, 0, 0, 0, 4.0743177e-37, 0, 0, 0.024292165, 0.02440805, 0.02440805, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0.029384371, 0, 0, 1.3678705e+18, 0, 1.3678705e+18, 0, 0.034611408, 0.034611408, 0.034611408, 0.034611408, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.039887957, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.04497632, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.049618732, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.053558331, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.05656242, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.059086569, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159, 0.058445159]

UPDATE:

I noticed that everything goes fine if I do:

Result(const cv::Mat1f &descriptor, const Keypoint &keypoint) : descriptor(descriptor.clone()), keypoint(keypoint){ std::cout<<"constructor="<<std::endl&lt;<descriptor&lt;<std::endl; }="" why?<="" p="">