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

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:41:02

问题


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).


回答1:


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);


来源:https://stackoverflow.com/questions/25180242/opencv-histogram-doesnt-count-white-pixels-in-bgr-from-gray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!