简体   繁体   中英

OpenCV Histogram doesn't count white pixels in BGR from GRAY

I'm using OpenCV combined with C++ to get the histogram (using calcHist function) of the image in BGR, which is converted from grayscale with GRAY2BGR.

The weird thing here is that histogram seems to count all pixels except... white ones (255, 255, 255).

What can be the case? Of course I can convert it in HSV and look up for S = 0 to detect white pixels (I hope it'll work, haven't tested it yet) but why the heck should I do so when I've the histogram counted already? Do you have any ideas?

PS. When it goes to the code, I use the recipe from popular "OpenCV Cookbook", chapter 4 for counting the color histogram. I read specific histogram bin with this line:

int val1 = histo.at<float>(col1.blue(),col1.green(),col1.red());

Where col1 is QColor type (RGB so I read the values in order as shown above to get BGR).

Did you set up the ranges correctly?

Based on the example: http://docs.opencv.org/modules/imgproc/doc/histograms.html#calchist

cv::Mat bgr(480, 640, CV_8UC3, cv::Scalar(255.0, 255.0, 255.0));

// Quantize the B, G and R channels to 30 levels
int histSize[] = {30, 30, 30};

// B, G and R varies from 0 to 255
float bRanges[] = { 0, 256 };
float gRanges[] = { 0, 256 };
float rRanges[] = { 0, 256 };
const float* ranges[] = { bRanges, gRanges, rRanges };

// we compute the histogram from the 0th and 2nd channels
int channels[] = {0, 1, 2};

cv::MatND hist;
cv::calcHist(&bgr, 1, channels, cv::Mat(), hist, 2, histSize, ranges, true, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM