Printing opencv key matrix descriptor values

I am having problems printing the values ​​of the descriptor matrix obtained using the "calculate" method of any opencv descriptor extractor. I want to print the function descriptor to a file one by one, but whenever I access some element of the descriptor matrix using "at", I get a different value for this element. The following is the for loop that I used to check the output of the descriptor matrix when using at.

for(int i=0; i<nF; i++){ if(lpx != keypoints[i].pt.x && lpy != keypoints[i].pt.y){ usedFeatures++; cerr << descriptors.row(i) << endl << endl; // printing row of descriptor matrix fileS << keypoints[i].pt.y << " " << keypoints[i].pt.x << " "; fileS << keypoints[i].size << " " << keypoints[i].angle << endl; if(i == nF - 2){ //printing subvector of descriptor matrix made of the element at row i and col 0 cerr << "get row i, col 0 " << descriptors.row(i).col(0) << endl; //same as before just inverting the order of access cerr << "get col 0, row i: " << descriptors.col(0).row(i) << endl; //printing the value of the element with 'at' cerr << (int)descriptors.at<uchar>(i, 0); //creating a new int and giving it the value of element (i, 0) of descriptor matrix. Should be the same //value shown on the subvector before int test = descriptors.at<uchar>(i, 0); //printing value of element cerr << "i, 0: " << test << endl; } 

The second "if" is the "if" test, which I did to see the values ​​printed when accessing the descriptor elements. Now printed

 cerr << descriptors.row(i) << endl << endl; 

when iterating nF - 2, I have the following result:

 [20, 11, 0, 18, 51, 3, 0, 3, 133, 50, 0, 0, 0, 0, 0, 11, 133, 18, 0, 0, 0, 0, 0, 3, 119, 2, 0, 0, 0, 0, 0, 2, 19, 5, 0, 4, 55, 27, 1, 1, 133, 25, 0, 1, 4, 1, 0, 22, 133, 18, 0, 0, 0, 0, 0, 14, 131, 13, 1, 0, 0, 0, 0, 1, 12, 1, 0, 1, 56, 133, 25, 13, 133, 14, 0, 0, 3, 8, 20, 80, 133, 38, 0, 0, 0, 0, 0, 51, 106, 11, 1, 0, 0, 0, 0, 23, 0, 0, 0, 0, 19, 126, 70, 11, 23, 0, 0, 0, 0, 9, 83, 133, 53, 1, 0, 0, 0, 0, 2, 133, 26, 3, 2, 0, 0, 0, 0, 28] 

And, as expected, the first two seals inside the second "if":

 cerr << "get row i, col 0 " << descriptors.row(i).col(0) << endl; cerr << "get col 0, row i: " << descriptors.col(0).row(i) << endl; 

give me [20]

But two other prints

 cerr << (int)descriptors.at<uchar>(i, 0); 

and

 int test = descriptors.at<uchar>(i, 0); cerr << "i, 0: " << test << endl; 

give me 0 instead of 20. The full result that I got for the line nF-2, which I showed earlier, when accessing elements with "at" and printing them:

  0 0 160 65 0 0 48 65 0 0 0 0 0 0 144 65 0 0 76 66 0 0 64 64 0 0 0 0 0 0 64 64 0 0 5 67 0 0 72 66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 65 0 0 5 67 0 0 144 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 64 0 0 238 66 0 0 0 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 

This is not at all what I expected. I've already tried a lot: casting with float, double, unsigned int instead of int and assigning variables to these types; transforming the matrix before printing, copying the matrix, then converting, creating a matrix of descriptors with a different type ... but none of them worked. I suspect this has something to do with the descriptor matrix type, although I'm pretty sure it has a uchar type (I checked with elemSize)

Thanks in advance, and sorry for my english and question size.

+8
c ++ matrix opencv feature-descriptor
source share
2 answers

Found the answer. It was really a type problem. The type of the returned descriptor matrix is ​​not what I thought it actually floats. Getting value with

 (float)descriptors.at<float>(i, 0); 

gives me the correct meaning. It's funny that I can swear I tried this for a float before, and it didn't work. I must have tried this only for int, double and unsigned int.

+7
source share

This does not answer why your problem occurs, but I remember that I had similar problems when trying to access my descriptor values.

I tried to write a piece of code that will work with any descriptors, since OpenCV has a lot of cv::DescriptorExtractor . The fact is that since I want to someday create my own independent OpenCV libraries that work with my descriptor interfaces, I need all the descriptors in the std::vector<vector<double> > structures.

Here is my code that converts cv::Mat descOld to std::vector< std::vector <double> > desc :

 cv::DescriptorExtractor *descCalc; // initialize descCalc descCalc->compute(*image, feats, descOld); // conversion to std::vector<std::vector <double> > : const double *temp; desc.clear(); desc.reserve(descOld.cols); for (int i=0, szi = this->desc.rows; i < szi; ++i){ temp = descOld.ptr<double>(i); desc.push_back(std::vector<double>(temp, temp+descOld.cols)); } assert(desc.size() == descOld.rows); assert(desc[0].size() == descOld.cols); 

Hope this helps.

+1
source share

All Articles