问题描述:

I am trying to get black and white histogram data from a color image. However the current setup I have with my histogram only shows me color data I'm sure that it's something that I have to modify in my current math setup.

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

}

Thanks in advance for any advice.

网友答案:

Here are two methods:

  1. Create whiteCount and blackCount variables. Iterate through all the pixels and increment whiteCount if the pixel is (255, 255, 255) and increment blackCount if the pixel is (0, 0, 0).

  2. Convert the image to grayscale, create a histogram and look at the first and last bins.

相关阅读:
Top