you know k. are the labels 1…k, and 0 is background? then you could, for i = 0 to k, calculate cv::countNonZero(labels == i)
there’s also calcHist
, and calculating a histogram is generally what you want to do here, but I hate OpenCV’s function because it’s so awkward to call.
or use std::count
and give it the flat data from the Mat. you can use ptr()
to get the data: OpenCV: cv::Mat Class Reference