从直方图中获取黑白强度值

时间:2011-08-12 15:30:31

标签: image-processing opencv histogram

我试图从彩色图像中获取黑白直方图数据。然而,我的直方图中的当前设置仅向我显示颜色数据我确信这是我必须在当前数学设置中修改的内容。

// Current setup on how to render histogram data to the screen with hist being the calculated histogram
histimg = Mat::zeros(200, 320, CV_8UC3)
int binW = histimg.cols / 16;
Mat buf(1, 16, CV_8UC3);
for( int i = 0; i < 16; i++ )
{
    buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./16), 255, 255);
}
cvtColor(buf, buf, CV_HSV2BGR);

for( int i = 0; i < 16; i++ )
{
    int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
    rectangle( histimg, Point(i*binW,histimg.rows),
        Point((i+1)*binW,histimg.rows - val),
        Scalar(buf.at<Vec3b>(i)), -1, 8 );
}

提前感谢任何建议。

1 个答案:

答案 0 :(得分:1)

以下是两种方法:

  1. 创建whiteCountblackCount个变量。迭代所有像素,如果像素为(255,255,255),则递增whiteCount,如果像素为(0,0,0),则递增blackCount

  2. 将图像转换为灰度,创建直方图并查看第一个和最后一个分档。