Reputation: 313
I have a matrix of 630 values (values range from 0-35
)...
I want to find the most frequently occurring value in this matrix. So how do I write a histogram for this? Also is there any other way that I can find the most frequently occurring value (I don't want to use counters as i will need 36 counters and My code would become very inefficient)
..Thanks!
Upvotes: 1
Views: 4135
Reputation: 1927
You can use calcHist with a Mat of size 1xN, where N is 630 in your case.
I don't understand your argument against counters. To build the histogram, you must use counters anyway. There are ways to make counting very efficient.
OR
Assuming your image is a cv::Mat variable im
with size 1x630
and type CV_8UC1
, try:
std::vector<int> counts(36, 0);
for (int c = 0; c < 630; c++)
counts.at(im.at<unsigned char>(1, c)) += 1;
std::cout << "Most frequently occuring value: " << std::max_element(counts);
This uses counting, but will not take more than 0.1ms on an average PC.
Upvotes: 3
Reputation: 5139
Why not do it manually?
Mat myimage(cvSize(1,638), CV_8U);
randn(myimage, Scalar::all(128), Scalar::all(20)); //Random fill
vector<int> histogram(256);
for (int i=0;i<638;i++)
histogram[(int)myimage.at<uchar>(i,0)]++;
Upvotes: 1