首页 文章

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

提问于
浏览
0

我正在使用OpenCV与C结合来获得BGR中图像的直方图(使用calcHist函数),该图像是使用GRAY2BGR从灰度转换而来的 .

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

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

PS . 当它进入代码时,我使用流行的“OpenCV Cookbook”第4章中的配方来计算颜色直方图 . 我用这行读了特定的直方图bin:

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

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

1 回答

  • 2

    你是否正确设置了范围?

    基于以下示例: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);
    

相关问题