Reputation: 79
There are a number of questions on here about calcHist in OpenCV but I couldn't find an answer to my question, and have read the documentation several times so here's to hoping someone can spot my problem with the following code:
//the setup is that I've got a 1x993 cv::Mat called bestLabels that contains cluster
//labels for 993 features each belonging to 1 of 40 different clusters. I'm just trying
//to histogram these into hist.
cv::Mat hist;
int nbins = 40;
int hsize[] = { nbins };
float range[] = { 0, 39 };
const float *ranges[] = { range };
int chnls[] = { 0 };
cv::calcHist(&bestLabels, 1, chnls, cv::Mat(), hist, 1, hsize, ranges);
This compiles, but when I run it, I get an error:
OpenCV Error: Unsupported format or combination of formats () in cv::calcHist
This was hard to just get it to compile in the first place, but now I'm really not sure what I'm missing. Help please!
Alternatively, I had tried to iterate through the elements of bestLabels and just increment the values in an array that would store my histogram, but using bestLabels.at(0,i) wasn't working either. There's got to be an easier way to pull individual elements out of a cv::Mat object.
Thanks for the help.
Upvotes: 2
Views: 2073
Reputation: 39806
What is the type of bestLabels ?
I can reproduce your error with CV_32S, but it works fine with CV_8U or CV_32F.
Maybe the easiest way is to convert it to uchar:
bestLabels.convertTo( bestLabels, CV_8U ); // CV_32F for float, might be overkill here
besides, a 'manual' histogram calculation is not sooo hard:
Mat bestLabels(1,933,CV_32S); // assuming 'int' here again
Mat hist(1,40,CV_8U,Scalar(0));
for ( int i=0; i<bestLabels.cols; i++ )
hist[ bestLabels.at<int>(0,i) ] ++;
Upvotes: 2