I am trying to understand the following piece of code Take from: Opencv Mat and more precisely this part:

Mat labels(0, 1, CV_32FC1);
Mat trainingData(0, dictionarySize, CV_32FC1);

From what I understand is that labels is equivalent to std::vector<float> and trainingData is equivalent to std::vector<std::vector<float>> and where std::vector<float> is of dimension dictionarySize. Is that correct?

I am asking this question because I want to convert bowDescriptor1 which is a MAT to std::vector<float>


Convert bowDescriptor1to vector:

   std::vector<float> data;
   for(size_t r = 0; r < bowDescriptor.rows;r++)
    {
       for(size_t c = 0; c < bowDescriptor.cols;c++)
       {
          data.push_back(bowDescriptor.at<float>(r,c));
        }
     }
有帮助吗?

解决方案

Without testing:

from documentation you can see that bowDescriptor seems to be a matrix of size 1 x dictionarySize http://docs.opencv.org/modules/features2d/doc/object_categorization.html#bowimgdescriptorextractor-descriptorsize

so you have to go through that matrix and save each element (float) to your vector<float>

try this code:

std::vector<float> currentBowDescriptor;
for(int col = 0; col < bowDescriptor1.cols; ++col)
{
    currentBowDescriptor.push_back(bowDescriptor.at<float>(0,col));
}

that's it. push_back those currentBowDescriptor s to another vector if you want.

If you want to save some computation time, you can even initialize the currentBowDescriptor in advance since you know the number of descriptor values (dictionarySize) and access those elements instead of pushing back.

hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top