Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

histograms are not really useful for what you wanted. sure, for a single channel you could count the non-zero bins, but if you have 2 channels, your histogram is a 2d matrix, and for 3 channels a 3d cube, that's not practical.

but we can make integer (hex) numbers from our pixels, and collect them in a std::set, which will only keep the unique values:

std::set<int> unique;
// iterate over pixels
for (Vec3b &p : cv::Mat_<Vec3b>(ocv)) {
    // "hash" representation of the pixel
    int n = (p[0] << 16) | (p[1] << 8) | (p[2]);
    unique.insert(n);
}

cout << "we have " << unique.size() << " unique colors:" << endl;
for (int i: unique)
    cout << format("%06x", i) << endl;

histograms are not really useful for what you wanted. sure, for a single channel you could count the non-zero bins, but if you have 2 channels, your histogram is a 2d matrix, and for 3 channels a 3d cube, that's not practical.

but we can make integer (hex) numbers from our pixels, and collect them in a std::set, which will only keep the unique values:

std::set<int> unique;
// iterate over pixels
pixels (assummes: CV_8UC3 !)
for (Vec3b &p : cv::Mat_<Vec3b>(ocv)) cv::Mat_<Vec3b>(image)) {
    // "hash" representation of the pixel
    int n = (p[0] << 16) | (p[1] << 8) | (p[2]);
    unique.insert(n);
}

cout << "we have " << unique.size() << " unique colors:" << endl;
for (int i: unique)
    cout << format("%06x", i) << endl;