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 !
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?
You don't need to convert from Mat
to InputArray
, but you can (and should) just pass a Mat
object where an InputArray
is requested. See here for a detailed explanation
kmeans accepts an InputArray, that should be an array of N-Dimensional points with float coordinates is needed.
With Mat
objects, you need img.at<type>(row, col)
to access value of the pixel. You can, however, use Mat_
that is a templated version of Mat
where you fix the type, so you can access the value just like img(r,c)
.
So the final code will be:
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
Mat1b grayImg = imread("path_to_image", IMREAD_GRAYSCALE);
Mat1f data(grayImg.rows*grayImg.cols, 1);
for (int r = 0; r < grayImg.rows; r++)
{
for (int c = 0; c < grayImg.cols; c++)
{
data(r*grayImg.cols + c) = float(grayImg(r, c));
}
}
// Or, equivalently
//Mat1f data;
//grayImg.convertTo(data, CV_32F);
//data = data.reshape(1, 1).t();
// I should have obtained the vector in p, so now I want to supply it to k-means:
int K = 8;
Mat1i bestLabels(data.size(), 0); // integer matrix of labels
Mat1f centers; // float matrix of centers
cv::kmeans(data, K, bestLabels,
cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
3, cv::KMEANS_PP_CENTERS, centers);
// Show results
Mat1b result(grayImg.rows, grayImg.cols);
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)));
}
}
imshow("Image", grayImg);
imshow("Result", result);
waitKey();
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With