OpenCV直方图不从GRAY计算BGR中的白色像素 [英] OpenCV Histogram doesn't count white pixels in BGR from GRAY

查看:339
本文介绍了OpenCV直方图不从GRAY计算BGR中的白色像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV结合C ++来获取BGR中的图像的直方图(使用calcHist函数),它是使用GRAY2BGR从灰度转换的。

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.

奇怪的是这里的直方图似乎计数除了...白色的所有像素(255,255,255)。

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

可能是什么情况?当然,我可以在HSV中转换它,并查找S = 0来检测白色像素(我希望它会工作,还没有测试它)但为什么我应该这样做时,我已经直方图计数已经?你有什么想法吗?

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。当它去代码,我使用流行的OpenCV Cookbook的食谱,第4章计数颜色直方图。我用这一行读取具体的直方图bin:

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

其中col1是QColor类型(RGB,所以我按照如上所示的顺序读取值以获得BGR) 。

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?

示例: 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);

这篇关于OpenCV直方图不从GRAY计算BGR中的白色像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆