I want to apply k-means clustering on the intensity values of a grayscale image. I'm really confused on how to represent the pixels into a vector. So if my image is H x W
pixels, then my vector should be H*W
dimensional.
What I've tried is :
int myClass::myFunction(const cv::Mat& img)
{
cv::Mat grayImg;
cvtColor(img, grayImg, CV_RGB2GRAY);
cv::Mat bestLabels, centers, clustered;
cv::Mat p = cv::Mat::zeros(grayImg.cols*grayImg.rows, 1, CV_32F);
int i = -1;
for (int c = 0; c<img.cols; c++) {
for (int r = 0; r < img.rows; r++) {
i++;
p.at<float>(i, 0) = grayImg.at<float>(r, c);
}
}
// I should have obtained the vector in p, so now I want to supply it to k-means:
int K = 2;
cv::kmeans(p, K, bestLabels,
cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
3, cv::KMEANS_PP_CENTERS, centers);
// Since K=2, I want to obtain a binary image with this, so the same operation needs to be reversed (grayImg -> p , then bestLabels -> binaryImage)
}
However I'm getting an error : Unhandled exception at 0x00007FFD76406C51 (ntdll.dll) in myapp.exe
I'm new to OpenCV so I'm not sure how to use any of these functions. I found this code here. For example, why do we use .at<float>
, some other post says that grayscale image pixels are stored as <char>
s ?? I'm getting confused more and more, so any help would be appreciated :)
Thanks !
Edit
Thanks to Miki, I found the right way to do it. But one final question, how do I see the contents of cv::Mat1b result
? I tried printing them like this :
for (int r = 0; r < result.rows; ++r)
{
for (int c = 0; c < result.cols; ++c)
{
result(r, c) = static_cast<uchar>(centers(bestLabels(r*grayImg.cols + c)));
if (result(r, c) != 0) {
std::cout << "result = " << result(r, c) << " \n";
}
}
}
But it keeps printing result=0
, even though I specifically ask it not to :) How do I access the values?