简体   繁体   English

OpenCV直方图不计算来自灰色的BGR中的白色像素

[英]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. 我将OpenCV与C ++结合使用以获取BGR中图像的直方图(使用calcHist函数),该图像是使用GREY2BGR从灰度转换而成的。

The weird thing here is that histogram seems to count all pixels except... white ones (255, 255, 255). 奇怪的是,直方图似乎要计算除白色像素(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? 当然,我可以将其转换为HSV并查找S = 0以检测白色像素(我希望它可以工作,尚未进行过测试),但是当我已经计算完直方图后为什么要这样做呢? ? Do you have any ideas? 你有什么想法?

PS. PS。 When it goes to the code, I use the recipe from popular "OpenCV Cookbook", chapter 4 for counting the color histogram. 当涉及到代码时,我使用流行的“ OpenCV Cookbook”第4章中的配方对颜色直方图进行计数。 I read specific histogram bin with this line: 我在这行中读取了特定的直方图bin:

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). 其中col1是QColor类型(RGB,所以我按上面显示的顺序读取值以获得BGR)。

Did you set up the ranges correctly? 您是否正确设置了范围?

Based on the example: http://docs.opencv.org/modules/imgproc/doc/histograms.html#calchist 根据示例: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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