opencv kmeans从图片中返回错误的聚类中心

时间:2018-04-08 15:18:11

标签: c++ opencv k-means

我正试图从深度框架中获取聚类的中心,但不能找到正确的坐标。我正在关注堆栈溢出example给出的示例,但我的矩阵是512 * 424类型CV_16SC1(ushort,一个通道)。 无论我做什么,我得到错误或没有中心的回归(在左上角而不是中间中心)。任何人都可以向我解释如何做到这一点? 以下是原始和输出的示例:images

//my code so far:
FILE *infile= NULL; 
fopen_s(&infile, p1ficheiro, "rb"); //if... blá blá

unsigned short *buffer = new unsigned short[512*424];
fread(buffer, 512 * 424, sizeof(unsigned short), infile)

//entry matrix
Mat frame(424,512,CV_16SC1,buffer);

int dimension = 8;

//working Mat
Mat temp;
frame.copyTo(temp);
temp.convertTo(temp, CV_32FC1);
temp.reshape(dimension , (512 * 424) / dimension );

//going to kmeans...
Mat labels, centers;
kmeans(temp, dimension , labels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 1000,0.001), 10, KMEANS_RANDOM_CENTERS, centers);

//finding centers
for (int j = 0; j < centers.rows; ++j)
{
std::cout << centers.row(j) << std::endl;
circle(temp, Point(centers.at<float>(j, 0), centers.at<float>(j, 1)), 30, Scalar::all(255), 2);
}

//just so it's more clear to the eye
frame.convertTo(frame, CV_16SC1, 8);
imshow("IN",frame);
imshow("OUT",temp);
waitkey(0);

//... close and free everything...

0 个答案:

没有答案