基本上,我有一个球员位置的两个点,我想聚集来自Vec2f的球员位置。
我尝试了这个代码示例,但它不起作用,并在调用kmeans函数时给出了一个exceptin。
float *pointsdata = new float[10];
for (std::map< unsigned int, Player >::iterator it = players.begin(); it != players.end(); ++it)
{
for (int i =0; i< players.size(); i++)
{
pointsdata[i] = it->second.m_Position.x;
pointsdata[i+1] = it->second.m_Position.y;
}
}
//float pointsdata[] = { 1,1, 2,2, 6,6, 5,5, 10,10};
cv::Mat points(10, 2, CV_32F, *pointsdata);
cv::Mat labels, centers;
cv::kmeans(points, 3, labels, cv::TermCriteria(CV_TERMCRIT_EPS, 1000, 0), 1000, cv::KMEANS_RANDOM_CENTERS, centers);
cout << "labels: " << labels << endl;
cout << "centers " << centers << endl;
答案 0 :(得分:0)
你有cv :: Mat API here。您尝试使用的构造函数如下,对吧?
Mat (int _rows, int _cols, int _type, void *_data, size_t _step=AUTO_STEP)
constructor for matrix headers pointing to user-allocated data
所以你应该做以下事情:
if(players.size() > 10)
{
cout << "ERROR, too many players\n";
// Do some error handling here
return;
}
//float pointsdata[10][2]; // use this if it doesnt need to be dynamic
/* 2D definition, but we need to create it as a 1D array with room for the 2d
float **pointsdata = new float*[10];
for(int i = 0; i < 10; ++i)
{
pointsdata[i] = new float[2];
}
*/
float *pointsdata = new float[10*2];
for (std::map< unsigned int, Player >::iterator it = players.begin();
it != players.end();
++it)
{
for (int i =0; i< players.size() && i < 10; i++)
{
pointsdata[i] = it->second.m_Position.x;
pointsdata[i + 10*i] = it->second.m_Position.y;
}
}
cv::Mat points(10, 2, CV_32F, pointsdata);